| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 264 人关注过本帖
标题:关于文件的问题
只看楼主 加入收藏
cqu0914
Rank: 1
等 级:新手上路
帖 子:4
专家分:2
注 册:2012-11-1
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
关于文件的问题
我写的这个程序,可是当我输入完必要的数据后,为什么还要在输一次才能显示结果?此外,如果,我想把程序运行结果放在某个特定的文件里,该怎么办?嗯,还有想用c做一个搜索应用,怎么存必要的数据,我该如何做一个文件呢?求大家的解答,感谢。
#include<stdio.h>
#include<stdlib.h>
int  main()
{struct stu_data
{int num;
char name[20];
char addr[20];
int text;
}stu[3];
FILE *fp;
int i,j;
fp=fopen("studentdata.txt","w");
if(fp==NULL)
{printf("cannot open the file\n");
return 0;
}
for(i=0;i<3;i++)
{scanf("%d%s%s%d\n",&stu[i].num,&stu[i].name,&stu[i].addr,&stu[i].text);
fwrite(&stu[i],sizeof(struct stu_data),1,fp);
}
fclose(fp);
fp=fopen("studentdata.txt","r");
if(fp==NULL)
{printf("cannot open the file\n");
return 0;
}
for(j=0;j<3;j++)
{fread(&stu[j],sizeof(struct stu_data),1,fp);
printf("%-15d%-20s%-20s%-5d\n",stu[j].num,stu[j].name,stu[j].addr,stu[j].text);
}
fclose(fp);
system("pause");
return 0;
}
搜索更多相关主题的帖子: include return cannot 
2013-01-07 00:51
cqu0914
Rank: 1
等 级:新手上路
帖 子:4
专家分:2
注 册:2012-11-1
收藏
得分:0 
哪位大神能给我给我回复?感谢啦

我思故我在。
2013-01-07 11:52
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:5 
看看c里面关于文件的操作,这种类似的例子很多

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2013-01-08 19:13
a151141
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:197
专家分:680
注 册:2012-10-19
收藏
得分:10 
程序有错误:
#include<stdio.h>
 #include<stdlib.h>
 int  main()
 {struct stu_data
 {int num;
 char name[20];
 char addr[20];
 int text;
 }stu[3];
 FILE *fp;
 int i,j;
 fp=fopen("studentdata.txt","w");
 if(fp==NULL)
 {printf("cannot open the file\n");
 return 0;
 }
 for(i=0;i<3;i++)
 {scanf("%d%s%s%d\n",&stu[i].num,&stu[i].name,&stu[i].addr,&stu[i].text);      /*这句有错误,字符串的输入只需其首地址即可,即stu[i].name,stu[i].add前不加&  */
 fwrite(&stu[i],sizeof(struct stu_data),1,fp);
 }
 fclose(fp);
 fp=fopen("studentdata.txt","r");
 if(fp==NULL)
 {printf("cannot open the file\n");
 return 0;
 }
 for(j=0;j<3;j++)
 {fread(&stu[j],sizeof(struct stu_data),1,fp);
 printf("%-15d%-20s%-20s%-5d\n",stu[j].num,stu[j].name,stu[j].addr,stu[j].text);
 }
 fclose(fp);
 system("pause");
 return 0;
 }

[ 本帖最后由 a151141 于 2013-1-8 19:22 编辑 ]

世界上幸福的事就是抓到一只羊,更幸福的事就是抓到两只羊……
2013-01-08 19:19
a151141
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:197
专家分:680
注 册:2012-10-19
收藏
得分:5 
其实你的函数可以创建一个文本,只不过和你编写的这个程序的cpp.c在一块。
如下程序可以在D盘创建一个名为std的txt文本文档
#include<stdio.h>
main()
{
    FILE *fp;
    fp=fopen("D:\\std.txt","w");
}


世界上幸福的事就是抓到一只羊,更幸福的事就是抓到两只羊……
2013-01-08 19:34
快速回复:关于文件的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017206 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved