You are on page 1of 4

Parth Sunil Chavan

20BCI0055
CSE2010
Slot L25 + L26
Lab Assessment 3

1. Write a C program to read regno., name and marks of n number of students


and store them in a text file.

C Code:
#include<stdio.h>
#include<stdlib.h>

struct Student_data
{
char reg_no[10];
char name[20];
int marks;
};
int main()
{
int n;
printf("Enter the number of Students: ");
scanf("%d",&n);
struct Student_data s[n];

FILE *fp;
fp=fopen("student.txt","w");
for(int i=0;i<n;i++)
{
printf("\nEnter Reg. No.: ");
scanf("%s",s[i].reg_no);
printf("Enter Name: ");
scanf("%s",s[i].name);
printf("Enter Marks: ");
scanf("%d",&s[i].marks);

}
for(int j=0;j<n;j++)
{
fprintf(fp,"Reg. No.: %s\t\t",s[j].reg_no);
fprintf(fp,"Name: %s\t\t",s[j].name);
fprintf(fp,"Marks: %d\n",s[j].marks);
}

fclose(fp);

Output:
2. Write a C program to print the m-n lines from a file? Where m and n line
numbers?

C Code:
#include<stdio.h>
#include<string.h>

int main()
{
FILE *fp;
fp=fopen("file1.txt","r");
int m,n;
printf("Enter m: ");
scanf("%d",&m);
printf("Enter n: ");
scanf("%d",&n);
char ch;
int count=0;
while(!feof(fp))
{
fscanf(fp,"%c",&ch);
if(ch=='\n')
{
count++;}

if(count>=m-1 && count<=n-1)


{
printf("%c",ch);
}

Output:

You might also like