Skip to main content

C Program to Read a Line From a File and Display it

 

Program to read text from a file

#include <stdio.h>
#include <stdlib.h> // For exit() function
int main() {
    char c[1000];
    FILE *fptr;
    if ((fptr = fopen("program.txt", "r")) == NULL) {
        printf("Error! opening file");
        // Program exits if file pointer returns NULL.
        exit(1);
    }

    // reads text until newline is encountered
    fscanf(fptr, "%[^\n]", c);
    printf("Data from the file:\n%s", c);
    fclose(fptr);

    return 0;
}

If the file is found, the program saves the content of the file to a string c until '\n' newline is encountered.

Suppose the program.txt file contains the following text in the current directory.

C programming is awesome.
I love C programming.
How are you doing? 

The output of the program will be:

Data from the file:
C programming is awesome.

If the file program.txt is not found, this program prints an error message.

Comments

Popular posts from this blog

C Program to Calculate Difference Between Two Time Periods

  Calculate Difference Between Two Time Periods # include <stdio.h> struct TIME { int seconds; int minutes; int hours; }; void differenceBetweenTimePeriod (struct TIME t1, struct TIME t2, struct TIME *diff) ; int main () { struct TIME startTime , stopTime , diff ; printf ( "Enter the start time. \n" ); printf ( "Enter hours, minutes and seconds: " ); scanf ( "%d %d %d" , &startTime.hours, &startTime.minutes, &startTime.seconds); printf ( "Enter the stop time. \n" ); printf ( "Enter hours, minutes and seconds: " ); scanf ( "%d %d %d" , &stopTime.hours, &stopTime.minutes, &stopTime.seconds); // Difference between start and stop time differenceBetweenTimePeriod(startTime, stopTime, &diff); printf ( "\nTime Difference: %d:%d:%d - ...

What is Cloud computing?

Cloud computing refers to the delivery of computing resources and services over the internet, allowing users to access and use applications, storage, and processing power without the need for local infrastructure or hardware. It involves the use of remote servers, typically hosted in data centers, to store, manage, and process data instead of relying on a local computer or server. In cloud computing, users can access a wide range of services and resources on-demand, which are usually provided by a cloud service provider (CSP). These services can include infrastructure resources (such as virtual machines, storage, and networking), platforms for developing and deploying applications, and software applications that can be accessed and used via the internet. Cloud computing offers several advantages over traditional on-premises computing models. It allows for scalability, where users can easily scale up or down their resource usage based on their needs, without having to invest in and mana...

C Program to Sort Elements in Lexicographical Order (Dictionary Order)

  Sort strings in the dictionary order # include <stdio.h> # include <string.h> int main () { char str[ 5 ][ 50 ], temp[ 50 ]; printf ( "Enter 5 words: " ); // Getting strings input for ( int i = 0 ; i < 5 ; ++i) { fgets(str[i], sizeof (str[i]), stdin ); } // storing strings in the lexicographical order for ( int i = 0 ; i < 5 ; ++i) { for ( int j = i + 1 ; j < 5 ; ++j) { // swapping strings if they are not in the lexicographical order if ( strcmp (str[i], str[j]) > 0 ) { strcpy (temp, str[i]); strcpy (str[i], str[j]); strcpy (str[j], temp); } } } printf ( "\nIn the lexicographical order: \n" ); for ( int i = 0 ; i < 5 ; ++i) { fputs (str[i], stdout ); } return 0 ; } Output Enter 5 words: R programming JavaScript Java C programming C++ programming In the lexicographical order: C programming C+...