File Operations in C tutorial and MCQ Questions
Table of Contents
Learn File Operations in C Programming MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests
In a computer, information is organized into Files and Directories. C File Handling functions enable us to read files from hard disk and write files to the hard disk.
Note: There is a special data type namely FILE (in capital letters). You should create a Pointer Variable to FILE to handle files on disk. FILE is a struct structure type internally.
File Operations in C
Most common C File Operations IO are as below.
- File Opening
- File Closing
- and File Creation
- File Reading (fscanf, fgetc, fgets & fread)
- and File Writing / Editing (fprintf, fputc, fputs & fwrite)
- File Seeking
1. File Opening
The C language’s fopen() function is used to open files. A FILE pointer is the result of the fopen function.
Syntax:
FILE *fp; fp = fopen("filename", "readmode"); if(fp == NULL) { printf("Unable to Read / Write / Create File"); exit(1); } //readmode = "r", "r+", "w", "w+", "a", "a+"
File Read Modes in C language are “r”, “r+”, “w”, “w+”, “a” and “a+”. R is for Read, W is for Write and A is for Append.
2. A File Closing
Every opened file needs to be closed after use in order to free up any memory or resources that it may have occupied. In C, the fclose() function is used to close a file.
Syntax:
FILE *fp; fp = fopen("filename", "readmode"); fclose(fp);
3. File Creation
File can be created simply by changing the File Opening Mode. There are no special functions in C language to create files. File opening modes “w”, “w+”, “a” and “a+” create a File if it is not existing on the disk.
Syntax:
FILE *fp; fp = fopen("abc.txt", "w+");
4. File Reading
A file must first be opened in order to be read File Reading file contents is possible with opening modes like “r,” “r+,” “w+,” and “a+.” End of File is the term we use. A predefined constant called EOF indicates the END of a file.
File Operations in C
Example 1: File Reading using FGETC – character by character reading
#include <stdio.h> int main() { char ch; FILE *fp; fp = fopen("abc.txt", "w+"); while( (ch=fgetc(fp)) != EOF ) { printf("%c", ch); } fclose(fp); return 9; }
Example 2: File Reading using FGETS – Line by line reading
#include <stdio.h> int main() { char str[80]; FILE *fp; fp = fopen("abc.txt", "w+"); while( (fgets(str, 79, fp)) != NULL ) { printf("%s", str); } fclose(fp); return 9; }
5. File Writing
To write a file on disk, it has to be opened in suitable File Opening Mode. File opening modes like “w”, “w+”, “a” and “a+” allow writing of content to files.
Example 1: File Writing using FPUTC – Character by Character
#include <stdio.h> int main() { char str[]= "Tom and Jerry are Good Friends.."; int i=0; FILE *fp; fp = fopen("abc.txt", "a"); while( str[i] != '\0' ) { fputc(str[i], fp); i++; } fclose(fp); return 9; }
File Operations in C
Example 2: File Writing using FPUTS – Line by Line or String by String
#include <stdio.h> int main() { char str[]= "Tom and Jerry are Good Friends.."; FILE *fp; fp = fopen("abc.txt", "a"); fputs(str, fp); fclose(fp); return 9; }
Example 3: File Reading and Writing using FSCANF and FPRINTF
#include <stdio.h> int main() { char str[20], str2[20]; int age; FILE *fp; fp = fopen("abc.txt", "a"); printf("Enter name and age: "); scanf("%s%d", str, &age); //Printing to File or Writing to File fprintf(fp, "%s %d", str, age); fclose(fp); //Now reading or Scanning from FILE fp = fopen("abc.txt", "r"); fscanf(fp, "%s %d", str2, &age); printf("Name=%s, Age=%d", str2, age); fclose(fp); return 9; }
6. File Seeking
Simply moving the virtual cursor within the file so that a Read or Write operation can be carried out at that cursor position is what file seeking entails. The fseek() function in the C language lets you move the cursor within a file.
File Operations
Note: File Seeking is usually done in Binary File Opening Modes “rb” and “wb”.
There are two types of File Opening Modes.
- Binary Mode
- Text Mode
1. Binary Mode
A Binary mode saves disk space by consuming only required amount of bytes to store data like text, numbers, video data, audio data and more. Binary mode is difficult to handle. Functions used in Binary mode are fread, fwrite and fseek().
2. Text Mode
Text mode can not save disk space. An integer 24 occupies same space as for storing a big number like 1234. Binary mode takes exactly same amount of bytes required to store in memory. Functions used in Text Mode are fprintf and fscanf. Text mode is easy to handle for humans.
File Opening Modes in C
There are Six file opening modes in C. Two more File opening modes for Binary Data “rb” and “wb” are also present. Letter “b” is added at the end to indicate Binary Mode. Letter “t” is added at the end of mode to indicate Text Mode. So, “rt” represents Read Mode for Text. are also present.
SNO | Mode | Explanation |
---|---|---|
1 | r | Read:1. Reading of file2. File is not created. |
2 | r+ | Read Plus:1. Reading2. Writing3. File is not created. |
3 | w | Write: Overwrites (modify) existing.1. Writing2. File is created |
4 | w+ | Write Plus: Overwrites (modify) existing.1. Writing2. Reading3. File is created. |
5 | a | Append: Appends text at the end.1. Writing2. File is created |
6 | a+ | Append Plus: Appends text at the end.1. Writing2. Reading3. File is created |
[WpProQuiz 46]
File Operations in C MCQ
1) What is the need for a File when you can store anything in memory.?
A) Memory (RAM) is limited in any computer.
B) A file is stored on Hard Disk which can store Gigabytes of data.
C) File stored on Hard Disk is safe even if PC is switched off. But Memory or RAM contents are cleared when PC is off.
D) All the above
Answer [=] D
2) What is the keyword used to declare a C file pointer.?
A) file
B) FILE
C) FILEFP
D) filefp
Answer [=] B
Explanation:
FILE *fp;
3) What is a C FILE data type.?
A) FILE is like a Structure only
B) FILE is like a Union only
C) FILE is like a user define int data type
D) None of the above
Answer [=] A
Explanation:
Yes. FILE type pointer eg. FILE *fp holds an address of a C Structure that can store type of file operation, memory location of current read and next read and other useful information.
4) Where is a file temporarily stored before read or write operation in C language.?
A) Notepad
B) RAM
C) Hard disk
D) Buffer
Answer [=] D
Explanation:
Yes. A Buffer is like an empty bucket that is filled with information so that direct read write operation on hard disk is avoided for better performance.
5) Choose a correct statement about C file operation program.?
int main() { FILE *fp; char ch; fp=fopen("readme.txt","r"); while((ch=fgetc(fp)) != EOF) { printf("%c",ch); } }
A) FOPEN opens a file named readme.txt in Read Mode (“r).
B) EOF is End Of File. ch==EOF checks for end of file and while loop stops or exits.
C) FGETC(fp) is a function that returns one character and cursor goes to next character.
D) All the above
Answer [=] D
Explanation:
Inside while loop, we have not done any increment operations like i++. Because fget() function takes care of returning one by one character from file.
6) What is the need for closing a file in C language.?
A) fclose(fp) closes a file to release the memory used in opening a file.
B) Closing a file clears Buffer contents from RAM or memory.
C) Unclosed files occupy memory and PC hangs when on low memory.
D) All the above
Answer [=] D
7) If a FILE pointer is NULL what does it mean.?
FILE *fp; fp=fopen("abc.txt","w");
A) Unable to open a file named abc.txt
B) abc.txt is not available on disk
C) Hard disk has hard ware problems.
D) All the above
Answer [=] D
8) Choose a correct statement about FGETS in C program.?
int main() { FILE *fp; char str[80]; fp=fopen("readme.txt","r"); while(fgets(str,80,fp) != NULL) { printf("%s",str); } fclose(fp); }
A) str in fgets() is a like a user buffer that can store 80 characters each time
B) FGETS returns null if no characters are left
C) fgets() reads content from File. FPUS writes content back to File.
D) All the above
Answer [=] D
Explanation:
Here “r” is File open mode. eg. “w” is used for writing a file.
9) Choose a correct statement about C file “R” mode operation using fopen.
fopen("abc.txt","r");
A) If the file abc.txt is found, fopen returns a FILE pointer.
B) If the file abc.txt is not found, fopen returns NULL or 0.
C) File abc.txt is only opened in Read Mode. Now write operation is performed.
D) All the above
Answer [=] D
10) Choose a correct statement about File Write Mode “w”.
File *fp; fp=fopen("abc.txt","w");
A) If the file abc.txt is not found File abc.txt is created on disk.
B) If the file abc.txt is found, fopen() returns a FILE pointer as usual. Every time, contents of abt.txt are overwritten in “w” mode.
C) Read operation is not allowed in “w” mode.
D) All the above
Answer [=] D
Explanation:
All the above
11) Choose a correct statement about C File Mode “w+”.
FILE *p; p=fopen("abc.txt","r+");
A) r+ mode allows reading of existing contents of file abc.txt only if file is found.
B) If file is not found, NULL is returned by fopen().
C) You can read existing contents, edit existing content and add new content.
D) All the above
Answer [=] D
12) Choose a correct statement about C file mode “w+”.
FILE *fp; fp=fopen("abc.txt","w+");
A) Like “w” mode, “w+” mode creates a new file abc.txt if not found. NULL is only returned if some other problems in PC exist in opening a file.
B) w+ mode allows you to read the file also. Only after writing, you can read contents if any written. Before writing existing contents are emptied.
C) w+ mode always makes a file contents empty like w mode.
D) All the above
Answer [=] D
13) Choose a correct statement about C file mode “a”.
FILE *fp; fp=fopen("abc.txt","a");
A) “a” is for append operation. You can append or add new content to the existing contents.
B) If file is not found, new file is created.
C) You can not write read file contents.
D) All the above
Answer [=] D
14) Choose a correct statement about C file mode “a+”.
FILE *fp; fp=fopen("abc.txt","a+);
A) a+ mode always appends new data to the end of existing content
B) a+ mode creates a new file if not found or existing.
C) a+ mode allows reading also. mode “a” allows only appending not reading.
D) All the above
Answer [=] D
15) Choose a correct statement about opening a file in binary mode for reading.
FILE *fp; fp=fopen("abc.txt","rb");
A) rb mode opens the file in binary mode
B) Binary mode is just reading or writing in bytes instead of integers, characters or strings.
C) Binary mode saves memory occupied by contents.
D) All the above
Answer [=] D
16) What is the syntax for writing a file in C using binary mode.?
FILE *fp;
A)
fp=fopen("abc.txt","wr");
B)
fp=fopen("abc.txt","wb");
C)
fp=fopen("abc.txt","wbin");
D)
fp=fopen("abc.txt","b");
Answer [=] B
17) What are the C functions used to read or write a file in Text Mode.?
A) fprintf(), fscanf()
B) fread(), fwrite()
C) fprint(), fscan()
D) read(), write()
Answer [=] A
Explanation:
You can even use fputs(). Only strings can read or write instead of integers, float or characters.
18) What are the C functions used to read or write a file in Binary Mode.?
A) fprintf(), fscanf()
B) fread(), rwrite()
C) readf(), writef()
D) printf(), scanf()
Answer [=] B
Explanation:
fwrite(pointer, size, count , filepointer); count=1 usually
fwrite(pointer, size, count , filepointer);
19) What is the C function used to move current pointer to the beginning of file.?
FILE *fp;
A) rev(fp)
B) rewind(fp)
C) rew(fp)
D) wind(fp)
Answer [=] B
Explanation:
ftell(fp) returns current position of pointer inside FILE.
fseek(fp,0,SEEK_END); long num=ftell(fp); //num contains the file size in bytes.
20) Choose a correct syntax for FSCANF and FPRINTF in c language.?
A) fprintf(“format specifier”,variables, fp); fscanf(“format specifier”,variables, fp);
B) fprintf(fp,count,”format specifier”,variables); fscanf(fp,count,”format specifier”,variables);
C) fprintf(fp,”format specifier”,variables); fscanf(fp,”format specifier”,variables);
D) None of the above
Answer [=] C
Explanation:
You can check for end of file contents using EOF.
fscanf()!= EOF fread() != EOF fgetc() != EOF fgets() != NULL fseek(fp,bytescount,SEEK_SET) moves pointer to that count location. Example a binary file is mp3, jpg etc.