Arman Akbarian
UNIVERSITY OF BRITISH COLUMBIA
PHYSICS & ASTRONOMY DEPT.

~/examples/C/C_IO.c.html

/* AAK:
 * This is an example code to learn about 
 * input/output facilities in C and working
 * with files 
 * last update:
 * Sat Sep 15 17:09:23 PDT 2012
 */
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]){
   char *cpath;
   FILE *TF; /* To output to a text file */
   FILE *BF; /* To output to a binary file */
   double x[10];
   double z[5];
   int n;
   int file_pos;
   double y;
   int size;
   char str[10];
   int i,rc;
   double r;
   char line[256];


   /* This is how to get the envirounment variables:*/
   /* Need to include stdlib.h header*/

   cpath = getenv("PATH");
   if (cpath !=NULL)
   {
      printf("The current path is: %s\n",cpath);
   }
   cpath = getenv("MYHOME");
   if (cpath !=NULL) {
      printf("MYHOME env is : %s\n",cpath);
   }
   else {
      printf("MYHOME env is not defined!\n");
   }

   /* argc is number of argument passe in (no arg >> argc=1)*/
   /* since by default the name of the program also is an argument */

   printf("%d arguments are passed into the program\n",argc-1);

   /* argv is array of strings that stores the actual arguments*/
   /* note that argv[0] is the name of the program */

   printf("The arguments that are passed in are:\n");

   for(int i=1;i<argc;i++){
      printf(" %s ",argv[i]);
   }
   printf("\n");

   /*
    in stdio.h: file *fopne(const char *fname, const char *opt)
    opt can be:
    r: open text file for reading
    w: create new file to write (or truncate to zero if exist)
    a: appends to the end of existing text file
    r+: open for updating (read/write)
    w+: create file for read/write
    a+: open file for reading/appending
    rb: open binary file for reading
    wb: open binary file for writing
    ab: open binary file for appending
    other options: wb+, ab+, rb+: similar
    */

   for(int i=0;i<10;i++){ x[i]= (i+1)*1.0000000101e+2;}

   TF = fopen("data.txt","w");
   BF = fopen("data.dat","wb");
   for(int i=1;i<=10;i++) {
      /* writing formatted text to text file: */
      fprintf(TF,"Data   %d    %2.14e\n",i,x[i-1]);
      /* writing binary (double precision real) to binary file */
      /* format: fwrite(pointer to data, # of data to write, size of each data, file) */
      fwrite(x, 10, sizeof(double),BF); /* outputs the whole array */
   }
   /* closing the files */
   fclose(TF);
   fclose(BF);

   BF = fopen("data.dat","r");
   TF = fopen("data.txt","r");


   if ( (TF == NULL) || (BF == NULL) ) {
      /* sending the text to standard error: */
      fputs("Can't open the files data.dat or data.txt",stderr);
   } else {
      puts("Files opened!"); /* no need for \n, output = standard output */
      /* or equvalently you can send to standard output, specified by "stdout" */
      fputs("Files opened...\n",stdout);
   }

   /* reading from binary: */

   n = fread(z,sizeof(double),5,BF);

   printf("%d block was read from binary file\n",n);
   printf("Values are: %f %f %f %f %f \n",z[0],z[1],z[2],z[3],z[4]);

   /* The position of file indicator is different now */
   n = fread(z,sizeof(double),5,BF);
   printf("The rest of data is: %f %f %f %f %f \n",z[0],z[1],z[2],z[3],z[4]);

   /* fseek changes the position of file indicator (where to read)
    * fseek(file *str, long int offset, int whence) 
    * whence can be: int position default defined vars:
    * SEEK_SET: beginning of file
    * SEEK_END: end of file
    * SEEK_CUR: current position
    * The following starts from file position 2 i.e. x[2] in data */
   file_pos = fseek(BF,3*sizeof(double),SEEK_SET);

   fread(&y,sizeof(double),1,BF);
   printf("4th element is : %f\n",y);
   /* rewind also brings the indicator to the beginning of the file */
   rewind(BF);

   fread(&y,sizeof(double),1,BF);
   printf("First element is: %f\n",y);
   /* and if we read now, it will start from beginning */
   /*ftell returns the current position of indicator */

   printf("The current position of file indicator: %d\n",ftell(BF));

   /*reading formatted text file */


   printf("Read the following data from data.txt\n");
   for(i=0;i<=12;i++){
      rc = fscanf(TF,"%s %d %lf",str,&n,&r);
      printf("string = %s, integer = %d, real = %2.14e, fscanf_return = %d\n",str,n,r,rc);
   }
   /* Note that when it reaches to end of file, it keeps returning the
    * same value while fscanf will return -1 if it can't read more */

   fclose(TF);
   fclose(BF);
   /* Another way of  reading double precision number from text file */

   r = 9999999999;
   TF = fopen("data2.txt","r");
   while (fgets(line,256,TF)) {
      sscanf(line,"%lf",&r);
      printf("read %1.14e\n",r);
   }
   fclose(TF);
}


last update: Wed May 11, 2016