File IO Flashcards

1
Q

What are the functions that can be used to

  1. read from stdin
  2. output to the stdout

Single character at a time?

A

int getchar(void): can be used to read in a single character at a time from the stdin

int putchar(int c) can be used to output a single character at a time.

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

What does getchar return?

A

getchar returns an EOF or an unsigned char

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

What is the prototype for fopen and what does it return?

A

the prototype for fopen is

FILE* fopen (const char* fname, const char* mode);

It returns either:

  • NULL, if the file doesn’t exist
  • the handle/stream to the file.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the different modes in which a file can be opened and what are the symbols?

A

The different modes are:

  • r: read
  • w: write
  • a: append
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the prototype of fprintf and how does it work?

A

int fprintf (FILE* fp, char* format, ……);

It takes in a file pointer/handler and then writes the text along with the input data onto that file.

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

Name the functions that can be used to write a character, and a string to a file.

A

Character: fputc(char c, FILE* fp);

String: fputs(char* str, FILE* fp);

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

Name the functions that can be used to read a

  1. character and also to read
  2. A string of length n
A

char c = fgetc(fp);

fgets(s, n, fp);

where n is the maximum length to read, the read string will be stored inside the variable s

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

gets vs fgets

A

gets: does not care about the size of the buffer and reads until a newline is found. The newline is not kept by gets.
fgets: reads from the file and stops when the maximum number of bytes are read, a newline character is found or the end of file is reached.

When fgets encounters a newline, it keeps it.

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

puts vs fputs

A

puts adds an extra \n after printing

fputs does not

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