Read a Line From a File and Display it

Write a program to Read a Line From a File and Display it.

C Program

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char ch[2001];
    FILE *fp;
    fp = fopen("hello.txt""r");

    if (fp == NULL)
    {
        printf("File is  not opened\n");
        return -1;
    }

    //scan the first line of the file
    fscanf(fp"%[^\n]"ch);
    printf("First line of the file :\n%s"ch);
    fclose(fp);
    return 0;
}
Create hello.txt file in your workspace
hello.txt
Hello World 123 81782@62
Good Morning                                                                   

Output:

First line of the file :
Hello World 123 81782@62


No comments:

Post a Comment