File Management
File Management
• Concept of files
Files
fp = fopen(“filename”, “mode”);
/*opens file with name filename , assigns identifier to fp */
• fp
– contains all information about file
– Communication link between system and program
• Mode can be
– r open file for reading only
– w open file for writing only
– a open file for appending (adding) data
Different modes
• Writing mode
– if file already exists then contents are deleted,
– else new file with specified name created
• Appending mode
– if file already exists then file opened with contents safe
– else new file created
• Reading mode
– if file already exists then opened with contents safe
– else error occurs.
• Ensures
– All outstanding information associated with file flushed out from
buffers
– All links to file broken
– Accidental misuse of file prevented
• If want to change mode of file, then first close and open again
Closing a file
Syntax: fclose(file_pointer);
Example:
FILE *fpr;
fpr = fopen("C:\\myfiles\\newfile.txt", "r");
if (fpr == NULL)
{
puts("Error while opening file");
exit();
}
Input / Output operations on files
• C provides several different functions for reading/writing
• fgetc():This function reads the character from current pointer’s position and
upon successful read moves the pointer to next character in the file. Once
the pointers reaches to the end of the file, this function returns EOF (End
of File). We have used EOF in our program to determine the end of the
file.
• syntax: putc(c,fp1);
– c : a character variable
– fp1 : pointer to file opened with mode w
• fputc() – is used to write a single character at a time to a given file. It writes
the given character at the position denoted by the file pointer and then
advances the file pointer.
• This function returns the character that is written in case of successful write
operation else in case of error EOF is returned.
• syntax: c = getc(fp2);
– c : a character variable
– fp2 : pointer to file opened with mode r
file pointer moves by one character position after every getc() and
putc()
getc() returns end-of-file marker EOF when file end reached
fclose(f1);
} /*end main */
fscanf() and fprintf()
• similar to scanf() and printf()
• in addition provide file-pointer
• fscanf():function is used to read formatted data from a file.
In a C program, we use fscanf() as below.
• fscanf (fp, “%d”, &age);
• Where, fp is file pointer to the data type “FILE”.
• age – Integer variable
• This is for example only. We can use any specifiers with any data
type as we use in normal scanf() function.
• Example:
fscanf(f2, “%d %f %s ”, &i, &f,str);
Here
i= integer
f = float
str= character or string
• fscanf returns EOF when end-of-file reached
fprintf() :
Declaration: int fprintf(FILE *fp, const char *format, …)
fprintf() function is used to write formatted data into a file. In a C
program
We use fprinf() as below.
fprintf (fp, “%s %d”, “var1”, var2);
FILE *inf;
struct Student inp;
inf = fopen ("c1.txt", "r");
if (inf == NULL) {
fprintf(stderr, "\nError to open the file\n");
exit (1);
}
while(fread(&inp, sizeof(struct Student), 1, inf))
printf ("roll_no = %d name = %s\n", inp.roll_no, inp.name);
fclose (inf);
}
Errors that occur during I/O
fp = fopen(“input.dat”, “r”);
if (fp == NULL)
printf(“File could not be opened \n ”);
Random access to files
$ cc args.c -o args.out
$ ./args.out 2 join leave 6
6
leave
join
2
./args.out
$