Index « Previous Next »

Question

Write a program which read a text file and display it contents on screen. Your program should asked the file name from user.

Source Code

#include <stdio.h>

int main()
{
    char in_name[80];
    FILE *in_file;
    int ch;

    printf("Enter file name:\n");
    scanf("%s", in_name);

    in_file = fopen(in_name, "r");

    if (in_file == NULL)
    {
        printf("Can't open %s for reading.\n", in_name);
    }
    else
    {
        while ((ch = fgetc(in_file)) != EOF)
        {
            printf("%c", ch);
        }
        fclose(in_file);
    }
    return 0;
}

Output

Enter file name:
sample.txt
Hello,

This is a sample text file.
:)