More on Files


Lecture 22


A file-copy Program

We have already used the function fgetc( ) which reads characters from a file. Its counterpart is a function called fputc( ) which writes characters to a file. As a practical use of these character I/O functions we can copy the contents of one file into another, as demonstrated in the following program. This program takes the contents of a file and copies them into another file, character by character.

fputc( ) writes to the file. Which file? The file signified by ft. The writing process continues till all characters from the source file have been written to the target file, following which the while loop terminates.
Note that our sample file-copy program is capable of copying only text files. To copy files with extension .EXE or .COM, we need to open the files in binary mode, a topic that would be dealt with in sufficient detail in a later section.

File Opening Modes

In our first program on disk I/O we have opened the file in read (“r”) mode. However, “r” is but one of the several modes in which we can open a file. Following is a list of all possible modes in which a file can be opened. The tasks performed by fopen( ) when a file is opened in each of these modes are also mentioned.
"r"
Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer which points to the first character in it. If the file cannot be opened fopen( ) returns NULL.
Operations possible – reading from the file.
"w"
Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file.
Operations possible – writing to the file.
"a"
Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer that points to the last character in it. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file.
Operations possible - adding new contents at the end of file.
"r+"
Searches file. If is opened successfully fopen( ) loads it into memory and sets up a pointer which points to the first character in it. Returns NULL, if unable to open the file.

Operations possible - reading existing contents, writing new contents, modifying existing contents of the file.

"w+"
Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist a new file is created. Returns NULL, if unable to open file.
Operations possible - writing new contents, reading them back and modifying existing contents of the file.
"a+"
Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer which points to the first character in it. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file.
Operations possible - reading existing contents, appending new contents to end of file. Cannot modify existing contents.

String in Files

For many purposes, character I/O is just what is needed. However, in some situations the usage of functions that read or write entire strings might turn out to be more efficient.
Reading or writing strings of characters from and to files is as easy as reading and writing individual characters. Here is a program that writes strings to a file using the function fputs( ).
Note that each string is terminated by hitting enter. To terminate the execution of the program, hit enter at the beginning of a line. This creates a string of zero length, which the program recognizes as the signal to close the file and exit.
We have set up a character array to receive the string; the fputs( ) function then writes the contents of the array to the disk. Since fputs( ) does not automatically add a newline character to the end of the string, we must do this explicitly to make it easier to read the string back from the file.
Here is a program that reads strings from a disk file.
The function fgets( ) takes three arguments. The first is the address where the string is stored, and the second is the maximum length of the string. This argument prevents fgets( ) from reading in too long a string and overflowing the array. The third argument, as usual, is the pointer to the structure FILE. When all the lines from the file have been read, we attempt to read one more line, in which case fgets( ) returns a NULL.






2 comments:

Unknown said...

This is great blog! Keep it up. aircon chemical wash price singapore

Fahad said...

Hi everyone!

If you're looking for a comprehensive guide to Singapore, you won't want to miss out on Saturday Reviews. This online blog is dedicated to providing you with all the information you need to discover Singapore like a local. From its rich culture to its dynamic lifestyle, Saturday Reviews covers it all.

Whether you're planning a trip to Singapore, or just want to learn more about the city-state, Saturday Reviews has got you covered. The blog is updated regularly with articles that showcase the best of what Singapore has to offer, from its famous hawker centers to its iconic landmarks.

So, if you want to know more about this fascinating city, be sure to check out Saturday Reviews. Trust me, you won't regret it!

Best regards,
A fan of .Saturday Reviews

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by CelebrityDisk | Written by Alamin - link | Grants For Single Moms