Computer Programming C Laboratory: File Operations C Programs

C Program to print the message "Ran out of data" and terminates further reading

C Programs using File Operations - using Error Handling

C Program to print the message "Ran out of data" and terminates further reading.

 /* C Program to prints the message "Ran out of data" and terminates further reading */

#include <stdio.h>

#include <conio.h>

void main()

{

/* Local definitions */

char *filename;

FILE *fp1, *fp2;

int i,number;

/* Statements */

clrscr();

fpl = fopen("TEST", "W"); /* file open function creates a new file for read/write operation */

for(i= 10;i<=100;i+=10)

putw(i,fpl);

fclose(fp1); /* file close function closes a filé associated with file pointer*/

printf("\nInput filename \n");

open_file:

scanf("%s", filename);

if((fp2 = fopen(filename, "r")) == NULL)

{

    printf("Cannot open the file. \n");

    printf("Type filename again. \n\n");

    goto open file;

} /* if */

else

for(i=1;i<= 20;i++)

{

    number = getw (fp2);

    if (feof(fp2))

    {

        printf("\nRan out of data. \n");

        break; /* break statement comes out of loop */

    } /* if */

    else

        printf("%d\n", number);

    } /* for */

fclose(fp2); /* file close */

} /* main */

OUTPUT

Input filename

TESTS

Cannot open the file.

Type filename again.

TEST

10

20

30

40

50

60

70

80

90

100

Ran out of data.

EXPLANATION: In this program fpl and fp2 are file pointers. The file TEST is opened and assigned to fpl. Then writes the data 10 to 100 using for loop and close the file pointer. The if statement checks the entered file name is correct or not if the entered file name is NULL then prints "cannot open the file", otherwise another if prints the result as "Ran out of data", while the if statement not satisfied then else block is executed and prints the data from the file as shown as output.


Computer Programming C Laboratory: File Operations C Programs : Tag: C Programming : - C Program to print the message "Ran out of data" and terminates further reading


Computer Programming C Laboratory: File Operations C Programs



Under Subject


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation