0% found this document useful (0 votes)
56 views

File Creation, Reading From and Writing To File:: CSE115L - Computing Concepts Lab Lab 15

The document discusses file input/output in C programming. It includes examples of opening, reading, writing, appending, and closing files. It also discusses reading/writing structures from files and printing selected data based on department. It provides 3 problems: 1) read student data from a CSV file and print it, 2) print students by department, and 3) save students by department to a new file.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

File Creation, Reading From and Writing To File:: CSE115L - Computing Concepts Lab Lab 15

The document discusses file input/output in C programming. It includes examples of opening, reading, writing, appending, and closing files. It also discusses reading/writing structures from files and printing selected data based on department. It provides 3 problems: 1) read student data from a CSV file and print it, 2) print students by department, and 3) save students by department to a new file.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CSE115L – Computing Concepts Lab

Lab 15

File creation, reading from and writing to file:

#include<stdio.h>
int main() #include<stdio.h>
{ #include<string.h>
FILE *fp; int main()
fp=fopen("test.txt","r"); {
if(fp != NULL) FILE *fp;
{ fp=fopen("test.txt","r");
printf("File has been opened"); char c;
fclose(fp); while((c=getc(fp))!=EOF)
} putchar(c);
else printf("File not found")
return 0; fclose(fp);
} return 0;
#include<stdio.h> }
#include<string.h>
int main()
{
FILE *fp;
char buffer[30];
strcpy(buffer,"File has been created");
fp=fopen("test.txt","w");
fprintf(fp,buffer);
fclose(fp);
return 0;
}

Input from and output to a file:

#include <stdio.h> #include <stdio.h>


struct s int main()
{ {
char name[50]; char name[50];
int height; int marks,i,n;
}; printf("Enter number of students: ");
int main() scanf("%d",&n);
{ fflush(stdin);
struct s a[5]; FILE *fptr;
FILE *fptr; fptr=(fopen("student.txt","a"));
int i; if(fptr!=NULL)
fptr=fopen("file.txt","r"); {
for(i=0;i<5;++i) for(i=0;i<n;++i)
{ {
fgets(a[i].name, 50, fptr); printf("For student%d\n",i+1);
fscanf(fptr,"%d",&a[i].height); printf("Enter name: ");
getc(fptr); gets(name);
} printf("Enter marks: ");
for(i=0;i<5;++i) scanf("%d",&marks);
{ fflush(stdin);
printf("%d:\nName: %s\n",i+1,a[i].name); fprintf(fptr,"Name: %s\n",name);
printf("Height: %d\n",a[i].height); fprintf(fptr,"Marks: %d\n",marks);
} }
fclose(fptr); fclose(fptr);
} }

return 0;
}
Appending a file:

#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
fp=fopen("append.txt","a");
fprintf(fp,"Append mode");
fclose(fp);
return 0;
}

Problems:

1. Write a program that will read names, ids, dept names, and cgpa of some students from a file and will show
the results. Consider that the name of the file is ‘input.csv’. It is just a text file where each line holds
information of one student. Example format of two lines in the file is as follows:

Moinul Islam, 21, Computer Science, 3.98


Abu Hena, 11, Finance, 3.45

Read all information from the file, and print them on screen (one student per line). Your program should use
the following structure to hold information of a student

typedef struct
{
char name[50];
int id;
char dept[20];
double cgpa;
} student;

2. Write a function void pintByDept(char *deptName , student allStudents[], int size) that will print the
information of only those students who belong to the department with name pointed to by deptName. The
array allStudent is of length ‘size’ and holds information of all the students.

3. Write a function void saveByDept(char *fileName, char *deptName , student allStudents[], int size) that
will save the information of all students who belong to the department with name pointed to by deptName
into a text file. The name of the file is given as an input parameter, fileName.

You might also like