0% found this document useful (0 votes)
84 views

MA 511: Computer Programming: Partha Sarathi Mandal

This document summarizes a lecture on file input and output in the MA 511: Computer Programming course. It discusses file buffers, the FILE data structure used to represent open files, and examples of opening files for reading and writing using fopen(), fprintf(), fscanf(), and fclose(). It also provides examples of writing integers to a data file and reading from a data file. Finally, it lists three programming assignments involving file I/O and array manipulation.

Uploaded by

Naveen Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

MA 511: Computer Programming: Partha Sarathi Mandal

This document summarizes a lecture on file input and output in the MA 511: Computer Programming course. It discusses file buffers, the FILE data structure used to represent open files, and examples of opening files for reading and writing using fopen(), fprintf(), fscanf(), and fclose(). It also provides examples of writing integers to a data file and reading from a data file. Finally, it lists three programming assignments involving file I/O and array manipulation.

Uploaded by

Naveen Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

MA 511: Computer Programming

Lecture 14: File read and write


http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html

Partha Sarathi Mandal


[email protected]
Dept. of Mathematics, IIT Guwahati
Semester 1, 2010-2011
I/O
• Buffer
– A segment of memory used to hold data while it is being processed.
– In a program, buffers are created to hold some amount of data from each of
the files that will be read or written.
• File
– A bunch of blocks of information written on your hard-drive
– OS knows the location and order of blocks and will present you with a
continuous view of file.
• FILE (as in File *)
– A data structure that holds file information
• What file
• Current position in file
• Permissions (read, write)
Data Files
Example:

#include <stdio.h>
main(){
FILE *fp;
int i;
fp = fopen("output.dat", "w");
if(fp==NULL)
printf("Error for opening a file\n");
for(i=65; i<90; i++)
fprintf(fp, "%c\n", i);
fclose(fp);
}
output.dat
A
B
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y

MA511: Computer Programming


Partha S Mandal, IITG
Data Files
Example: FILE: spl structure type that
establishes the buffer area
#include <stdio.h> fp : pointer variable pointing to the
beginning of the buffer area.
main(){ fp=fopen(file-name, file-type);
FILE *fp;
int i; “r” : open a existing file for reading
fp = fopen("output.dat", "w"); “w”: opening a new file for writing,
if(fp==NULL) overwrite if file exist.
printf("Error for opening a file\n"); “a” : opening an existing file for
for(i=65; i<90; i++) appending, create if not exist.
“r+”: opening an existing file for read and
fprintf(fp, "%c\n", i); write.
fclose(fp); “w+”: opening a new file for both reading
} & writing, overwrite if file exist.
“a+”: opening a new file for reading and
appending, create if not exist.
Reading a Data File
Example:

#include <stdio.h>
main(){
FILE *fp1;
char ch;
fp1 = fopen("output.dat", "r");
if(fp1==NULL)
printf("Error for opening a file\n");
else{

while(!feof(fp1)){
fscanf(fp1, "%c", &ch);
printf("%c",ch);
}
}
fclose(fp1);
}
Assignment
• Write a C program for inserting an integer before
a given target value in a given array of integer.
• Write a C program for deleting a given value in a
given array of integer.
• Write a C program for (i) writing integers 1-100 to
a file (fwrite.dat) (ii) read data from the file
fwrite.dat, identify numbers which are perfect
square and write over a new file (newfile.dat).

You might also like