CSE109 Week10
CSE109 Week10
Lecture: 21-22
Reference Book: Teach yourself C (3rd Ed.)
Chapter/Section: -
• Binary
• No character translation will occur
• There is one-to-one correspondence
Open a File
FILE *fopen (char *filename, char
*mode);
• In <stdio.h>
• Filename
Mode Strings
• Path of the file
• “r”, “w”, “a”, “
• Mode
• r+”, “w+”,”a+”
• Text/binary (b)
• Read (r)/write (w)/append (a) • “rb”,”wb”, “ab”,
size_t fwrite (void *buffer, size_t size, size_t num, FILE *fp);
• buffer: address of the memory which is the start address to write
• size: size of the object to be written
• num: number of the objects to be written
• fp: the stream of the destination file where we want to write
Use of fwrite and fread
#include<stdio.h> #include<stdio.h>
struct student struct student
{ {
int id; int id;
double cgpa; double cgpa;
}; };
int main(){ int main(){
int n,i, max; int n,i, max;
struct student s[10]; struct student s[10];
FILE *fp; FILE *fp;
fp = fopen("f5","wb"); fp=fopen("f5","rb");
if(fp==NULL) if(fp==NULL)
return 1; return 1;
scanf("%d",&n); scanf("%d",&n);
for(i=0;i<n;i++) for(i=0;i<n;i++)
{ {
scanf("%d %lf",&s[i].id, &s[i].cgpa); fread(&s[i],sizeof(s[i]),1,fp);
} }
for(i=0;i<n;i++) //Reading all at once without any loop
{ //fread(&s[0],sizeof(s[0]),n,fp);
fwrite(&s[i],sizeof(s[i]),1,fp);
} for(i=0;i<n;i++)
//Writing all at once without any loop {
//fwrite(&s[0],sizeof(s[0]),n,fp); printf("%d %lf\n",s[i].id,s[i].cgpa);
} }
fclose(fp);
return 0;
}
freopen
FILE *freopen(const char *filename, const char *mode, FILE
*stream)
• associates a new filename with the given open stream and at the
same time closes the old file in the stream.
For example,
freopen(“input.txt","r",stdin);
stdin was the default stream originally associated with console
(console is considered as a file). After the freopen function call,
stdin has been used to open a new stream to file “input.txt”.
So, scanf will read its input from “data_input.txt”.
Use of freopen
#include<stdio.h>
int main()
{
int i, j, n, arr[10];
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout
);
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
printf("%d ",arr[i]);
return 0;
}
File Operations
• File handle : FILE *
• Open a file : fopen
• Input/Output
• Character IO : fgetc, fputc
• String IO : fgets, fputs
• Formatted IO : fscanf, fprintf
• Raw IO : fread, fwrite
• Close a file : fclose
• Other operations :
• freopen, fseek, ftell
Sample Exercise
• Suppose, you are given two input files named “input1.txt” and “input2.txt”.
In both of the files, a single line contains an integer number. Both the files
contain same number of integers. There will be an output file named
“output.txt”. You have to take the 1st integer of input file 1 and the 1st integer
of input file 2, and output their summation in the 1 st line of the output file.
Then take the 2nd integers from both the inputs files and output their
summation in the second line of output file, and so on up to the end of the
input files. Write the full C code for this.
1st input file 2nd input file Correspondin
sample data sample data g output file
sample data
2 100 102
4 4 8
-2 98 96
4 -100 -96
8 -8 0
Solution
#include<stdio.h> while(fscanf(fp1,"%d",&i)!=EOF &&
fscanf(fp2,"%d",&j)!=EOF)
int main(int argc,char *argv[]) {
{ k=i+j;
FILE *fp1, *fp2, *fp3; fprintf(fp3,"%d\n",k);
int i,j,k; }
fp1 =
fopen("data_input1.txt","r"); fclose(fp1);
if(fp1==NULL) fclose(fp2);
{ fclose(fp3);
printf("Cannot open file\n");
exit(1); return 0;
} }
fp2 =
fopen("data_input2.txt","r");
if(fp2==NULL)
{
printf("Cannot open file\n");
exit(1);
}
fp3 =
fopen("data_output.txt","w");
if(fp3==NULL)
{
printf("Cannot open file\n");
exit(1);
}
Acknowledgement
This slide has been prepared by taking help from numerous resources. The notable contributors
are listed below.
1. The slides have been compiled and curated by Khaled Mahmud Shahriar, Assistant
Professor, CSE, BUET for CSE287 offered to the Department of MME.
2. Content and organization of many pages have been taken from the lecture slides and codes
of the course CSE110 offered to the Department of EEE that were -
i. primarily created by Johra Muhammad Moosa, Assistant Professor (on leave), CSE,
BUET and
ii. later modified by Madhusudan Basak, Assistant Professor, CSE, BUET
3. Most of the wonderful coding examples have been taken from the course CSE281 offered
to the Department of BME instructed by Rifat Shahriyar, Professor, CSE, BUET (course link
).
4. search and all the sites that it made available in response to the course related
queries. Some of the sites are: https://geeksforgeeks.org/, https://www.tutorialspoint.com,
https://www.w3schools.com and the list goes on …
Thank You ☺