Output of C programs | Set 40 (File handling)
Last Updated :
18 Aug, 2017
Prerequisite : File handling
1. What is the output of this program by manipulating the text file?
C
#include <stdio.h>
int main()
{
if (remove("myfile.txt") != 0)
perror("Error");
else
puts("Success");
return 0;
}
Options:
a) Error
b) Success
c) Runtime Error
d) Can’t say
Answer : d
Explanation : If myfile.txt exists, then it will delete the file. Else it will print an error message.
2. What is the output of this program?
C
#include <stdio.h>
int main()
{
FILE* p;
int c;
int n = 0;
p = fopen("myfile.txt", "r");
if (p == NULL)
perror("Error opening file");
else {
do {
c = getc(p);
if (c == '$')
n++;
} while (c != EOF);
fclose(p);
printf("%d\n", n);
}
return 0;
}
Options:
a) Count of ‘$’ symbol
b) Error opening file
c) Any of the mentioned
d) None of the mentioned
Answer : c
Explanation : Any one is possible – Either the file doesn’t exist or If exist, it will print the total number of ‘$’ character.
3. What is the output of this program in the text file?
C
#include <stdio.h>
int main()
{
FILE* pFile;
char c;
pFile = fopen("sample.txt", "wt");
for (c = 'A'; c <= 'E'; c++) {
putc(c, pFile);
}
fclose(pFile);
return 0;
}
Options:
a) ABCD
b) ABC
c) ABCDE
d) None of the mentioned
Answer : c
Explanation : In this program, We are printing from A to E by using the putc function.
Output:
$ g++ out2.cpp
$ a.out
ABCDE
4. What is the name of the myfile2 file after executing this program?
C
#include <stdio.h>
int main()
{
int result;
char oldname[] = "myfile2.txt";
char newname[] = "newname.txt";
result = rename(oldname, newname);
if (result == 0)
puts("success");
else
perror("Error");
return 0;
}
Options:
a) name
b) new
c) newname
d) None of the mentioned
Answer : c
Explanation : In this program, We are renaming the myfile2 to newname by using the function rename.
Output:
myfile2.txt is renamed to newname.txt
5. How many number of characters are available in newname.txt?
C
#include <stdio.h>
int main()
{
FILE* p;
int n = 0;
p = fopen("newname.txt", "rb");
if (p == NULL)
perror("Error opening file");
else {
while (fgetc(p) != EOF) {
++n;
}
if (feof(p)) {
printf("%d\n", n);
} else
puts("End-of-File was not reached.");
fclose(p);
}
return 0;
}
Options:
a) 10
b) 15
c) Depends on the text file
d) None of the mentioned
Answer : c
Explanation : In this program, We are reading the number of characters in the program by using the function feof.
Output:
$ g++ out4.cpp
$ a.out
162
Related Article : Quiz on File Handling
Similar Reads
Output of C programs | Set 46 QUE.1 What would be output following C program? C #include <stdio.h> int main() { char* str = "IncludeHelp"; printf("%c\n", *&*str); return 0; } OPTION a) Error b) IncludeHelp c) I d)*I Answer: c Explanation : & is a reference operator, * is de-reference operator. W
3 min read
Output of C programs | Set 42 QUE.1 What is the output of following program? C #include <stdio.h> int main() { int x = 10, *y, **z; y = &x; z = &y; printf("%d %d %d", *y, **z, *(*z)); return 0; } a. 10 10 10 b. 100xaa54f10 c. Run time error d. No Output Answer : a Explanation: Because y contains the addre
3 min read
Output of C programs | Set 45 1. What will be the output of following program? C #include<stdio.h> void main() { int i = 10; static int x = i; if (x == i) printf("equal"); else if (x < i))) printf("less than"); else printf("greater than"); } Options: (a)equal (b)greater than (c)less than (d
2 min read
Output of C programs | Set 43 1. What is the output of following program? C #include <stdio.h> int main() { int a = 1, b = 2, c = 3; c = a == b; printf("%d", c); return 0; } Choose the correct answer: (A) 0 (B) 1 (C) 2 (D) 3 Answer : (A) Explanation : "==" is relational operator which returns only two values, eit
3 min read
Output of C programs | Set 48 1. What is the output of following program? C #include <stdio.h> #define square(x) (x * x) int main() { int x, y = 1; x = square(y + 1); printf("%d\n", x); return 0; } Options: (A) Error (B) 4 (C) 3 (D) Garbage value Answer : (C) Explanation : The macro function square(x)(x*x) calcul
3 min read