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

Lecture of useable

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

Lecture of useable

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

File

• A file is a place on the disk where a group of related data is stored.


• Data can be stored in a file and read whenever necessary, without
destroying it.
• There are two ways to manage a file: low level I/O (system calls) and high
level I/O (I/O library functions).
• Necessity of using files:
 When a program is terminated or the computer is turned off, the entire data is lost.
Storing in a file will preserve data even if the program terminates.
 It takes a lot of time to enter a large amount of data. If there is a file containing all the
data, it is easier to access the contents of the file.
 Data can easily be moved from one computer to another without any changes.
• There are two types of files: text file (.txt) and binary file (.bin).
File (contd.)
• File operations:
 Creating a file
 Opening a file
 Writing data to a file
 Reading data from a file
 Closing a file
 Creating a file: To use a file, the first job is to create a file using a pointer of type FILE. For example,
FILE *fp; Here, FILE is a structure defined in the I/O library.
 Opening a file: The next job is to open a file. The general format is
*fp=fopen(“file name”, “mode”); Here, fopen is, a library function, used to open a
file with a file name and a mode.
mode can be one of the following three types:
r – open a file for reading only
w – open a file for writing only
a – open a file for appending data to it
For example: *fp=fopen(“E:\\Nitu\\C Programs\\result.txt”, “w”); If the file result.txt does not exist in the location (E:\\
Nitu\\C Programs), then fopen creates a new file result.txt in write mode.
File (contd.)

Writing data to a file: There are different I/O functions to write data
into a file if it is open in write mode.
putc function is used to write a single character to a file. For example,
putc(p,fp); Here, p is a character type variable and fp is the file pointer.
putw function is used to write an integer value to a file. For example,
putw(x,fp); Here, x is an integer type variable and fp is the file pointer.
fprintf function is used to write mixed data simultaneously. For example,
fprintf(fp, “%c %f %d”, p, 10.50,x);
File (contd.)
Reading data from a file:
getc(): p=getc(fp);
This statement reads a character from a file pointed by fp and assigns
the value to the character type variable p.
The file pointer moves by one character position for every getc/putc operation.
This function returns an end of file marker EOF, once the end of file is reached.
So, reading a file should be stopped when the EOF is encountered.
getw(): x=getw(fp); for reading an integer value from a file.
fscanf() function is used to read mixed data simultaneously.
fscanf(fp, “%f %d”, &a, &b); When the end of file is reached it returns
the value EOF.
File (contd.)

Closing a file: A file should be closed after using it (reading/writing).


fclose function is used to close a file. For example,
fclose(fp); This statement closes the file pointed by fp.
This ensures that all outstanding information associated with that file is
flushed out from the buffers and all links to the file are broken.
However, all files are closed automatically whenever a program terminates.
It is a good programming habit to close a file immediately once you are done.
File (contd.)
• Example program:
main() fprintf(fp,"%d",number); // writing to the file
{ fclose(fp); // closes the file
int number;
FILE *fp; //file pointer fp = fopen("E:\\Nitu\\C Programs\\write.txt","r");
// opens a file in read mode
fp = fopen("E:\\Nitu\\C Programs\\write.txt","w");
// opens a file in write mode fscanf(fp,"%d ", &number); // reading from the file

if(fp == NULL) { // file not found printf("The value is=%d ", number);
printf("Error!"); fclose(fp); // closes the file
exit(1); }
}

printf("Enter number: ");


scanf("%d",&number); //reading from terminal
File (contd.)
• Another example program:
main()
{ char ch;
FILE *fp;
fp = fopen("E:\\Nitu\\C Programs\\test.txt", "r");

if(fp == NULL) { // file not found


printf("Error! File not found");
exit(1);
}
ch = getc(fp); // reads a character from the file test.txt
while (ch != EOF) { // checks for EOF character
putc(ch, stdout); // writes a character to the terminal
ch = getc(fp);
}
fclose(fp); // closes the file
}

You might also like