//Bài 3.thao tác trên tập tin: tu sửa và thêm mẫu tin
///* Chuong trinh file truy nhap tuan tu - : tao,xem,sua,them vao cuoi file,cho phep chon ten file */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
struct danhsach
{
char ten[100];
int tuoi;
};
void nhap(FILE *t)
{
danhsach ds;
char s[3];
do
{
printf("ten: ");gets(ds.ten);
if(strlen(ds.ten)!=0)
{
printf("tuoi: ");gets(s);ds.tuoi=atoi(s);
fwrite(&ds,sizeof(ds),1,t);
}
}while(strlen(ds.ten)!=0);
}
void sua(FILE *t)
{
danhsach ds;
char s[3];
fseek(t,0,2);
int n=ftell(t)/sizeof(ds);
rewind(t);
for(int i=0;i<n;i++)
{
fseek(t,i*sizeof(ds),0);
fread(&ds,sizeof(ds),1,t);
printf("\nhien tai:\nten:%s\ntuoi:%d\n--sua=<enter>--ok=<0>\n",ds.ten,ds.tuoi);
char c=getch();
if(c!='0')
{
printf("ten:");gets(ds.ten);
printf("tuoi: ");gets(s);ds.tuoi=atoi(s);
fseek(t,i*sizeof(ds),0);
fwrite(&ds,sizeof(ds),1,t);
}
}
}
void xuat(FILE *t)
{
danhsach ds;
while(fread(&ds,sizeof(ds),1,t)!=0)
{
printf("\nten:%s\ntuoi:%d\n",ds.ten,ds.tuoi);
}
}
void main()
{
FILE *t;
t=fopen("E:\\bai3.txt","w+b");
nhap(t);
rewind(t);
sua(t);
rewind(t);
xuat(t);
fclose(t);
}
0 comments:
Post a Comment