C Language File (Basic I/O) Flashcards
File
logical collection of 1s and 0s;
all files are truly binary.
File system
provides an overall context for organizing and naming the files.
Text (aka ASCII) files
1s and 0s in the file are encoded in ASCII, making it a
plain printable text that is generally human readable and editable.
Binary files
1s and 0s in files are encoded in a format other than ASCII. The format is generally created by the programmer (.DOC, .PPT, .OBJ). Generally
not human-readable or editable i.e. looks like garbage. Usually generated and interpreted by some program. This name is a misnomer because all files are truly
binary; it should actually be called “non-ASCII” to refer to “everything else.”
FILE
a structure type that holds information about an open file
○ Information like place in the file system, our position in the file, etc.
open/close a file
○ fopen( ): helper function to open a file
○ fclose( ): helper function to close a file
To read/write 1 character aka a byte from/to a file:
○ fgetc( ): reads character from a file and advances “position indicator”
○ fputc( ): writes character to a file and advances “position indicator”
To read/write 1 line as a string from/to a file:
fgets( ): reads a string from a file and stores it in string. Stops reading when n-1 characters are read, when the newline character is read, or EOF.
fputs( ): writes a string to a file.
To read/write multiple bytes from/to a file:
○ fread( ): reads data from a file into an array
○ fwrite( ): writes data from an array into a file
To read/write formatted strings from/to a file (or a string itself):
○ fprintf( ): writes formatted string to a file
○ fscanf( ): reads formatted string from a file
○ sscanf( ): reads formatted data from a string
Helpful ways to tell if you are at the end of a file:
EOF: actually a typedef for “-1.” You can compare the return of functions like fgetc( ) to EOF to see if you’ve reached the end of a file. ○ int feof(FILE *f): a function that you can call to see if you are at the end of a file. Returns “1” if you are at the end of a file.
The three I/O connected devices are:
○ stdin: standard input (console)
○ stdout: standard output (console, for output)
○ stderr: standard error (console, for error message)
How set up?
There are three special files that are opened automatically for all C programs:
stdin, stdout, and stderr. The programmer does not need to call fopen( ) or
fclose( ) on these files. These special “files” aren’t really files; they are I/O
devices! They represent the I/O devices of the keyboard and the ASCII display
(the normal one and a special “error” display). These files are consistent with the
idea that C is indeed “file-oriented,” meaning that it treats I/O devices no
differently from files. You can
Arguments to main( ):
○ argc: number of “strings” on the command line (argc >= 1)
○ argv: list of strings containing all of these words
■ Note the declaration of argv as a pointer to an array of pointers; double-dereferencing
Can ASCII files be open in its true binary form with a text editor?
No, have to use tools like hexdump
How does C handles files
handles them as one long string. Can’t pick and choose where I want to go in file, instead the file is treated like it was a tape used in a cassete recorderer.