C Questions and Answers – File Operations – 2

«
»

This set of C Multiple Choice Questions & Answers focuses on “File Operations – 2”.

1. what is the function of fputs()?
a) read a line from a file
b) read a character from a file
c) write a character to a file
d) write a line to a file
View Answer

Answer: d
Explanation: The fputs() is used to write a line to a file. fputs() syntax can be written as
int fputs(const char *str, FILE *stream);
advertisement

2. What does the following C code snippet mean?

char *gets(char *s)

a) reads the next input line into the array s
b) writes the line into the array s
c) reads the next input character into the array s
d) write a character into the array
View Answer

Answer: a
Explanation: gets() reads the next input line into the array s, terminating newline is replaced with ‘\0’.It returns s, or NULL if end of file or error occurs.
advertisement
advertisement

3. Which function will return the current file position for stream?
a) fgetpos()
b) fseek()
c) ftell()
d) fsetpos()
View Answer

Answer: c
Explanation: The current file position is returned by ftell() function for stream, or -1L on error.
advertisement

4. Select the right explanation for the following C code snippet.

int fgetpos(FILE *stream, fpos_t *s)

a) records the current position in stream in *s
b) sets the file position for stream in *s
c) positions stream at the position recorded in *s
d) reads from stream into the array ptr
View Answer

Answer: a
Explanation:fgetpos() records the current position in stream in *s, for subsequent use by fsetpos() . The type fpost_t is suitable for recording such values.
advertisement

5. Which functions is declared in <errno. h>?
a) fseek()
b) ftell()
c) ferror()
d) fsetpos()
View Answer

Answer: c
Explanation: ferror() is declared under <errno. h>. ferror() returns non-zero if the error indicator for stream is set.
advertisement

6. setvbuf() and setbuf() function controls buffering for the stream.
a) true
b) false
View Answer

Answer: a
Explanation: setvbuf() and setbuf() controls buffering for the stream. If buff is NULL, buffering is turned off for the stream.

7. The functions vprintf(), vfprintf(), and vsprintf() are not equivalent to the corresponding printf() functions except the variable argument list.
a) true
b) false
View Answer

Answer: b
Explanation: The functions vprintf() , vfprintf() , and vsprintf() are similar to the corresponding printf() functions except that the variable argument list is replaced by arg.

8. The______function reads atmost one less than the number of characters specified by size from the given stream and it is stored in the string str.
a) fget()
b) fgets()
c) fput()
d) fputs()
View Answer

Answer: b
Explanation: The fgets() function reads one less than the number of characters indicated by the size from the given stream and it is stored in the string str. The fgets() terminates as soon as it encounters either a newline character, EOF, or other error.

9. What does the following C code snippet mean?

int ungetc(int c, FILE *stream)

a) pushes c back onto a stream
b) deletes c from the stream
c) reads frequency of c in stream
d) no action is taken by the command
View Answer

Answer: a
Explanation: ungetc() pushes c back onto stream, where it will be returned on the next read. Only one character of pushback per stream is Guaranteed.

10. Choose the correct difference between getc() and fgetc().
a) If it is not a macro, it may evaluate stream more than once
b) if it is amacro, it may not evaluate stream more than once
c) if it is a macro, it may evaluate stream more than once
d) no difference between fgetc() and getc()
View Answer

Answer: c
Explanation: getc() is equivalent to fgetc() except that if it is a macro, it may evaluate more than once.

Sanfoundry Global Education & Learning Series – C Programming Language.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter