Académique Documents
Professionnel Documents
Culture Documents
PROGRAMMING
Chapter 10: Data Files & File Processing
Objectives
Example:
thefile = fopen(my_file.txt,r);
The following is the list of open mode that can be associated with
fopen( ).
r : to open existing file in input mode so data can be read from it.
The file to be opened must exist phisically on disk for this open mode
to be successful.
w : the file is intended to be an output file and we are planning to
write data to it.
a : to append data to the end of existing file.
thefile = fopen(filename,"r");
#include <stdio.h>
int main(void)
{
char ch;
FILE *thefile;
if ((thefile = fopen(my_file.txt","r")) == NULL)
{
printf("File fail to open\n");
}
else
{
ch = fgetc(thefile); /* read a character from the file */
printf("%c",ch); /* print the value of ch to the screen */
}
fclose(thefile);
return 0;
}
prepared by NI, edited by MAF
fputc( )
Example:
char name[50];
int age;
fscanf(theFile, %s %d, name, &age);
prepared by NI, edited by MAF
fprintf( )
Syntax:
fgets(stringVariable, size, internal_name);
Example:
char content[50];
fgets(content, 50, theFile);