0% found this document useful (0 votes)
9 views2 pages

QUIZ 15

The document is a quiz for a programming fundamentals course focused on file handling in C. It includes questions on file operations, true/false statements about functions, and a coding exercise to merge files and count lines. The quiz is structured with specific marks and estimated time for each question.

Uploaded by

mabdullah4505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

QUIZ 15

The document is a quiz for a programming fundamentals course focused on file handling in C. It includes questions on file operations, true/false statements about functions, and a coding exercise to merge files and count lines. The quiz is structured with specific marks and estimated time for each question.

Uploaded by

mabdullah4505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CC-112 Programming Fundamentals Spring 2022

Quiz 15 – Chapter 11 – File Handling [30 Marks, 30 Minutes]

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"

Quiz 15 – Chapter 11 – File Handling Page 1 of 2


CC-112 Programming Fundamentals Spring 2021

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;
_____________________________________________ _____________________________________________
}
_____________________________________________

_____________________________________________

Quiz 15 – Chapter 11 – File Handling Page 2 of 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy