I/O in C Flashcards

1
Q

What are the “standard” streams?

A
  • stdin - standard input
    • usually from input but can also be used when piping in unix
  • stdout - standard output
    • usually to screen but can also be used when piping in unix
  • stderr - standard error
    • usually to screen

note: #include <stdio.h> is required</stdio.h>

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

What using scanf, what operater is needed?

A

& is needed for int, floar or char but not needed with strings

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

How would you introduce error checking with scanf?

A

while(scanf(“%d”, &1) == 1){

//will loop while we have appropriate input

}

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

How would you skip blank spaces or dashes in the input stream?

A

scanf(“%d %d %d”, &day, &month, &year);

scanf(“%d-%d-%d”, &day,&month,&year);

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

How do you use fgets( ) and how do you specifify a maximum number of characters? How does this differ from fscanf( )? What does fscanf( ) return?

A

define MAX 81

char buffer[MAX];

fgets(buffer, MAX, stdin);

  • reads from stdin, stores info in buffer up to the MAX-1 (it allows for the addition of the null terminating character)

fscanf( ) reads the whole line un buffered

  • it returns the number of input items converted and assigned successfully
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you open a file in C?

List the read modes.

A

FILE * inputfile = NULL;

inputfile = fopen(“names.txt”, “r”);

Read Modes:

  • r - read only
  • w - write to (overwriting current file if there is one)
  • a - appends to the file
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the basic file handling functions:

A
  • fopen()
  • fclose()
  • fscanf()
  • fprintf()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you error check the opening of a file?

A

File handler becomes NULL when an fopen() occurs

if(inputfile == NULL)

{

printf(“Unable to open input file.\n”);

return 1;//ends program if inside main function

}

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

How would you read a list of name from inputfile?

A

while(fscanf(inputfile, “%s”, name) == 1)

{

}

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

How would you open a file to write to, and then write to it?

A

FILE *outfile = null;

outfile = fopen(“name.txt”, “w”);

if (fprintf(“%s\n”, name) <= 0){printf(“error writing to file”); return 1;}

fputs(char * s, * filehandler);

This will not only write to the file but error check it at the same time.

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

How do you use:

  1. fgets()
  2. fputs()
A
  1. char *fgets(char *s, int n, FILE * fp)
    1. reads an entire line into s, up to n-1 characters in length
    2. returns the point s on success or NULL if error or EOF is reached
    3. (‘\n’) stops reading but is included in the string
  2. int fputs(char *s, FILE *fp);
    1. returns the number of characters written if successful, otherwise, return EOF
How well did you know this?
1
Not at all
2
3
4
5
Perfectly