C Program To Read A File


♣ Try it Out ::
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 50

main()
{
    FILE *fp;
    char ch, fn[MAX];

    printf("Type a file name which you wish to see: ");
    gets(fn);

    fp = fopen(fn, "r");

    if(fp == NULL)
    {
        printf("\nError! File does not exist.");
        getch();
        exit(1);
    }

    printf("The file: %s contains: \n\n", fn);

    while((ch=fgetc(fp)) != EOF)
        putc(ch, stdout);

    fclose(fp);

    getch();
    return(0);
}

♣ Output ::
Type a file name which you wish to see: file1.txt
The file: file1.txt contains:

Nurul Amin Muhit
Student of Leading University
B.Sc [engg.] in CSE

♣ Downloads ::

Comments

Popular posts from this blog

C++ :: Topological Sort Algorithm (using DFS)

How to Hack Facebook Account

C++ :: Strongly Connected Components Algorithm (SCC)