Output of C programs | Set 40 (File handling)

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

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    if (remove("myfile.txt") != 0)
        perror("Error");
    else
        puts("Success");
    return 0;
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.