Image Loading... Image Loading...
Click Here to Visit Our Sponsor

Home
News
Cartoon

Books
Reviews

SimTel
Compress
Database
Delphi
Desktop
Diskutl
Fileutl
Graphics
Inet
Mmedia
Prog
Scrsave
Util

C/C++
CodeBase!
Libraries
Tutorials
WebLinks

Pascal
Libraries
WebLinks



Home / C Tutorials
The C language
Text Files in C
by Marshall Brain
Thursday, May 27, 1999

Published with kind permission of DevCentral
Copyright © 1998 Interface Technologies, Inc. All Rights Reserved.


Text files in C are straightforward and easy to understand. They work the same way as Pascal text files. All text file functions and types in C come from the stdio library.

When you need text I/O in a C program, and you need only one source for input information and one sink for output information, you can rely on stdin (standard in) and stdout (standard out). You can then use input and output redirection at the command line to move different information streams through the program. There are six different I/O commands in <stdio.h> that you can use with stdin and stdout:

printf prints formatted output to stdout
scanf reads formatted input from stdin
puts prints a string to stdout
gets reads a string from stdin
putc prints a character to stdout
getc, getchar reads a character from stdin

The advantage of stdin and stdout is that they are easy to use. Likewise, the ability to redirect I/O is very powerful. For example, maybe you want to create a program that reads from stdin and counts the number of characters:


 #include <stdio.h>  
 #include <string.h> 
 
 void main()  
 {    
     char s[1000];    
     int count=0;     
      while (gets(s))      
         count += strlen(s);    
     printf("%d\n",count);  
 } 

Enter this code and run it. It waits for input from stdin, so type a few lines. When you are done, press CTRL-D to signal end-of-file (eof). gets reads a line until it detects eof, then returns a 0 so that the while loop ends. When you press CTRL-D, you see a count of the number of characters in stdout (the screen).

Now, suppose you want to count the characters in a file. If you compiled the program to a.out, you can type the following:


 a.out < filename 

Instead of accepting input from the keyboard, the contents of the file named filename will be used instead. You can achieve the same result using pipes:


 cat < filename | a.out 

You can also redirect the output to a file:


 a.out < filename > out 

This command places the character count produced by the program in a text file named out.

Sometimes, you need to use a text file directly. For example, you might need to open a specific file name and read from or write to it. You might want to manage several streams of input or output or create a program like a text editor that can save and recall data or configuration files on command.

fopenopens a text file
fclosecloses a text file
feofdetects end-of-file marker in a file
fprintfprints formatted output to a file
fscanfreads formatted input from a file
fputsprints a string to a file
fgetsreads a string from a file
fputcprints a character to a file
fgetcreads a character from a file

You use fopen like reset and rewrite in Pascal. It opens a file for a specified mode (the three most common are r, w, and a, for read, write, and append). It then returns a file pointer that you use to access the file. For example, suppose you want to open a file and write the numbers 1 to 10 in it. You could use the following code:


 #include <stdio.h> 
 #define MAX 10

 void main()  
 {    
     FILE *f;    
     int x; 
      f=fopen(" out","w");    
     for(x = 1; x <= MAX; x++)      
         fprintf(f,"%d\n",x); 
     fclose(f); 
 }

The fopen statement here opens a file named out with the w mode. This is a destructive write mode, which means that if out does not exist it is created, but if it does exist it is destroyed and a new file is created in its place. The fopen command returns a pointer to the file, which is stored in the variable f. This variable is used to refer to the file. If the file cannot be opened for some reason, f will contain NULL.

The fprintf statement should look very familiar: It is just like printf but uses the file pointer as its first parameter. The fclose statement closes the file when you are done.

To read a file, open it with r mode. In general, it is not a good idea to use fscanf for reading: Unless the file is perfectly formatted, fscanf will not handle it correctly. Instead, use fgets to read in each line and then parse out the pieces you need.

The following code demonstrates the process of reading a file and dumping its contents to the screen:


 #include <stdio.h> 
  
 void main()  
 {    
     FILE *f;    
     char s[1000]; 
     
     f = fopen("infile","r");    
     while (fgets(s, 1000, f)!=NULL)      
         printf("%s",s);    
     fclose(f);  
 } 

The fgets statement returns a NULL value at the end-of-file marker. It reads a line (up to 1,000 characters in this case) and then prints it to stdout. Notice that the printf statement does not include \n in the format string, because fgets adds \n to the end of each line it reads. Thus, you can tell if a line is not complete in the event that it overflows the maximum line length specified in the second parameter to fgets.

C Errors to Avoid:

  • Do not accidentally type close instead of fclose . The close function exists, so the compiler accepts it. It will even appear to work if the program only opens or closes a few files. However, if the program opens and closes a file in a loop, it will eventually run out of available file handles and/or memory space and crash, because close is not closing the files correctly.


This Article
Introduction
A simple program
Branching/looping
Arrays
C Details
Functions
Libraries/makefiles
Text files
Pointers
Parameters
Dynamic structures
Pointers and arrays
Strings
Operator precedence
The command line
Binary files

NetLinks
DevCentral
DJGPP C Compiler
GNU C++ Compiler
Tendra C Compiler

Next:  Introduction to Pointers in C

   
Image Loading...