0% found this document useful (0 votes)
4 views2 pages

Readwriteinfile

This C program allows the user to input the names and marks of a specified number of students, which are then stored in a file named 'std.txt'. After writing the data to the file, the program reads the information back and displays it on the console. Error handling is included for file operations to ensure proper execution.

Uploaded by

bikalp0000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Readwriteinfile

This C program allows the user to input the names and marks of a specified number of students, which are then stored in a file named 'std.txt'. After writing the data to the file, the program reads the information back and displays it on the console. Error handling is included for file operations to ensure proper execution.

Uploaded by

bikalp0000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

//C Program to read name and marks of n students and store it in file std.

txt and
//read name and marks of students from same file
#include <stdio.h>
#include<stdlib.h>
int main() {
char name[50];
int marks,i,n;
printf("Enter number of students: ");
scanf("%d",&n);
FILE *fptr;
fptr=(fopen("std.txt","w"));
if(fptr==NULL) {
printf("Error!");
exit(1);
}
for (i=0;i<n;++i) {
printf("Enter name: ");
scanf("%s",name);
printf("Enter marks: ");
scanf("%d",&marks);
fprintf(fptr,"%s \t %d \n", name,marks);
}
fclose(fptr);
fptr = fopen("std.txt", "r");
if (fptr == NULL) {
printf("Error: Could not open file");
exit(1);
}
printf("Name\tMarks\n");
while (fscanf(fptr, "%s %d", name, &marks) != EOF){
printf("%s\t%d\n", name, marks);
}
fclose(fptr);
return 0;
}

You might also like