Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Output of C programs | Set 40 (File handling)

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Prerequisite : File handling
1. What is the output of this program by manipulating the text file?




#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?




#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?




#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?




#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?




#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

This article is contributed by Ajay Puri. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Last Updated : 18 Aug, 2017
Like Article
Save Article
Similar Reads