Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer science 2009-2010 C programming language lesson 4.

Similar presentations


Presentation on theme: "Computer science 2009-2010 C programming language lesson 4."— Presentation transcript:

1 Computer science 2009-2010 C programming language lesson 4

2 Aims of lesson 4 Reading from / writing to files

3 In general a program needs : Files - To read data (text, numbers, images, …) Need to write and to read in files from inside the program - To save results (text, numbers, images, …) To manipulate a file one needs to use a pointer : FILE *fichier fichier is called a dataflow. Opening a file : FILE *fopen (char *nom, char *mode) Example : fichier = fopen(“C:/Data/file1.txt”,”r”); This function returns a pointer on the open file

4 Files mode explains how to use the dataflow : - “r” : read - “w” : write (if existing the file is suppressed) - “a” : append (at the end of an existing file) Closing a file : int fclose (FILE *fichier) nom is a character string (character array) containing the file name or a standard dataflow (stdin, …) FILE *fopen (char *nom, char *mode) Always close a file after use ! Example : fclose(fichier);

5 Reading and writing functions Writing : double a; fprintf(fichier,«Var = %lf\n»,a); fprintf(FILE *fichier,*char proto,…); printf(«Var = %lf\n»,a); Same syntax as printf ! Example : Reading : fscanf(FILE *fichier,*char proto,…); double a; fscanf(fichier,«Var = %lf\n»,&a); scanf(«Var = %lf\n»,&a); Same syntax as scanf ! Example :

6 Example - Writing include void main() { double a,b; FILE *fichier; a=1.5;b=2.5; // Open the file in writing mode fichier = fopen("C:/goudail/fichier.txt","w"); // Check if opening was successful if (fichier != NULL) { // Write data to the file fprintf(fichier,"%lf\n",a); fprintf(fichier,"%lf\n",b); // Close the file fclose(fichier); }

7 Example - Reading #include void main() { int i; double tab[2]; double *p; FILE *fichier // Open file in reading mode fichier = fopen("C:/goudail/fichier.txt","r"); if (fichier != NULL) { p=tab; // Initialize pointer p for(i=0;i<2;i++) scanf(fichier,"%lf\n",p++); fclose(fichier); } for(i=0;i<2;i++) printf("%lf\n",tab[i]); }

8 File formats The preceding files were written in « text » format -> we have used the fprintf and fscanf functions -> the ASCII code of each character is written in the file When we use numerical data, it is often more efficient to write them in a binary form. Consider the number 1.345643355454E-18 In double format, its size is 8 bytes In ASCII format, its size is 20 bytes (1 byte per character) Writing a data bloc in binary format : int fwrite(void *source,int size, int count, FILE *flow) Reading a data bloc in binary format : int fread(void *dest,int size, int count, FILE *flow) The data are registered thanks to a pointer

9 Example – Binary file #define DIM 10000 void main() { int i; double sum,tab1[DIM],tab2[DIM]; FILE *fichier; // Fill the array for(i=0;i<DIM;i++) {tab1[i]=i*atan(1);} // Write to the file in binary mode fichier = fopen("C:/goudail/fichier.bin","wb"); if (fichier != NULL) { fwrite(tab1,sizeof(double),DIM,fichier); fclose(fichier); } // Read the file fichier = fopen("C:/goudail/fichier.bin","rb"); if (fichier != NULL) { fread(tab2,sizeof(double),DIM,fichier); fclose(fichier); }


Download ppt "Computer science 2009-2010 C programming language lesson 4."

Similar presentations


Ads by Google