Professional Documents
Culture Documents
File System
File System
File System
• If you want to make only minor changes to the input data each
time you execute the program.
• For example, if your income is the same every month and only
your expenses change, it is cumbersome to repeatedly type the
same number for each month.
Why is use the file system in program?
FILE *inptr;
inptr=fopen("file.txt","r");
fscanf(inptr,"%d",&i); Output:
fscanf(inptr,"%d %1f",&k,&x); 36
123
fclose(inptr); 5.34643e-315
cout<<i<<endl<<k<<endl<<x;
return 0;
}
Exercise
Write a program to read your score from last semester form an
input file named mark.txt, which has one line of data consisting
of four subject marks (no characters); for example,
80 70 60 65
compute your average mark, and write all the input data and
average mark on the screen.
WRITING output to a file
Opening and closing a file
FILE *fileptr;
fileptr = fopen(filename, access_mode);
Read mode (“r”)
Write mode (“w”)
fclose(fileptr);
FILE *myfile;
myfile=fopen("fileOut.txt","w");
fprintf(myfile,"Week=%d\nYear=%d\n",week,year);
fprintf(myfile,"Income=%lf\nExpenses=%lf\n",income,expenses);
fclose(myfile);
return 0;
}
Exercise
Write a program that writes the multiplication tables (from 1 to
16) in a text file (mul.txt). The format that will be printed at the
text file must look like this
1*1=1
1*2=2
...
16*11=176
16*12=192
Thank You!!