QUIZ 15
QUIZ 15
MAbdullah
Name: _____________________________ 133715
Roll No: _____________________________
Question 1: [1x5 marks] [estimated time 5 minutes] 1. Consider file content “This is a new file”
Supply the missing words to complete the statements.
#include <stdio.h>
end-of-file marker informs the program that
1. The _______________ int main(){
there’s no more data to process. FILE* p;
2. "rb+"
_________ mode opens an existing binary file for int n = 0;
update. p = fopen("newname.txt", "rb");
if (p == NULL)
3. A C program administers each file with a separate
printf("Error opening file");
_____________
FILE structure.
else {
4. __________
rewind() repositions the file position pointer to
while (fgetc(p) != EOF) {
the beginning (byte 0) of the file. ++n;
5. Function ____________
fseek() repositions the file position }
pointer to a specific location in the file. if (feof(p)) {
printf("%d\n", n);
Question 2: [1x5 marks] [estimated time 5 minutes] } else
puts("End-of-File was not reached.");
In the following questions mark True or False.
a) (T / F) Function fopen takes three arguments. fclose(p);
}
b) (T / F) When you open a file for writing, fopen
return 0;
doesn’t warn you if the file already exists.
}
c) (T / F) Function feof returns true only after the
program attempts to read the nonexistent data
Output: ______________________________________
"Error opening file"
following the last line.
2.
d) (T / F) Function fread returns the number of
#include <stdio.h>
items it successfully inputs.
int main(){
e) (T / F) You should always close a file as soon as
FILE* pFile;
it’s no longer needed.
pFile = fopen("sample.txt", "wt");
Question 3: [4x3 marks] [estimated time 10 minutes] for (char c = 'F'; c >= 'A'; c--) {
putc(c, pFile);
Dry Run the pseudocode and write the expected
}
output in the File. (Write “Blank” for no output or
fclose(pFile);
write “compile Error” if there is any error).
return 0;
}
Output: _____________________________________
"FEDCBA"
3. _____________________________________________
#include <stdio.h>
#include <stdio.h> void mergeFiles(const char *file1, const char *file2, const
_____________________________________________
char *output) {
int main(){ FILE *f1 = fopen(file1, "r");
_____________________________________________
FILE* p; FILE *f2 = fopen(file2, "r");
FILE *fout = fopen(output, "w");
_____________________________________________
int n = 0;
char line[256];
p = fopen("myfile.txt", "r");
_____________________________________________
if (!f1 || !f2 || !fout) {
if (p == NULL)
printf("Error opening files.\n");
perror("Error opening file"); _____________________________________________
return;
else { }
_____________________________________________
do { while (fgets(line, sizeof(line), f1)) {
c = getc(p); fputs(line, fout);
_____________________________________________
}
if (c == '$')
while (fgets(line, sizeof(line), f2)) {
n++; _____________________________________________
fputs(line, fout);
} while (c != EOF); }
_____________________________________________
fclose(p); fclose(f1);
printf("%d\n", n); _____________________________________________
fclose(f2);
fclose(fout);
} _____________________________________________
}
return 0;
} int countLines(const char *filename) {
_____________________________________________
FILE *file = fopen(filename, "r");
int lines = 0;
_____________________________________________
Compile Error
Output: ______________________________________ char ch;
_____________________________________________
if (!file) {
Question 4: [8 marks] [estimated time 10 minutes] printf("Error opening file.\n");
_____________________________________________
return -1;
Write a C program that:
}
Merge the contents two files “A.txt” and “B.txt” _____________________________________________
into a third file “C.txt” while ((ch = fgetc(file)) != EOF) {
Read the file “C.txt” and display the number of if (ch == '\n') {
_____________________________________________
lines in the file on Console. lines++;
}
_____________________________________________ _____________________________________________
}
_____________________________________________ _____________________________________________
fclose(file);
return lines;
_____________________________________________ _____________________________________________
}
_____________________________________________ _____________________________________________
int main() {
mergeFiles("A.txt", "B.txt", "C.txt");
printf("Number of lines in C.txt: %d\n", countLines("
_____________________________________________ _____________________________________________
C.txt"));
return 0;
_____________________________________________ _____________________________________________
}
_____________________________________________
_____________________________________________