0% found this document useful (0 votes)
38 views20 pages

File Input/Output

The document discusses file input/output (I/O) in C, including: 1) File pointers represent open files and are used to pass file streams to I/O functions. Standard streams like stdin, stdout, and stderr are predefined. 2) The fopen, fclose, fread, fwrite functions are used to open, close, read from, and write to files. Text and binary modes determine how data is handled. 3) Command line arguments allow accessing filenames and arguments when a program runs. Random access functions like fseek allow precise positioning within files.

Uploaded by

Maria Qadeer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views20 pages

File Input/Output

The document discusses file input/output (I/O) in C, including: 1) File pointers represent open files and are used to pass file streams to I/O functions. Standard streams like stdin, stdout, and stderr are predefined. 2) The fopen, fclose, fread, fwrite functions are used to open, close, read from, and write to files. Text and binary modes determine how data is handled. 3) Command line arguments allow accessing filenames and arguments when a program runs. Random access functions like fseek allow precise positioning within files.

Uploaded by

Maria Qadeer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

22.

FILE INPUT/OUTPUT

File Pointers and Streams


Declarations of functions that perform file I/O appear in
<stdio.h>. Each function requires a file pointer as a
parameter.
A file pointer is a pointer to a FILE structure (FILE is
defined in <stdio.h>). A program may declare as many file
pointers as needed:
FILE *fp1, *fp2;

A file pointer represents a stream, which may be a file or


in generalany source of input or output.

File Pointers and Streams


Three streams are standard:
stdin Standard input
stdout Standard output
stderr Standard error
These streams need not be opened or closed.
Standard input and output can be redirected in both UNIX
and Windows:
prog <data >result
UNIX also allows redirection of the standard error stream.
Only certain versions of Windows (NT and 2000) allow this.
Redirection of standard error is done by using 2> instead of
>.

Opening and Closing Files


Files can be opened by calling fopen:
FILE *fopen(const char *filename, const char *mode);

mode can be one of the following:


"r" Open for reading
"w" Open for writing (file need not exist)
"a" Open for appending (file need not exist)
"r+" Open for reading and writing, starting at
beginning
"w+" Open for reading and writing (truncate if file
exists)
"a+" Open for reading and writing (append if file
exists)
If the file cannot be opened, fopen returns NULL.

Files can be closed by calling fclose:


int fclose(FILE *stream);

Opening and Closing Files


Example:
#include <stdio.h>
#include <stdlib.h>
#define INPUT_FILE "example.dat"
int main(void)
{
FILE *fp;
fp = fopen(INPUT_FILE, "r");
if (fp == NULL) {
printf("Can't open %s\n", INPUT_FILE);
exit(EXIT_FAILURE);
}

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[])
{

argc is a count of the number of command line arguments


(including the name of the program itself).
argv is an array of pointers to the command line
arguments. argv[0] contains a pointer to the name of the
program. argv[1] through argv[argc-1] point to the
remaining command line arguments.

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 */

The ferror function can confirm that an error occurred:


int ferror(FILE *stream); /* returns nonzero if
error */

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);

fwrite writes nmemb elements of size size to stream. fwrite


returns the actual number of elements written.
The fread function reads a block of binary data:
size_t fread(void *ptr, size_t size, size_t nmemb,
FILE *stream);

fread reads up to nmemb elements of size size from


stream, storing them at the address specified by ptr. fread
returns the actual number of elements read.

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);

The new file position is determined by offset and whence.


offset is a (possibly negative) byte count relative to the
position specified by whence. whence must have one of the
following values:
SEEK_SET Beginning of file
SEEK_CUR Current file position
SEEK_END End of file

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 */

ftell returns the current file position:


long int ftell(FILE *stream);

This may be saved and later supplied to a call of fseek:


long int file_pos;
file_pos = ftell(fp);

fseek(fp, file_pos, SEEK_SET);


/* return to previous position */

The call rewind(fp) is equivalent to fseek(fp, 0,


SEEK_SET).

Example Program: Modifying a


Database
#include <stdio.h>
#include <stdlib.h>
#define NAME_LEN 25
#define MAX_PARTS 100
#define DATA_FILE "invent.dat"
struct part {
int part_no;
char part_name[NAME_LEN+1];
int on_hand;
} inventory[MAX_PARTS];
int num_parts;

Example Program: Modifying a


Database
int main(void)
{
FILE *fp = fopen(DATA_FILE, "rb+");
if (fp == NULL) {
fprintf(stderr, "Can't open %s\n", DATA_FILE);
exit(EXIT_FAILURE);
}
num_parts = fread(inventory, sizeof(struct part),
MAX_PARTS, fp);
/* modify inventory */
rewind(fp);
fwrite(inventory, sizeof(struct part), num_parts,
fp);
fclose(fp);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy