C supports string based I/O operations using fgets (for input) and fputs (for output) functions. The behaviours of these two functions are quite similar to the gets and puts functions, discussed earlier. The differences can be seen from the following declaration:
fgets(<string>, <length>, <file pointer>); fputs(<string>, <file pointer>);
The function fgets reads characters including embedded spaces from the file pointed to by the file pointer and stores them in the string, appending a null character to terminate the string. The maximum number of characters read would be equal to length - 1, since one character position is kept aside for null terminator character, viz. \0. If this function encounters a newline character \n or the end of the file EOF before it has read the specified number of characters, viz. length - 1, it returns only the characters read up to that point including the new line character. Thereafter, fgets automatically puts a null terminator at end of the read string.
Note that you can also use the function fscanf to read strings from a file. However, fscanf has a limitation that it stops reading after encountering the first blank space character.
Function fgets returns NULL (a symbolic constant defined in stdio.h with a 0 value), if read attempt fails (may be due to end of file being encountered).
The function fputs writes the string to the file pointed to by the file pointer. It returns a non-negative value on success, otherwise EOF is returned in case of any error. Note that you can also use function fprintf to write a string into a file.
To understand the usage of fgets and fputs, we will re-write the file copy program that we wrote earlier. However, this time we will use fgets and fputs instead of fgetc and fputc.
/* Program to create a copy of file1.txt as file2.txt -------------------------------------------------- */ #include <stdio.h> main() { FILE *fp1, *fp2; char line[81]; fp1 = fopen("file1.txt", "r"); if(fp1 == NULL) { puts("Error!! Unable to open file1.txt for reading."); return; } fp2 = fopen("file2.txt", "w"); if(fp2 == NULL) { puts("Error!! Unable to create file2.txt."); return; } while(fgets(line,81,fp1) != NULL) fputs(line,fp2); fclose(fp1); fclose(fp2); }
Compare the above program with the one written using fgetc and fputc in the previous article.
You can use the following code to view the contents of the newly created file file2.txt.
FILE *fp; char line[81]; fp = fopen("file2.txt", "r"); if(fp == NULL) { puts("Error!! Unable to open file2.txt for reading."); return; } while(fgets(line,81,fp) != NULL) printf("%s\n",line); fclose(fp);
1. Identify the errors, if any, in the following program:
#include<stdio.h> main() { FILE *fp; char ch; while((ch = getchar()) != EOF) fputc(ch,fp); fclose(fp); }
2. Identify the errors, if any, in the following program:
#include<stdio.h> main() { FILE *fp1, *fp2; char ch; fp1 = fopen("in.dat", "w"); fp2 = fopen("out.dat", "r"); while((ch = fgetc(fp1)) != EOF) fputc(ch,fp2); fclose(fp1); }
File Handling in C - Part 1 of 7
File Handling in C - Part 2 of 7
File Handling in C - Part 4 of 7
File Handling in C - Part 5 of 7
How to move your Email accounts from one hosting provider to another without losing any mails?
How to resolve the issue of receiving same email message multiple times when using Outlook?
Self Referential Data Structure in C - create a singly linked list
Mosquito Demystified - interesting facts about mosquitoes
Elements of the C Language - Identifiers, Keywords, Data types and Data objects
How to pass Structure as a parameter to a function in C?
Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of premier engineering colleges & management institutes, and IT professionals.
Rajeev has founded Computer Solutions & Web Services Worldwide. He has hands-on experience of building variety of websites and business applications, that include - SaaS based erp & e-commerce systems, and cloud deployed operations management software for health-care, manufacturing and other industries.