0% found this document useful (0 votes)
0 views

03_C_Standard_Library_Text_File_IO

The document outlines various C Standard Library functions for file input and output, including getchar, putchar, getc, putc, gets_s, puts, fopen, fgets, fputs, fscanf, and fprintf. It explains how these functions interact with standard input and output streams, as well as how to handle file redirection and formatted data. Additionally, it provides links to Linux manual pages for further reference and examples of programs demonstrating these functions.

Uploaded by

samsauder0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

03_C_Standard_Library_Text_File_IO

The document outlines various C Standard Library functions for file input and output, including getchar, putchar, getc, putc, gets_s, puts, fopen, fgets, fputs, fscanf, and fprintf. It explains how these functions interact with standard input and output streams, as well as how to handle file redirection and formatted data. Additionally, it provides links to Linux manual pages for further reference and examples of programs demonstrating these functions.

Uploaded by

samsauder0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Text File Input and Output – C Standard Library

1. The following program uses the C Standard Library functions getchar and putchar to
perform file input and output on the standard input device and standard output
device. The standard input device is typically referred to as stdin and the standard
output device is typically referred to as stdout. The standard input device (stdin)
defaults to the terminal’s keyboard and the standard output device (stdout) defaults to
the terminal’s monitor.

Standard Input-Output Streams

/usr/include/stdio.h

extern struct _IO_FILE *stdin; /* Standard input stream. */


extern struct _IO_FILE *stdout; /* Standard output stream. */
extern struct _IO_FILE *stderr; /* Standard error output stream. */

Linux Manual Pages


https://man7.org/linux/man-pages/man3/getchar.3p.html
https://man7.org/linux/man-pages/man3/putchar.3p.html

To trace system calls on Linux, enter strace ./Program

Program
C:\Code\00_C_Code\12_Text_File_IO\01_A_Standard_IO_getchar_putchar

2. The following program uses the C Standard Library functions getc and putc to perform
file input and output on the standard input device and standard output device. The C
Standard Library functions getc and putc both receive a pointer to a FILE (FILE*). FILE is
defined in the C Standard Library’s stdio.h file.

NOT: A FILE* is similar to a Unix file descriptor, but a FILE* (like stdin or stdout) is a
pointer to some object structure, whereas a file descriptor is just an integer. For
example, 0, 1, and 2 are the file descriptor versions of stdin, stdout, and stderr,
respectively.
Linux Manual Pages
https://man7.org/linux/man-pages/man3/getc.3p.html
https://www.man7.org/linux/man-pages/man3/putc.3p.html
https://cs61.seas.harvard.edu/site/ref/file-descriptors/#gsc.tab=0
https://en.wikipedia.org/wiki/File_descriptor

To trace system calls on Linux, enter strace ./Program

Program
C:\Code\00_C_Code\12_Text_File_IO\01_B_Standard_IO_getc_putc
3. The following program uses the C Standard Library functions gets_s and puts to
perform file input and output on the standard input device and standard output
device. The function gets_s receives a pointer to an array of characters (char*) that
represents a string buffer, and an integer that represents the size of the string buffer.
The function puts receives a pointer to an array of characters (char*) that represents a
null-terminated string.
Linux Manual Pages
https://man7.org/linux/man-pages/man3/gets.3.html
https://docs.oracle.com/cd/E88353_01/html/E37843/gets-s-3c.html
https://man.freebsd.org/cgi/man.cgi?query=gets_s
https://man7.org/linux/man-pages/man3/puts.3.html

To trace system calls on Linux, enter strace ./Program

Program
C:\Code\00_C_Code\12_Text_File_IO\01_C_Standard_IO_gets_s_puts

4. The following program uses the C Standard Library functions getc and putc to perform
file input and output on the standard input device and standard output device. The
standard input device, stdin, is redirected from the terminal’s keyboard to a regular
ASCII text file. The standard output device, stdout, is redirected from the terminal’s
monitor to a regular ASCII text file. Data is read from a regular file associated with
stdin and written to a regular file associated with stdout. The file data is read from
standard-input using the file input redirection operator (<) and written to standard-
output using the file output redirection operator (>). This program must be executed
from the command-line
Linux Manual Pages
https://man7.org/linux/man-pages/man3/getc.3p.html
https://www.man7.org/linux/man-pages/man3/putc.3p.html
https://cs61.seas.harvard.edu/site/ref/file-descriptors/#gsc.tab=0
https://en.wikipedia.org/wiki/File_descriptor

To trace system calls on Linux, enter strace ./Program

Program
C:\Code\00_C_Code\12_Text_File_IO\01_D_Standard_IO_Redirection

5. The following program uses the C Standard Library functions fopen, getc and putc to
perform file input and output on regular text files. The C Standard Library fopen
function receives a string that represents a path and filename and returns a pointer to
a FILE (FILE*) that is associated with the opened file. FILE is defined in the C Standard
Library’s stdio.h file. The C Standard Library functions getc and putc both receive a
FILE*.

For text file input and output, the fopen function also receives as the second
parameter a file I/O mode string that determines the read, write and append
characteristics of the open file. The file I/O mode string represents one of the following
sequences:

 r Open text file for reading. The stream is positioned at the beginning of the
file.
 r+ Open for reading and writing. The stream is positioned at the beginning of
the file.
 w Truncate file to zero length or create text file for writing. The stream is
positioned at the beginning of the file.
 w+ Open for reading and writing. The file is created if it does not exist,
otherwise it is truncated. The stream is positioned at the beginning of the file.
 a Open for appending (writing at end of file). The file is created if it does not
exist. The stream is positioned at the end of the file.
 a+ Open for reading and appending (writing at end of file). The file is created
if it does not exist. Output is always appended to the end of the file. POSIX is
silent on what the initial read position is when using this mode. For glibc, the
initial file position for reading is at the beginning of the file, but for
Android/BSD/MacOS, the initial file position for reading is at the end of the file.

Linux Manual Pages


https://man7.org/linux/man-pages/man3/fopen.3.html
https://man7.org/linux/man-pages/man3/fclose.3.html
https://man7.org/linux/man-pages/man3/getc.3p.html
https://man7.org/linux/man-pages/man3/putc.3p.html
https://man7.org/linux/man-pages/man1/strace.1.html

To trace system calls on Linux, enter strace ./Program

Program
C:\Code\00_C_Code\12_Text_File_IO\02_A_Copy_File_getc_putc

6. The following program uses the C Standard Library functions fopen, fgets and fputs to
perform file input and output on regular files. The function fgets receives a pointer to
an array of characters (char*) that represents a string buffer, the size of the string
buffer, and a FILE* that represents the opened file that’s used for input. The function
fputs receives a pointer to an array of characters (char*) that represents a string buffer
and a FILE* that represents the opened file that’s used for output. The string buffer is
null-terminated.
Linux Manual Pages
https://man7.org/linux/man-pages/man3/fopen.3.html
https://man7.org/linux/man-pages/man3/fclose.3.html
https://man7.org/linux/man-pages/man3/fgets.3p.html
https://man7.org/linux/man-pages/man3/fputs.3p.html
https://man7.org/linux/man-pages/man1/strace.1.html

To trace system calls on Linux, enter strace ./Program


Program
C:\Code\00_C_Code\12_Text_File_IO\02_B_Copy_File_fgets_fputs

7. The following program uses the C Standard Library functions fopen, fscanf and fprintf
to perform formatted file input and output on regular files. The function fscanf receives
a FILE* that represents the opened file that’s used for input, a string that represents a
format specifier, and a pointer to an array of characters (char*) that represents a string
buffer. The function fscanf reads up to the first space character and discards the space
character before writing the data to the buffer. The function fprintf receives a FILE*
that represents the opened file that’s used for output, a string that represents a format
specifier, and a pointer to an array of characters (char*) that represents a string buffer.
The string buffer is null-terminated.

NOTE: the size of the string buffer is not supplied as a parameter to fscanf which is a
security risk and can lead to buffer overflows if the input exceeds the size of the buffer.
This can cause undefined behavior, crashes, or security vulnerabilities.
Linux Manual Pages
https://man7.org/linux/man-pages/man3/fopen.3.html
https://man7.org/linux/man-pages/man3/fclose.3.html
https://man7.org/linux/man-pages/man3/fscanf.3p.html
https://man7.org/linux/man-pages/man3/fprintf.3p.html
https://man7.org/linux/man-pages/man1/strace.1.html

To trace system calls on Linux, enter strace ./Program

Program
C:\Code\00_C_Code\12_Text_File_IO\02_C_Copy_File_fscanf_fprintf

8. The following program uses the C Standard Library functions fopen, fscanf and fprintf
to perform formatted file input and output on regular files. The integer data written to
the output file using fprintf is converted to an ASCII string and then written to the
output file.
.
Linux Manual Pages
https://man7.org/linux/man-pages/man3/fopen.3.html
https://man7.org/linux/man-pages/man3/fclose.3.html
https://man7.org/linux/man-pages/man3/fscanf.3p.html
https://man7.org/linux/man-pages/man3/fprintf.3p.html
https://man7.org/linux/man-pages/man1/strace.1.html

To trace system calls on Linux, enter strace ./Program

Program
C:\Code\00_C_Code\12_Text_File_IO\03_A_Read_Write_Integer_fscanf_fprintf

9. The following program uses the C Standard Library functions fopen, fscanf and fprintf
to perform formatted file input and output on regular files. The data written to the
output file represents a three-field record. Each record field written to the output file
represents an ASCII string.
.
Linux Manual Pages
https://man7.org/linux/man-pages/man3/fopen.3.html
https://man7.org/linux/man-pages/man3/fclose.3.html
https://man7.org/linux/man-pages/man3/fscanf.3p.html
https://man7.org/linux/man-pages/man3/fprintf.3p.html
https://man7.org/linux/man-pages/man1/strace.1.html

To trace system calls on Linux, enter strace ./Program

Program
C:\Code\00_C_Code\12_Text_File_IO\03_B_Read_Write_Records_fscanf_fprintf

10. The following program uses the C Standard Library functions fopen, fscanf and fprintf
to perform formatted file input and output on regular files. The data written to the
output file represents a time stamp. Each record field written to the output file
represents an ASCII string.
.
Linux Manual Pages
https://man7.org/linux/man-pages/man3/fopen.3.html
https://man7.org/linux/man-pages/man3/fclose.3.html
https://man7.org/linux/man-pages/man3/fscanf.3p.html
https://man7.org/linux/man-pages/man3/fprintf.3p.html
https://man7.org/linux/man-pages/man1/strace.1.html
https://man7.org/linux/man-pages/man2/time.2.html
https://man7.org/linux/man-pages/man3/time_t.3type.html
https://man7.org/linux/man-pages/man3/ctime.3p.html

To trace system calls on Linux, enter strace ./Program

Program
C:\Code\00_C_Code\12_Text_File_IO\03_C_Write_Time_Stamp_fscanf_fprintf

11. The following program prompts the user for the input file name (source) and the
output file name (destination).

Linux Manual Pages


https://man7.org/linux/man-pages/man3/fopen.3.html
https://man7.org/linux/man-pages/man3/fclose.3.html
https://man7.org/linux/man-pages/man3/scanf.3.html
https://man7.org/linux/man-pages/man3/getc.3p.html
https://man7.org/linux/man-pages/man3/putc.3p.html
https://man7.org/linux/man-pages/man1/strace.1.html

To trace system calls on Linux, enter strace ./Program

Program
C:\Code\00_C_Code\12_Text_File_IO\04_Filename_Prompts
References
https://man7.org/linux/man-pages/man3/fopen.3.html

https://man7.org/linux/man-pages/man3/fclose.3.html

https://man7.org/linux/man-pages/man3/scanf.3.html

https://man7.org/linux/man-pages/man3/getc.3p.html

https://man7.org/linux/man-pages/man3/putc.3p.html

https://man7.org/linux/man-pages/man1/strace.1.html

https://man7.org/linux/man-pages/man2/time.2.html

https://man7.org/linux/man-pages/man3/time_t.3type.html

https://man7.org/linux/man-pages/man3/ctime.3p.html

You might also like