stdio.h Flashcards

1
Q

What is fputs()?

Declaration:

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

A

Writes a string to the specified stream up to but not including the null character.

On success a nonnegative value is returned. On error EOF is returned.

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

What are stderr, stdin, and stdout?

A

These are pointers to FILE types which correspond to the standard error, standard input, and standard output streams.

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

What is fseek()?

Declaration:

int fseek(FILE *stream, long int offset, int whence);

A

Sets the file position of the stream to the given offset. The argument offset signifies the number of bytes to seek from the given whence position. The argument whence can be:

SEEK_SET Seeks from the beginning of the file.
SEEK_CUR Seeks from the current position.
SEEK_END Seeks from the end of the file.

On a text stream, whence should be SEEK_SET and offset should be either zero or a value returned from ftell.

The end-of-file indicator is reset. The error indicator is NOT reset.

On success zero is returned. On error a nonzero value is returned.

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

What is freopen()?

Declaration:

FILE *freopen(const char *filename, const char *mode, FILE *stream);

A

Associates a new filename with the given open stream. The old file in stream is closed. If an error occurs while closing the file, the error is ignored. The mode argument is the same as described in the fopen command. Normally used for reassociating stdin, stdout, or stderr.

On success the pointer to the stream is returned. On error a null pointer is returned.

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

What is:

int vprintf(const char *format, va_list arg);

A

Sends formatted output to stdout using an argument list

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

What is:

int vsprintf(char *str, const char *format, va_list arg);

A

Sends formatted output to a string using an argument list

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

What are SEEK_CUR, SEEK_END, and SEEK_SET?

A

These are used in the fseek function.

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

What is getc()?

Declaration:

int getc(FILE *stream);

A

Gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream.

This may be a macro version of fgetc.

On success the character is returned. If the end-of-file is encountered, then EOF is returned and the end-of-file indicator is set. If an error occurs then the error indicator for the stream is set and EOF is returned.

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

What is TMP_MAX?

A

The maximum number of unique filenames that the function tmpnam can generate.

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

What is putchar()?

Declaration:

int putchar(int char);

A

Writes a character (an unsigned char) specified by the argument char to stdout.

On success the character is returned. If an error occurs, the error indicator for the stream is set and EOF is returned.

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

What is remove()?

Declaration:

int remove(const char *filename);

A

Deletes the given filename so that it is no longer accessible (unlinks the file). If the file is currently open, then the result is implementation-defined.

On success zero is returned. On failure a nonzero value is returned.

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

What is fopen()?

Declaration:

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

A

Opens the filename pointed to by filename. The mode argument may be one of the following constant strings:

r read text mode
w write text mode (truncates file to zero length or creates new file)
a append text mode for writing (opens or creates file and sets file pointer to the end-of-file)
rb read binary mode
wb write binary mode (truncates file to zero length or creates new file)
ab append binary mode for writing (opens or creates file and sets file pointer to the end-of-file)
r+ read and write text mode
w+ read and write text mode (truncates file to zero length or creates new file)
a+ read and write text mode (opens or creates file and sets file pointer to the end-of-file)
r+b or rb+ read and write binary mode
w+b or wb+ read and write binary mode (truncates file to zero length or creates new file)
a+b or ab+ read and write binary mode (opens or creates file and sets file pointer to the end-of-file)

If the file does not exist and it is opened with read mode (r), then the open fails.

If the file is opened with append mode (a), then all write operations occur at the end of the file regardless of the current file position.

If the file is opened in the update mode (+), then output cannot be directly followed by input and input cannot be directly followed by output without an intervening fseek, fsetpos, rewind, or fflush.

On success a pointer to the file stream is returned. On failure a null pointer is returned.

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

What is getchar()?

Declaration:

int getchar(void);

A

Gets a character (an unsigned char) from stdin.

On success the character is returned. If the end-of-file is encountered, then EOF is returned and the end-of-file indicator is set. If an error occurs then the error indicator for the stream is set and EOF is returned.

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

What is setvbuf()?

Declaration:

int setvbuf(FILE *stream, char *buffer, int mode, size_t size);

A

Defines how a stream should be buffered. This should be called after the stream has been opened but before any operation has been done on the stream. The argument mode defines how the stream should be buffered as follows:

_IOFBF Input and output is fully buffered. If the buffer is empty, an input operation attempts to fill the buffer. On output the buffer will be completely filled before any information is written to the file (or the stream is closed).
_IOLBF Input and output is line buffered. If the buffer is empty, an input operation attempts to fill the buffer. On output the buffer will be flushed whenever a newline character is written.
_IONBF Input and output is not buffered. No buffering is performed.

The argument buffer points to an array to be used as the buffer. If buffer is a null pointer, then setvbuf uses malloc to create its own buffer.

The argument size determines the size of the array.

On success zero is returned. On error a nonzero value is returned.

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

What is rename()?

Declaration:

int rename(const char *old_filename, const char *new_filename);

A

Causes the filename referred to by old_filename to be changed to new_filename. If the filename pointed to by new_filename exists, the result is implementation-defined.

On success zero is returned. On error a nonzero value is returned and the file is still accessible by its old filename.

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

What is fsetpos()?

Declaration:

int fsetpos(FILE *stream, const fpos_t *pos);

A

Sets the file position of the given stream to the given position. The argument pos is a position given by the function fgetpos. The end-of-file indicator is cleared.

On success zero is returned. On error a nonzero value is returned and the variable errno is set.

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

What is typedef size_t?

A

The unsigned integer result of the sizeof keyword.

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

What is tmpfile()?

Declaration:

FILE *tmpfile(void);

A

Creates a temporary file in binary update mode (wb+). The tempfile is removed when the program terminates or the stream is closed.

On success a pointer to a file stream is returned. On error a null pointer is returned.

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

What is rewind()?

Declaration:

void rewind(FILE *stream);

A

Sets the file position to the beginning of the file of the given stream. The error and end-of-file indicators are reset.

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

What is ftell()?

Declaration:

long int ftell(FILE *stream);

A

Returns the current file position of the given stream. If it is a binary stream, then the value is the number of bytes from the beginning of the file. If it is a text stream, then the value is a value useable by the fseek function to return the file position to the current position.

On success the current file position is returned. On error a value of -1L is returned and errno is set.

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

What is perror()?

Declaration:

void perror(const char *str);

A

Prints a descriptive error message to stderr. First the string str is printed followed by a colon then a space.

Then an error message based on the current setting of the variable errno is printed.

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

What is NULL?

A

The value of a null pointer constant.

23
Q

What is fwrite()?

Declaration:

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

A

Writes data from the array pointed to by ptr to the given stream. It writes nmemb number of elements of size size. The total number of bytes written is (size*nmemb).

On success the number of elements writen is returned. On error the total number of elements successfully writen (which may be zero) is returned.

24
Q

What is BUFSIZ?

A

An integer which represents the size of the buffer used by the setbuf function.

25
Q

What is fgets()?

Declaration:

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

A

Reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first. The newline character is copied to the string. A null character is appended to the end of the string.

On success a pointer to the string is returned. On error a null pointer is returned. If the end-of-file occurs before any characters have been read, the string remains unchanged.

26
Q

What are _IOFBF, _IOLBF, and _IONBF?

A

These are used in the setvbuf function.

27
Q

What is typedef FILE?

A

A type suitable for storing information for a file stream.

28
Q

What is:

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

A
29
Q

What is EOF?

A

A negative integer which indicates an end-of-file has been reached.

30
Q

What is putc()?

Declaration:

int putc(int char, FILE *stream);

A

Writes a character (an unsigned char) specified by the argument char to the specified stream and advances the position indicator for the stream.
This may be a macro version of fputc.

On success the character is returned. If an error occurs, the error indicator for the stream is set and EOF is returned.

31
Q

What is fread()?

Declaration:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

A

Reads data from the given stream into the array pointed to by ptr. It reads nmemb number of elements of size size. The total number of bytes read is (size*nmemb).

On success the number of elements read is returned. On error or end-of-file the total number of elements successfully read (which may be zero) is returned.

32
Q

What is FILENAME_MAX?

A

An integer which represents the longest length of a char array suitable for holding the longest possible filename. If the implementation imposes no limit, then this value should be the recommended maximum value.

33
Q

What is:

int printf(const char *format, …);

A

Sends formatted output to stdout

34
Q

What is puts()?

Declaration:

int puts(const char *str);

A

Writes a string to stdout up to but not including the null character. A newline character is appended to the output.

On success a nonnegative value is returned. On error EOF is returned.

35
Q

What is:

int vfprintf(FILE *stream, const char *format, va_list arg);

A

Sends formatted output to a stream using an argument list

36
Q

What is FOPEN_MAX?

A

An integer which represents the maximum number of files that the system can guarantee that can be opened simultaneously.

37
Q

What is:

int scanf(const char *format, …);

A

Reads formatted input from stdin

38
Q

What is fclose()?

Declaration:

int fclose(FILE *stream);

A

Closes the stream. All buffers are flushed.

If successful, it returns zero. On error it returns EOF.

39
Q

What is ungetc()?

Declaration:

int ungetc(int char, FILE *stream);

A

Pushes the character char (an unsigned char) onto the specified stream so that the this is the next character read. The functions fseek, fsetpos, and rewind discard any characters pushed onto the stream.

Multiple characters pushed onto the stream are read in a FIFO manner (first in, first out).

On success the character pushed is returned. On error EOF is returned.

40
Q

What is setbuf()?

Declaration:

void setbuf(FILE *stream, char *buffer);

A

Defines how a stream should be buffered. This should be called after the stream has been opened but before any operation has been done on the stream. Input and output is fully buffered.

The default BUFSIZ is the size of the buffer. The argument buffer points to an array to be used as the buffer. If buffer is a null pointer, then the stream is unbuffered.

41
Q

What is:

int sprintf(char *str, const char *format, …);

A

Sends formatted output to a string.

42
Q

What is feof()?

Declaration:

int feof(FILE *stream);

A

Tests the end-of-file indicator for the given stream. If the stream is at the end-of-file, then it returns a nonzero value. If it is not at the end of the file, then it returns zero.

43
Q

What is L_tmpnam?

A

An integer which represents the longest length of a char array suitable for holding the longest possible temporary filename created by the tmpnam function.

44
Q

What is fflush()?

Declaration:

int fflush(FILE *stream);

A

Flushes the output buffer of a stream. If stream is a null pointer, then all output buffers are flushed.
If successful, it returns zero. On error it returns EOF.

45
Q

What is:

int fscanf(FILE *stream, const char *format, …);

A

Reads formatted input from a stream

46
Q

What is ferror()?

Declaration:

int ferror(FILE *stream);

A

Tests the error indicator for the given stream. If the error indicator is set, then it returns a nonzero value. If the error indicator is not set, then it returns zero.

47
Q

What is clearerr()?

Declaration:

void clearerr(FILE *stream);

A

Clears the end-of-file and error indicators for the given stream. As long as the error indicator is set, all stream operations will return an error until clearerr or rewind is called.

48
Q

What is typedef fpos_t?

A

A type suitable for storing any position in a file.

49
Q

What is tmpnam()?

Declaration:

char *tmpnam(char *str);

A

Generates and returns a valid temporary filename which does not exist. Up to TMP_MAX different filenames can be generated.

If the argument str is a null pointer, then the function returns a pointer to a valid filename. If the argument str is a valid pointer to an array, then the filename is written to the array and a pointer to the same array is returned. The filename may be up to L_tmpnam characters long.

50
Q

What is fgetpos()?

Declaration:

int fgetpos(FILE *stream, fpos_t *pos);

A

Gets the current file position of the stream and writes it to pos.

If successful, it returns zero. On error it returns a nonzero value and stores the error number in the variable errno.

51
Q

What is gets()?

Declaration:

char *gets(char *str);

A

Reads a line from stdin and stores it into the string pointed to by str. It stops when either the newline character is read or when the end-of-file is reached, whichever comes first. The newline character is not copied to the string. A null character is appended to the end of the string.

On success a pointer to the string is returned. On error a null pointer is returned. If the end-of-file occurs before any characters have been read, the string remains unchanged.

52
Q

What is:

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

A

Reads formatted input from a string

53
Q

What is fputc()?

Declaration:

int fputc(int char, FILE *stream);

A

Writes a character (an unsigned char) specified by the argument char to the specified stream and advances the position indicator for the stream.

On success the character is returned. If an error occurs, the error indicator for the stream is set and EOF is returned.