Input and Output in C is provided by several standard library functions and not by the core language itself
The standard library functions operate on streams (source or destination of data)
The state of a stream is stored in the FILE structure declared in <stdio.h>
A FILE structure variable should not be created by the programmer
The C library provides three standard streams in <stdio.h>
Standard input – stdin
Standard output – stdout
Standard error – stderr
Stream that represents the standard input; usually this is the keyboard
The stdin object is the standard input stream; it can be redirected by using the freopen function
It can also be redirected at the command line
executable < filename
stdin of programexecutable1 | executable2
executable1 available in stdin of executable2Stream that represents the standard output; usually this is the display
The stdout object is the standard output stream; it can be redirected using the freopen function
It can also be redirected at the command line
executable > filename
stdout of program to file filenameexecutable1 | executable2
stdout of executable1 to stdin of executable2Stream that represents the standard error; usually this is the display
The stdout object is the standard output stream; it can be redirected using the freopen function
Program errors should be sent to this stream; this way, when the standard output is redirected to some other file, the error messages will continue to appear to the user on the display
It can also be redirected at the command line
executable 2> filename
Open a file stream
FILE* fp;
fp = fopen(name, mode);
name is a relative or absolute file name and path
mode can be "r", "w", "a", "rt", "wt", "at", "rb", "wb", "ab", "r+t", "w+t", "a+t", "r+b", "w+b", or "a+b"
Use the functions in <stdio.h> to manipulate the content of the file stream
Call fclose to close the file stream
Function printf prints text to stdout
int printf(char* format, ...)
Function fprintf prints to any open stream
int fprintf(FILE* fp, char* format, ...)
format is the format string; it contains the text to be printed, interspersed with conversion specifications that are used to convert and print the arguments that follow
%[flags][width][.precision][modifiers]type
flags can be -, +, space, 0, #
width specifies the minimum field width
precision specifies different things depending on the type
modifiers can be h, l or L
type can be d, i, o, x, X, u, c, s, f, e, E, g, G, p, n, or %
Function scanf reads formatted input from stdin
int scanf(char *format, ...)
Function fscanf reads formatted input from any stream
int fscanf(FILE *fp; char *format, ...)
format is the format string containing text to be matched against the input
Blanks and tabs in the format string are ignored
White-space characters in the input stream act as field separators
%[*][width][modifiers]type
* specifies assignment suppression
width specifies the maximum width
modifiers can be h or l
type can be d, i, o, u, x, c, s, e, f, g, p, n, [...], [^...], or %
scanf("%d/%d/%d", &day, &month, &year)
A function may contain a variable length argument list
int printf(const char*, ...)
Header <stdarg.h> contains macro definitions that define how to read the argument list
Declare a variable ap of type va_list
Call va_start(ap, lastarg) to initialize ap; lastarg is the last argument before ...
Call va_arg(ap, type) to read next argument
Call va_end(ap) to clean up
int getc (FILE* fp)
fp, or EOFint putc(int c, FILE* fp)
c to stream fpEOF on errorgetchar()
getc(stdin)putchar(c)
putc(c, stdout)char* fgets(char* line, int maxline, FILE* fp)
(maxline - 1) characters from file stream fpNULL on error, or EOFint fputs(char* line, FILE* fp)
line to the file stream fpEOF if an error occursfseek(FILE* stream, long offset, int origin)
SEEK_SET, SEEK_CUR or SEEK_ENDlong ftell(FILE* stream)
-1L on errorvoid rewind(FILE* stream)
fseek(fp, 0L, SEEK_SET)void clearerr(FILE* stream)
int feof(FILE* stream)
int ferror(FILE* stream)
void perror(const char*)
Requires <sys/stat.h> and, on BSD/Linux, <dirent.h>
DIR* dir;
struct dirent* item;
struct stat statbuf;
dir = opendir(".");
item = readdir(dir);
while(item != NULL) {
stat(item->d_name, &statbuf);
if(S_ISDIR(statbuf.st_mode)) {
//...
}
item = readdir(dir);
}
Write a program that functions like the Unix tar command. The program should pack all files in the current directory into a single file whose name is specified at the command line. If the program receives the –u flag followed by a file name, it should unpack the content of the file to the current directory
pack [-u] filename