Write a program that will read your personal information such as name, student id, and home district from the keyboard and save it into a filename myinfo.txt. Then write another program to read information from this txt file and display it as output of your program.
Solution:
Type1
#include<stdio.h>
int main()
{
FILE *file;
char name[30];
int id,num,i;
char district[15];
file=fopen("myinfo.txt","a");
if(file==NULL)
printf("not");
else
{
printf("File is open \n");
printf("Enter of integer number of student \n");
scanf("%d",&num);
for(i=0; i<num; i++)
{
printf("Enter student name\n");
scanf("%s",&name);
printf("Enter student ID\n");
scanf("%d",&id);
printf("Enter student district\n");
scanf("%s",&district);
fprintf(file,"%s\t\t%d\t\t%s\n",name,id,district);
}
fclose(file);
}
}
Solution type2 :
#include<stdio.h>
int main()
{
FILE *file;
char ch;
file=fopen("myinfo.txt","r");
if(file==NULL)
printf("not");
else
{
printf("File is open \n");
while(!feof(file))
{
ch=fgetc(file);
printf("%c",ch);
}
fclose(file);
}
}
0 comments:
Post a Comment