C Language File (Basic I/O) Flashcards

1
Q

File

A

logical collection of 1s and 0s;

all files are truly binary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

File system

A

provides an overall context for organizing and naming the files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Text (aka ASCII) files

A

1s and 0s in the file are encoded in ASCII, making it a

plain printable text that is generally human readable and editable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Binary files

A

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.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

FILE

A

a structure type that holds information about an open file

○ Information like place in the file system, our position in the file, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

open/close a file

A

○ fopen( ): helper function to open a file

○ fclose( ): helper function to close a file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

To read/write 1 character aka a byte from/to a file:

A

○ fgetc( ): reads character from a file and advances “position indicator”
○ fputc( ): writes character to a file and advances “position indicator”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

To read/write 1 line as a string from/to a file:

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

To read/write multiple bytes from/to a file:

A

○ fread( ): reads data from a file into an array

○ fwrite( ): writes data from an array into a file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

To read/write formatted strings from/to a file (or a string itself):

A

○ fprintf( ): writes formatted string to a file
○ fscanf( ): reads formatted string from a file
○ sscanf( ): reads formatted data from a string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Helpful ways to tell if you are at the end of a file:

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Arguments to main( ):

A

○ 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can ASCII files be open in its true binary form with a text editor?

A

No, have to use tools like hexdump

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does C handles files

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Base file operations

A

open, closed, read and write, rewind file to start, seek to a specific position in the file.

17
Q

fopen

A

FILE* fopen (const char *filename, const char *mode)
filename: a string containing name of the file in the file system
mode: a string containing the type of file access (read/write/etc.)
• “r” – open file for reading
• “w” – open file for writing
• “a” – append to file; if file exists, add stuff at the end
• “rb” – open binary file for reading
• “wb” – open file for binary output

Return:
• If file does not exist or cannot be created, NULL is returned
• Otherwise, a pointer to the open FILE is returned

18
Q

Type FILE in C

A

data type that holds information about an open file.

typedef FILE structure, created in standard C library

19
Q

fgetc()

A

int fgetc (FILE* stream)

stream: the pointer to an open file one wishes to read a character from

Return:
• Returns a byte (1 character) read from the file as an integer
• If the file is at its end, it returns EOF
• EOF is typically -1 and indicates it has reached the end of the file

20
Q

fputc()

A

writes character to a file and advances “position indicator”

int fputc (int character, FILE* stream)

Return:
• If no error occurs, returns the same character that has been written
• If an error occurs, returns EOF

21
Q

What is EOF

A
  • # define EOF -1 /* could be different # on different systems*/
  • A constant used to indicate the end of a file
22
Q

Always close a file after opening it

A

fclose( File *filename)

23
Q

fread()

A
reads data from a file into an array
size_t fread (void *ptr, size_t size, size_t nmemb, FILE *stream)

note: size_t is a typically a typedef’ed “unsigned int”

ptr: pointer (of any type) to an array you want to read data into
size: size (in bytes) of a single element of your array
nmemb: the total number of elements in your array
stream: pointer to an open file to read from

  • The total number of elements successfully read
  • If this number is different from nmemb parameter, you’ve hit EOF or an error occurred
24
Q

fwrite(

A

size_t fwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream)

ptr: pointer (of any type) to an array you want to write data from
size: size (in bytes) of a single element of your array
nmemb: total number of elements in your array
stream: pointer to an open file to write to

Return:
• The total number of elements successfully written
• If this number is different from nmemb parameter, an error has occurred

25
Q

Why does this fail

int* array ;
FILE *theFile = fopen (“input_file", “rb"); // test me for NULL!
// read in an array of ints
fread (array, sizeof(int), 10, theFile);
A

array points to nothing
how to solve, allocated memory on stack
int array[10];

26
Q

fgets()

A

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

char* fgets (char* str, int n, FILE* stream)

str: pointer to an array of chars to read string into
n: maximum number of characters to be read from file (including NULL)
stream: pointer to an open file to read from

  • On success, function returns pointer to str
  • On failure, NULL pointer is returned
27
Q

fgets vs fread vs fgetc vs fscanf

A

fgetc() is intended to read 1 byte of data from a file. Gets one character (8bits) from a file

fgets reads a line – i.e. it will stop at a newline. ‘gets’ an entire string from a file

fread() is intended to get multiple bytes of data from a file.
fread reads raw data – it will stop after a specified (or default) number of bytes, independently of any newline that might or might not be present.
fread not only reads until the end of the line but to the end of the file or as many bytes as specified as a parameter

fscanf() is intended to get an entire string from an ASCII file that is formatted the way the programmer specifies.

28
Q

FPUTS

A

char* fputs (const char* str, FILE* stream)

str: pointer to array containing NULL terminated string
stream: pointer to an open file to write to

->fputs writes until it hits null

  • On success, returns a non-negative value
  • Otherwise, returns EOF
29
Q

WILL THIS WORK?

int main ()
{
char* array1 ;
char* array2 = “Tom” ;
char array3 [3] ;
FILE *theFile = fopen (“input_file.txt", “r"); // test me for NULL!
fgets (array1, 4, theFile) ; 
fgets (array2, 4, theFile) ; 
fgets (array3, 4, theFile) ; 
fclose (theFile);
return 0;
}
A

All three will fail

  1. array1 points to NULL
  2. array2 is pointer to String Literal
  3. array has size three but I am reading in 4 Elements
30
Q

fprintf

A

writes formatted string to a fil

int fprintf (FILE* stream, const char* format, …)

stream: pointer to an open file to read from
Format: “formatted” string to be written to the file
*optionally, the formatted string can embed format tags, replaced with these extra arguments

  • On success, total number of characters written
  • On failure, a negative number is returned

e.g. fprintf(file, “%s %d”, name, num)

31
Q

fscanf

A

reads formatted string from a file
int fscanf (FILE* stream, const char* format, …)
stream: pointer to an open file to read from
format: the “formatted” string to be read to with formatting tags

Return:
• On success, returns number of items successfully matched and assigned
• On failure, less than you expected indicates error has occurred

32
Q

sscanf

A

reads formatted data from a STRING!

int sscanf (const char* str, const char* format, …)

str: a string sscanf() will parse – basically, the input to your function
format: the “formatted” of the string to be parsed (the formatting tags)

  • On success, returns number of items successfully matched and assigned
  • On failure, less than you expected indicates error has occurred, EOF is returned
33
Q

What is Endianness

A

Refers to how the binary data in a file is ordered
2 types
- big endian files: Bytes are stored in file from MSB to LSB: e.g. CADE 0000 0002
-little endian files: stored in file from LSB to MSB
DECA 0000 0200.
!bytes and not bits are swapped

34
Q

The purpose of a FILE* in C is to point to a directory we wish to work within our C program

A

false, point to a file

35
Q

Print elements in argv

A

for (int i=0; i< argc ; i++) {

printf ( “argv[%d] = %s\n”, i, argv[i] ) ;}