๐Ÿ’ป
B.TECH I YEAR โ€ข I SEMESTER โ€ข R22

Programming for Problem Solving

C Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R22 Regulation ๐ŸŽ“ I Year ๐Ÿ“š I Semester ๐Ÿ’ป C Programming ๐Ÿงช Lab Program
Selected Program

Write a C program to display the contents of a file to standard output device.

โ† All Lab Programs

Write a C program to display the contents of a file to standard output device.

demo.txt

StudyGlance: A Fully loaded Notebook!

C Program
//to display the contents of a file to standard output device.
#include <stdio.h>

int main() {
    FILE *fp;          // Declare a file pointer
    char ch;             // To store each character read from the file

    // Open the file in read mode
    fp = fopen("demo.txt", "r");

    // Check if the file was opened successfully
    if (fp == NULL) {
        printf("Error: Could not open the file.\n");
        return 1; // Exit the program with an error code
    }

    // Read and display characters from the file until the end
    while ((ch = fgetc(fp)) != EOF) {
        putchar(ch);  // Output the character to the standard output
    }

    // Close the file
    fclose(fp);

    return 0;  
}

OUTPUT
StudyGlance: A Fully loaded Notebook!

๐Ÿ’ป Online C Compiler

Run and test this C program online. You can also edit the code before executing it.

๐Ÿ’ก Edit the program in the compiler and click Copy Program to copy the latest edited code.
โ† Back to PPS Lab Programs ๐Ÿ“š All Lab Manuals