How to write a C program to input student details into a file for two subjects in C Programming Language ?
Solution:
/*Entry of student's details into a file for two subjects */
#include <stdio.h>
#define HIGH_MARK 100
#define SIZE 2
int main()
{
FILE *fp;
char fname[10];
int i,total,n;
float avg,percent;
struct student
{
char name[10];
int rollno;
char subject1[10];
int s1marks,s2marks;
char subject2[10];
};
struct student s[10];
printf("\nEnter the name of the file\n");
scanf("%s",fname);
printf("Enter the number of student's details you want to enter\n");
scanf("%d",&n);
fp=fopen(fname, "wa+");
if(fp == NULL)
{
printf("\nThe file open attempt was unsuccessful\n");
}
for(i=0;i<n;i++)
{
printf("\n\t\tSTUDENT RECORD\t\t\nEnter\n\nName:\t");
scanf("%s",s[i].name);
printf("\nRollno:\t");
scanf("%d",&s[i].rollno);
printf("\nSubject-1:");
scanf("%s",s[i].subject1);
printf("\nMarks:\t");
scanf("%d",&s[i].s1marks);
printf("\nSubject-2:");
scanf("%s",s[i].subject2);
printf("\nMarks:\t");
scanf("%d",&s[i].s2marks);
fprintf(fp,"\n\t\tSTUDENT RECORD\t\t\nENTER\n\nNAME:\t\t");
fprintf(fp,"%s",s[i].name);
fprintf(fp,"\nROLL NO:\t");
fprintf(fp,"%d",s[i].rollno);
fprintf(fp,"\nSUBJECT-1:\t");
fprintf(fp,"%s",s[i].subject1);
fprintf(fp,"\nMARKS:\t\t");
fprintf(fp,"%d",s[i].s1marks);
fprintf(fp,"\nSUBJECT-2:\t");
fprintf(fp,"%s",s[i].subject2);
fprintf(fp,"\nMARKS:\t\t");
fprintf(fp,"%d\n",s[i].s2marks);
total=((s[i].s1marks)+(s[i].s2marks));
avg=total/SIZE;
percent=((((float) total)/(SIZE*HIGH_MARK))*100);
fprintf(fp,"\nThe total of all the marks the student obtained is:\t");
fprintf(fp,"%d",total);
fprintf(fp,"\nThe average of the marks obtained is:\t");
fprintf(fp,"%f\n",avg);
fprintf(fp,"\nThe percentage obtained is ");
fprintf(fp,"%f% \n",percent);
if(percent > 80)
{
fprintf(fp,"\nThe student passed in distinction\n\n");
}
else if(percent <50)
{
fprintf(fp,"\nThe student is Failed\n");
}
}
}
Learn More :
File Handling
File
- COPY DATA FROM ONE FILE TO ANOTHER FILE USING C PROGRAM
- C Program File Input & Output Example
- C Program To Destruc Self Execution File ?
- C Program To Store Students Record In A Text File
- C Program To Write Data In A File And Search Data From File
- Make a copy of file given in argv[1] to file given in argv[2]
- C Program to copy a file to another file
- C Function to read instructions from the file and obey them
- Client/Server C program to make client send the name of a file
- A small I/O bound program to copy N bytes from an input file to an output file.
- C Program to Read and Write FIFO
- File Number Control C Program Example
- C Program to opens and reads dictionary file specified in spellrc
- C Program to Recursively converts all file-names to lower-case
- C Program to Find the Size of File using File Handling Function
- C Program Recursive function that searches for a given file in a given folder
- C Program Read a char & print next char ( file to file )
- Identifies the architecture Windows PE (exe or dll) file was compiled C Program
- File Handling (console to file) in C Program
Two
- Write a c program to find out H.C.F. of two numbers.
- Write a C program to find maximum or equal between two numbers ?
- ADDITION OF TWO MATRICES USING C PROGRAM
- C Program To Find Product Of Two No Using MACRO
- C Program To Swap Two Numbers Without Using Third Variable
- C Program Concatenating Two Strings Into A Third System
- C Program to concatenate two strings without using string functions
- C Program to Interchanging Two Numbers
- C Program produsul scalar a doi vectori .
- C Program The dot product of two vectors
- C Program to two timing functions to measure process time
- C Program to calculate sum of two m*n matrices & store the result in 3 matrix
- C Program to swap the values of two variables by using call by reference
- Find the union and intersection of two sets of integer store it in two array C Program
- Menu driven program in C to Calculate Length, Copy into Another Compare Concatenate of Two String
- C Program To Multiply Two Polynomials
- Create Two Singly Linked List Perform Differences Display It C Program
- Adding two polynomial functions C Program Using Structure
- Program to Add Two Polynomials Using Linked List C Program
- Menu Driven Program to Read Two Integers Find Sum, Difference and Product C Program
- Input Two Numbers and Print Greater Number C Program
Student
Input
- C Program to Get User Input and Compare
- C Program Basic logging of user input/output
- C Program File Input & Output Example
- C Program To Input & Print More Than One Words In Single Line
- Take microphone input and send to headphones
- A small I/O bound program to copy N bytes from an input file to an output file.
- C Program That in a Given Natural Number x Insert Figure c at Position p
- Input reads in the array positions and range for random generation.
- C Program that prompts the user to input a string, (whitespaces allowed)
- C Program to Input Number by the user between 1-100
- How to Gets User Input For Height in C Programming
- DEMULTIPLEXER CO ANALOG INPUTS
- C Program to Simulation program for FCFS ( First Come First Serve )
- Program to Display Pie Chart Accepting User Input C Program
- Input a Number and Check if it's Palindrome or not C Program
- Input Number and Print it's Reverse C Program
- Input Number and Check if it's Armstrong C Program
- Input Number and Check if it's Even or Odd C Program
- Input Two Numbers and Print Greater Number C Program
- Input Number and Calculate Sum of it's Digits C Program
Store
- C Program To Store Students Record In A Text File
- C Program To Store Students Record Using Structure
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- C Program to calculate sum of two m*n matrices & store the result in 3 matrix
- C Program to accept n numbers from user store these numbers into an array & calculate average of n numbers
- C Program to accept n numbers & store all prime numbers in an array & display this result
- C program to accept n number from user,store these numbers into an array & count the no of occurrences of each number
- C Program to accept n numbers from user store these numbers into an array & reverse an array elements using function
- C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order
- C Program to accept n numbers from user,store these numbers into an array & sort the number of an array
- C Program to accept n numbers from user, store these numbers into an array. Find out Maximum & Minimum number from an Array
- C Program to accept data from user store that data into text file
- Find the union and intersection of two sets of integer store it in two array C Program
- Accept n number from user, store these number into an array and calculate the average of n number
- C Program - Hash Table to store information about a student