Structures and Files
Structures and Files
getch();
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int roll;
char name[50];
float marks;
};
int main(void)
{
struct student s;
system("clear");
printf("Enter Roll:");
scanf("%d",&(s.roll));
printf("Enter Name:");
scanf("%s",&(s.name));
printf("Enter Marks:");
scanf("%f",&(s.marks));
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",s.roll,s.name,s.marks);
return 0;
}
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",ptr->roll,ptr->name,ptr->marks);
return 0;
}
Creating the array of structures and accessing the structure array elements
using . Dot operator.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int roll;
char name[50];
float marks;
};
int main(void)
{
int i,n;
struct student s[50];
system("clear");
printf("Enter the number of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Roll %d:",i+1);
scanf("%d",&(s[i].roll));
printf("Enter Name %d:",i+1);
scanf("%s",&(s[i].name));
printf("Enter Marks %d:",i+1);
scanf("%f",&(s[i].marks));
}
for(i=0;i<n;i++)
{
printf("Student Record%d\n",i+1);
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",s[i].roll,s[i].name,s[i].marks);
}
return 0;
}
Creating the array of structures and accessing the structure array elements
using Arrow operator.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int roll;
char name[50];
float marks;
};
int main(void)
{
int i,n;
struct student s[50];
system("clear");
Creating the array of structures and accessing the structure array elements
using pointer (without using Arrow operator).
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
int roll;
char name[50];
float marks;
};
int main(void)
{
int i,n;
struct student s[50];
system("clear");
printf("Enter the number of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter Roll %d:",i+1);
scanf("%d",&((*(s+i)).roll));
printf("Enter Name %d:",i+1);
scanf("%s",&((*(s+i)).name));
printf("Enter Marks %d:",i+1);
scanf("%f",&((*(s+i)).marks));
}
for(i=0;i<n;i++)
{
printf("Student Record%d\n",i+1);
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",(*(s+i)).roll,(*(s+i)).name,
(*(s+i)).marks);
}
return 0;
}
float marks;
} goru;
int main(void)
{
int i,n;
goru *s;
system("clear");
printf("Enter the number of students:");
scanf("%d",&n);
s=(goru *)malloc(n*sizeof(goru));
for(i=0;i<n;i++)
{
printf("Enter Roll %d:",i+1);
scanf("%d",&((*(s+i)).roll));
printf("Enter Name %d:",i+1);
scanf("%s",&((*(s+i)).name));
printf("Enter Marks %d:",i+1);
scanf("%f",&((*(s+i)).marks));
}
for(i=0;i<n;i++)
{
printf("Student Record%d\n",i+1);
printf("Roll=%d\nName=%s\nMarks=%2.2f\n",(*(s+i)).roll,(*(s+i)).name,
(*(s+i)).marks);
}
return 0;
}
FILE PROGRAMMING IN C
Program to read a text file & displaying the content of the file,#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int isFileExist(const char *fileName)
{
FILE *fp;
if(fp=fopen(fileName,"r"))
{
return 1;
fclose(fp);
}
else
{
return 0;
}
};
int main(void)
{
char file[50];
FILE *fp;
int count;
char ch;
printf("Enter file name:");
scanf("%s",file);
if(isFileExist(file))
{
if(fp=fopen(file,"r"))
{
for(count=0;((ch=getc(fp))!=EOF)&&(!feof(fp));count++)
{
printf("%c",ch);
}
printf("\n%d characters read",count);
fclose(fp);
}
else
{
fputs("Error Opening File!!!",stderr);
exit(1);
}
}
else
{
printf("File Does not Exist!!!");
}
return 0;
}
Program to read a text file & displaying the content of the file,#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SIZE 100
int isFileExist(const char *fileName)
{
FILE *fp;
if(fp=fopen(fileName,"r"))
{
return 1;
fclose(fp);
}
else
{
return 0;
}
};
int main(void)
{
char file[50];
FILE *fp;
int count;
char *str;
printf("Enter file name:");
scanf("%s",file);
if(isFileExist(file))
{
if(fp=fopen(file,"r"))
{
str=(char *)calloc(MAX_SIZE,sizeof(char));
for(count=0;!feof(fp);count++)
{
fscanf(fp,"%s",str);
printf("%s%c",str,getc(fp));
}
fclose(fp);
}
else
{
fputs("Error Opening File!!!",stderr);
exit(1);
}
}
else
{
printf("File Does not Exist!!!");
}
return 0;
}
Program to read a text file & displaying the content of the file,#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_SIZE 100
int isFileExist(const char *fileName)
{
FILE *fp;
if(fp=fopen(fileName,"r"))
{
return 1;
fclose(fp);
}
else
{
return 0;
}
};
int main(void)
{
char file[50];
FILE *fp;
int count;
char *str;
printf("Enter file name:");
scanf("%s",file);
if(isFileExist(file))
{
if(fp=fopen(file,"r"))
{
str=(char *)calloc(MAX_SIZE,sizeof(char));
for(count=0;fread(str,sizeof(str),1,fp)==1&&!feof(fp);count++)
{
printf("%s",str);
}
fclose(fp);
}
else
{
fputs("Error Opening File!!!",stderr);
exit(1);
}
}
else
{
printf("File Does not Exist!!!");
}
return 0;
}
int main(void)
{
char file[50]="e://student.txt";
FILE *fp;
int count,i,n;
stud *s;
printf("Enter the no of students:");
scanf("%d",&n);
if(isFileExist(file))
{
if(fp=fopen(file,"a+"))
{
s=(stud *)calloc(n,sizeof(stud));
for(i=0;i<n;i++)
{
printf("Enter roll %d:",i+1);
scanf("%d",&((*(s+i)).roll));
printf("Enter name %d:",i+1);
scanf("%s",&((*(s+i)).name));
printf("Enter marks %d:",i+1);
scanf("%f",&((*(s+i)).marks));
}
for(i=0;i<n;i++)
{
fprintf(fp,"Record %d\n%d\n%s\n%f\n",i+1,(*(s+i)).roll,(*(s+i)).name,(*(s+i)).marks);
}
fclose(fp);
}
else
{
fputs("Error Opening File!!!",stderr);
exit(1);
}
}
else
{
if(fp=fopen(file,"w+"))
{
s=(stud *)calloc(n,sizeof(stud));
for(i=0;i<n;i++)
{
printf("Enter roll %d:",i+1);
scanf("%d",&((*s).roll));
printf("Enter name %d:",i+1);
scanf("%s",&((*s).name));
printf("Enter marks %d:",i+1);
scanf("%f",&((*s).marks));
}
for(i=0;i<n;i++)
{
fprintf(fp,"Record %d\n%d\n%s\n%f\n",i+1,(*s).roll,(*s).name,(*s).marks);
}
fclose(fp);
}
else
{
fputs("Error Opening File!!!",stderr);
exit(1);
}
}
return 0;
}
};
int main(void)
{
char file[50]="e://student.txt";
FILE *fp;
int count,i,n;
stud *s;
char str[50];
printf("Enter the no of students:");
scanf("%d",&n);
if(isFileExist(file))
{
if(fp=fopen(file,"a+"))
{
s=(stud *)calloc(n,sizeof(stud));
for(i=0;i<n;i++)
{
printf("Enter roll %d:",i+1);
scanf("%d",&((*(s+i)).roll));
printf("Enter name %d:",i+1);
scanf("%s",&((*(s+i)).name));
printf("Enter marks %d:",i+1);
scanf("%f",&((*(s+i)).marks));
}
for(i=0;i<n;i++)
{
fwrite(s+i,sizeof(stud),1,fp);
}
printf("File saved successfully\n");
fclose(fp);
}
else
{
fputs("Error Opening File!!!",stderr);
exit(1);
}
}
else
{
if(fp=fopen(file,"w+"))
{
s=(stud *)calloc(n,sizeof(stud));
for(i=0;i<n;i++)
{
printf("Enter roll %d:",i+1);
scanf("%d",&((*s).roll));
Now save the structure elements into a file name called studentRecord.txt, if
the file exists and contains data previously show the previously saved data and
append new data into it.
#include<stdio.h>
#include<conio.h>
typedef struct student
{
int roll;
char name[30];
float marks;
} pranay;
int main(void)
{
int n,i,count=0;
clrscr();
pranay *s,*r;
FILE *fp;
if((fp=fopen("E:\\studentRecord.txt","a+"))==NULL)
{
fputs("File Error!!!",stderr);
exit(1);
}
else
{
r=(pranay *)calloc(10,sizeof(pranay));
rewind(fp);
for(i=0;fread(r+i,sizeof(pranay),1,fp)==1;i++)
{
printf("Student Record#%d\n",i+1);
printf("Roll:%d\n",(*(r+i)).roll);
printf("Name:%s\n",(*(r+i)).name);
printf("Marks:%3.2f\n",(*(r+i)).marks);
}
//Another way to perform this
/*while(fread(&r[count],sizeof(pranay),1,fp)==1)
{
count++;
}*/
count=i;
printf("File records=%d\n",count);
printf("Enter the no of students:");
scanf("%d",&n);
s=(pranay *)calloc(n,sizeof(pranay));
if(s!=NULL)
{
for(i=0;i<n;i++)
{
printf("Enter roll %d:",i+1);
scanf("%d",&((*(s+i)).roll));
printf("Enter name %d:",i+1);
scanf("%s",&((*(s+i)).name));
printf("Enter marks %d:",i+1);
scanf("%f",&((*(s+i)).marks));
}
for(i=0;i<n;i++)
{
fwrite(s+i,sizeof(pranay),1,fp);
}
}
fclose(fp);
}
getch();
return 0;
}
Now rewriting the above program using the same dynamic array of structures
to read from the file and also to append new data into the file and also
changing the type of the file (studentRecord.txt) from text file to binary file.
#include<stdio.h>
#include<conio.h>
#define MAX_STUDENT 50
typedef struct student
{
int roll;
char name[50];
float marks;
} pranay;
int main(void)
{
int n,i,count=0;
clrscr();
FILE *fp;
pranay *s;
s=(pranay *)calloc(MAX_STUDENT,sizeof(pranay));
if((s==NULL)||((fp=fopen("E:\\studentRecord.bin","a+b"))==NULL))
{
fputs("Error!!!",stderr);
exit(1);
}
else
{
rewind(fp);
for(i=0;fread(s+i,sizeof(pranay),1,fp)==1;i++)
{
printf("Student Record#%d\n",i+1);
printf("Roll:%d\n",(*(s+i)).roll);
printf("Name:%s\n",(*(s+i)).name);
printf("Marks:%3.2f\n",(*(s+i)).marks);
}
count=i;
printf("\n%d existing records retrieved\n",count);
printf("Enter the no of students:");
scanf("%d",&n);
if(n<=(MAX_STUDENT-count))
{
for(i=count;i<count+n;i++)
{
printf("Enter roll %d:",i+1);
scanf("%d",&((*(s+i)).roll));
printf("Enter name %d:",i+1);
scanf("%s",&((*(s+i)).name));
printf("Enter marks %d:",i+1);
scanf("%f",&((*(s+i)).marks));
}
fwrite(s+count,sizeof(pranay),n,fp);
}
else
{
printf("Exeeds intake\n");
}
}
fclose(fp);
getch();
return 0;
}