Lecture 3 - File I/O Flashcards

1
Q

include

A
  • always pulls a header file into another file
  • ex. #include “file.h” pull in file.h from present directory
  • ex. #include <file.h></file.h>
  • pulls the file from /user/include/file.h
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

boolean variables

A
  • we can use variables of type “bool” and assign values of true or false to them
  • you need to #include <stdbool.h> to do this</stdbool.h>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

fopen()

A
  • FILE *fopen(char *file_name, char *mode);
  • file_name: name of file
  • mode: will we read it, write it, or both?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

modes for fopen()

A
  • “r”: opens the file only for reading, must already exist
  • “w”: creates a file for writing or overwrites the given file
  • “a”: appends to the end of a file if it exists or creates and writes to it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

fopen() return values

A
  • if successful, fopen() opens the file and returns a FILE pointer to represent the file
  • if not successful, returns a NULL pointer
  • doesn’t matter what file points to as long as it’s not NULL
  • ALWAYS CHECK THE RETURN VALUE
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

fclose()

A
  • int close(FILE *file_pointer);
  • every successfully opened file must be closed when we are finished with it
  • when the file is closed its internal data is flushed
  • does NOT set the file pointer to null
  • you should manually set it AFTER fclose()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

fclose() return values

A
  • on success, fclose() returns a 0
  • on failure, fclose() returns EOF
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

fprintf()

A
  • fprintf(FILE *stream, const char *format, …)
  • works just like printf() except it takes an extra FILE pointer argument (writes to file)
  • printf(“Hello, world”); is equivalent to fprintf(stdout, “Hello, world”);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

reading data in c

A
  • c is a little different than Java when it comes to reading data
  • think printf() in reverse
    ex. scan(“%d %d”, buffer, &int_var);
  • returns number of successful data conversions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

fscanf()

A
  • works just like scans() except that it takes an extra FILE pointer argument (reads file)
  • the following are equivalent.. scans(“%s”, buffer);
    fscanf(stdin, “%s”, buffer);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

proper error checking with a file

A

-check that the file isn’t NULL after trying to open it
- fclose() the file after you’re done
- set the file to NULL after closing it

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

file I/O essentials

A
  • in a program we refer to files with FILE pointers
  • a file must be OPENED before writing or reading from it (except stdin, stdout, stderr)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

feof()

A
  • int feof (FILE *ptr)
  • checks to
  • used to check to see if try to read past file end
  • returns 0 if the end of file has not been reached and nonzero if it has
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

ferror()

A
  • int ferror(FILE *ptr)
  • used to check if a file stream encountered an error during reading or writing
  • returns 0 if no error occurred and nonzero if one did
  • does not stop execution when an error occurs, just sets an internal error flag for the file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

clearerr()

A
  • void clearerr(FILE *ptr)
  • used to reset the error and end of file flags for a file stream if they exists
  • files set flags internally to mark errors/EOF, clearerr removes them so file can operate w/o issues
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

fwrite()

A
  • size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);
  • ptr is data to be written, size is size of each element, count is number of elements to write, stream is file pointer to write to
  • used to write binary data to a file
  • returns the number of elements to be written (less if error occurred)
17
Q

fread()

A
  • size_t fread(void *ptr, size_t size, size_t count, FILE *stream)
  • prt is pointer to memory where the data will be stored
  • size is size in bytes of each element
  • count is number of elements to read
  • steam is file pointer
  • returns number of elements successfully read (less than if error)
18
Q

stdout

A
  • the default output stream in C (short for standard output)
  • directs output to the console/terminal
  • used by printf(), fprintf() and puts()
  • ex. fprintf(stdout, “Hi!”);
19
Q

printf()

A
  • used to print formatted text to stdout
  • int printf(const char *format, …);
  • format specifies how the text should be printed
  • … allows passing multiple arguments to be formatted
  • returns number of characters printed