File Input/Output
File Input/Output
FILE INPUT/OUTPUT
fclose(fp);
return 0;
}
Command-Line Arguments
The main function may have parameters named argc and
argv, which allow access to the command line when the
program is executed:
int main(int argc, char *argv[])
{
Command-Line Arguments
The following program (exist.c) obtains a file name from the
command line and checks whether the file can be opened:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
if (argc != 2) {
printf("usage: exist filename\n");
exit(EXIT_FAILURE);
}
if ((fp = fopen(argv[1], "r")) == NULL)
printf("%s does not exist\n", argv[1]);
else {
printf("%s exists\n", argv[1]);
fclose(fp);
}
return 0;}
Text I/O
The standard I/O library provides a number of functions
and macros for reading character data. Here is a partial list:
getc(fp) Reads a character from fp (macro)
getchar() Reads a character from stdin (macro)
fgetc(fp) Similar to getc, but is a function
scanf(format, ...) Reads formatted input from stdin
fscanf(fp, format, ...) Reads formatted input from fp
gets(s) Reads characters from stdin up to next
newline
fgets(s, n, fp) Reads at most n 1 characters from fp
Text I/O
Here is a partial list of output functions:
putc(c, fp) Writes the character c to fp (macro)
putchar(c) Writes the character c to stdout (macro)
fputc(c, fp) Similar to putc, but is a function
printf(format, ...) Writes formatted output to stdout
fprintf(fp, format, ...) Writes formatted output to fp
puts(s) Writes the string s to stdout
fputs(s, fp) Writes the string s to fp
Text I/O
Watch out for small differences between similar functions:
puts always adds a new-line character to the end of its
output; fputs doesnt.
fgets always includes a new-line character at the end of its
input string; gets doesnt.
All output functions return a value:
putc, putchar, and fputc return the character written.
printf and fprintf return the number of bytes written.
puts and fputs return the last character written.
All seven functions return EOF (a macro defined in
<stdio.h>) if an error occurs during output.
Detecting End-of-File
When getc, getchar, or fgetc detects that the end of the
input file has been reached or that an input error has
occurred, it returns EOF. Note: All three return an integer,
not a character.
The feof function can confirm that end-of-file was actually
reached:
int feof(FILE *stream); /* returns nonzero if eof */
Detecting End-of-File
The value returned by scanf and fscanf indicates the
actual number of input items that were read. If end-of-file
occurred before even one item could be read, the return
value is EOF.
gets and fgets return a pointer to the string read; on error
or end-of-file, they return NULL.
Binary Streams
There are two kinds of streams:
A text stream consists of lines of characters terminated by
the newline character.
A binary stream consists of a sequence of bytes
(characters).
Under Windows, there are two differences between text
streams and binary streams:
When a new-line character is written to a text stream, it is
expanded into a carriage- return/line-feed pair. The reverse
translation takes place during input.
A control-Z character (\x1a) in an input file is assumed to
mark the end of the file.
Binary Streams
Under UNIX, there is no difference between a text stream
and a binary stream.
Whether a file is treated as a text file or a binary file
depends on the mode that was specified when it was
opened. The modes listed previously are used for text files.
When a binary file is opened, the mode string should
include the letter b:
"rb" Open for reading
"wb" Open for writing (file need not exist)
"ab" Open for appending (file need not exist)
"rb+" Open for reading and writing, starting at beginning
"wb+" Open for reading and writing (truncate if file exists)
"ab+" Open for reading and writing (append if file exists)
Binary I/O
The fwrite function writes a block of binary data:
size_t fwrite(const void *ptr, size_t size, size_t
nmemb, FILE *stream);
Binary I/O
Numeric data written using printf is converted to character
form. Numeric data written using fwrite is left in binary form.
Advantages of using fwrite:
Less disk space is required.
Writing and reading takes less time.
Disadvantages:
Data cant be read easily by humans.
Data isnt portable between different types of computers.
Although fread and fwrite are normally applied to binary
streams, they can be used with text streams as well.
Conversely, the text I/O functions can be used with binary
streams. In both cases, however, watch out for unexpected
results.
Random Access
The fseek, ftell, and rewind functions support random
access within files. Random access is most often used with
binary files. These functions can also be used with text
files, but some restrictions apply.
fseek allows repositioning within a file.
int fseek(FILE *stream, long int offset, int
whence);
Random Access
Examples of fseek:
fseek(fp, 0, SEEK_SET); /* move to beginning of file
*/
fseek(fp, 0, SEEK_END); /* move to end of file */
fseek(fp, -10, SEEK_CUR); /* move back 10 bytes */