DaidalosCheung / C

0 stars 0 forks source link

File I/O #8

Open DaidalosCheung opened 3 years ago

DaidalosCheung commented 3 years ago

FILE I/O

Function

DaidalosCheung commented 3 years ago

fscanf

Live Demo

#include <stdio.h>
#include <stdlib.h>

int main () {
   char str1[10], str2[10], str3[10];
   int year;
   FILE * fp;

   fp = fopen ("file.txt", "w+");
   fputs("We are in 2012", fp);

   rewind(fp);
   fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);

   printf("Read String1 |%s|\n", str1 );
   printf("Read String2 |%s|\n", str2 );
   printf("Read String3 |%s|\n", str3 );
   printf("Read Integer |%d|\n", year );

   fclose(fp);

   return(0);
}

Let us compile and run the above program that will produce the following result −

Read String1 |We| Read String2 |are| Read String3 |in| Read Integer |2012|

DaidalosCheung commented 3 years ago

ftell & fseek function

/* This program try to demonstrate the function 
   ftell: report the current position in file
   fseek: A calculated record number (SEEK_SET)
          A position relative to the current position (SEEK_CUR)
      A position relative to the end of the file (SEEK_END)
 */
#include <stdio.h>

int main () {
   FILE *fp;
   int len;
   long position;

   fp = fopen("file.txt", "r");
   if( fp == NULL )  {
      perror ("Error opening file");
      return(-1);
   }
   // without this line, the result will be 0;
   /* fseek(fp, 0, SEEK_END);  */ //tell the end positon
   /* fseek(fp, 25, SEEK_SET); */ //tell 25 bytes from the beginning
   /* fseek(fp, 3, SEEK_CUR); */  //tell the 3 behind the current position
   /* len = ftell(fp); */
   /* printf("Total size of file.txt = %d bytes\n", len); */

   for (int i = 0; i < 9; i++) {
     fseek (fp, 3, SEEK_CUR);
     position = ftell(fp);
     printf("The current position is %3ld.\n", position);
   }

   fclose(fp);

   return(0);
}
DaidalosCheung commented 3 years ago

fwrite & fprintf

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char * argv[]) {
  FILE *fp = NULL;
  char * message = "A whole new world, A dazzling place I never knew. But when I'm way up here, it's crystal clear. That now I'm in a whole new world with you(Now I'm in a whole new world with you)";
  if ( (fp = fopen("aticleworld.dat", "wb")) == NULL ) {
    printf("Cannot open the file.\n");
    exit(EXIT_FAILURE);
  }

  int len = strlen(message);

  fwrite(&len, sizeof(int), 1, fp);
  fwrite(message, sizeof(char), len, fp);
  fprintf(fp, "\nThe total len is %d.\n", len);

  fclose(fp);
  return 0;
}

Result in "aticleworld.dat" \B1\00\00\00A whole new world, A dazzling place I never knew. But when I'm way up here, it's crystal clear. That now I'm in a whole new world with you(Now I'm in a whole new world with you) The total len is 177.

DaidalosCheung commented 3 years ago

perror

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char * argv[]) {
  FILE * fp;
  char file[20];

  /* first rename if there is any file */
  rename("file.txt", "newfile.txt");

  /* now let's try to open same file */
  fp = fopen("file.txt", "r");
  if (fp == NULL) {
1. perror("Error: ");
2. printf(stderr, "Error, cannot open %s file\n", file);
    return(-1);
  }

  fclose(fp);
  return(0);
}

Output :

  1. Error: : No such file or directory
  2. Error, cannot open ��yC file