/*運行程序按1可以創(chuàng)建并且在一個文本文件里寫入保存數(shù)據(jù),再重新運行,按3,無法讀出,而且原文本文件的內(nèi)容都空了*/#include<stdio.h>#include<stdlib.h>#include<math.h>#define N 100struct product{ int num; ? ? ? ? ? ? ? ? ? ? ? //設(shè)備編號 char type[20]; ? ? ? ? ? ? //設(shè)備類 型};FILE *fp;void input(struct product *p);void scan(struct product *p);?int system(const char *string);int main(){ struct product pro[N]; int i; int number=0; if((fp=fopen("test1.txt","wt"))==NULL) { printf("不能打開文件.\n"); exit(1); } fwrite(&number,sizeof(int),1,fp); fclose(fp); printf("* 1.添加新設(shè)備 ? ? ? ? ? ? ? ? ? ? ? ?3.瀏覽設(shè)備信息 ? ? ? ? ?*\n"); printf("請用數(shù)字選擇你需要的功能:"); scanf("%d",&i); system("cls"); ? ? ? ? ? ? ? ? ?//清屏操作 switch(i) { case 1: input(pro); break; case 3: scan(pro); break; } return 0;}void input(struct product *p) ? ? ? ? ? ? ? //寫入{ char str[20]; int n; if((fp=fopen("test1.txt","wt+"))==NULL) { printf("不能打開文件.\n"); exit(1); } rewind(fp); fread(&n,sizeof(int),1,fp); n++; rewind(fp); fwrite(&n,sizeof(int),1,fp); fclose(fp); if((fp=fopen("test1.txt","at+"))==NULL) { printf("不能打開文件.\n"); exit(1); } printf("輸入設(shè)備編號:"); scanf("%s",str); p->num=atoi(str); fwrite(&(p->num),sizeof(int),1,fp); printf("輸入設(shè)備類型:"); scanf("%s",p->type); fwrite(p->type,sizeof(char),1,fp); fclose(fp);}void scan(struct product *p) ? ? ? ? ? ? ? //瀏覽 讀出{ int i,n; int num; char type[20]; if((fp=fopen("test1.txt","rt"))==NULL) { printf("error on delete!"); exit (1); } rewind(fp); fread(&n,sizeof(int),1,fp); for(i=0;i<n;i++) { fread(&num,sizeof(int),1,fp); fread(type,sizeof(char),1,fp); printf("%d ? ?%s ? \n",num,type); } fclose(fp);}
1 回答

慕少1414285
TA貢獻4條經(jīng)驗 獲得超0個贊
這個可以讀出
char ch;
ch=fgetc(fp);
? ? while(ch!=EOF)
{
? ? ? ? putchar(ch);
? ? ? ? ch=fgetc(fp);
? ? }
- 1 回答
- 0 關(guān)注
- 1804 瀏覽
添加回答
舉報
0/150
提交
取消