.
Previous

Interview Questions on C File Handlings

1. How to open a file in write mode?

File *fp;
Fp = fopen(“path_to_file”,”w”);

2. How to open a file in read mode?

File *fp;
Fp = fopen(“path_to_file”,”r”);

3. How to open a file in append mode?

File *fp;
Fp = fopen(“path_to_file”,”a”);

4. How to write contents to a File?

fputc() writes the character value.
fputs() writes the string s to the output stream referenced by fp.
fprintf(FILE *fp, const char *format);

5. How to read contents of file?

fgetc() function reads a character from the input file referenced by fp.
fgets() reads up to n - 1 characters from the input stream referenced by fp to the to the buffer
fscanf() reads reads characters from the input stream referenced by fp to the buffer.

6. What are the binary I/O Functions?

size_t fread(void *ptr, size_t size_of_elements, 
             size_t number_of_elements, FILE *a_file);
              
size_t fwrite(const void *ptr, size_t size_of_elements, 
             size_t number_of_elements, FILE *a_file);
Both of these functions should be used to read or write blocks of memories

7. Write a small program to copy file contents to another file?

See C Programs

Previous