| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 330 人关注过本帖
标题:谁过来说一下如何读写文件。
只看楼主 加入收藏
★☆
Rank: 2
来 自:北京
等 级:禁止访问
帖 子:23
专家分:11
注 册:2009-10-16
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
谁过来说一下如何读写文件。
这是我写的代码,说一下为什么,不能正确的读出来我录入的信息。
还有怎么才能让这个程序每次运行都不删除次录入信息,谢谢
#include<stdio.h>
main()
{
int year,month,day;
char note;
FILE*fp;
fp=fopen;

if((fp=fopen("d:\\notebook.txt","w"))==NULL)
{puts("This file can not be opened");exit(0);}
fscanf(stdin,"%d%d%d%s",year,month,day,&note);
fprintf(fp,"%d%d%d%s",year,month,day,note);
fclose(fp);





if((fp=fopen("d:\\notebook.txt","r"))==NULL)
{gets("This input file can not be opened");exit(0);}
fscanf(fp,"%d%d%d%ls",year,month,day,&note);
fprintf(stdout,"%d %d %d   %s",year,month,day,note);
fclose(fp);
















getch();
}
搜索更多相关主题的帖子: 文件 
2009-10-20 20:46
m456m654
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:3
帖 子:783
专家分:2806
注 册:2009-9-17
收藏
得分:14 
#include<stdio.h>
main()
{
int year,month,day;
char note[100];
FILE*fp;
if((fp=fopen("d:\\notebook.txt","w"))==NULL)
   {
    puts("This file can not be opened");
    exit(0);
    }
fscanf(stdin,"%d%d%d%s",&year,&month,&day,note);//因为是格式输入,所以note对应的字符串中不能含有空格
fprintf(fp,"%d%d%d%s",year,month,day,note);
fclose(fp);
if((fp=fopen("d:\\notebook.txt","r"))==NULL)
   {
    gets("This input file can not be opened");
    exit(0);
    }
fscanf(fp,"%d%d%d%ls",&year,&month,&day,note);
fprintf(stdout,"%d %d %d   %s",year,month,day,note);//结果可以写到相应txt文档中去,但Tc运行的结果year怎么老是不对呢?
fclose(fp);

getch();
}


[ 本帖最后由 m456m654 于 2009-10-20 21:31 编辑 ]
2009-10-20 21:20
★☆
Rank: 2
来 自:北京
等 级:禁止访问
帖 子:23
专家分:11
注 册:2009-10-16
收藏
得分:0 
我希望解决的就是tc运行输出数字为什么总是不对

多多指教
2009-10-20 21:34
m456m654
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:3
帖 子:783
专家分:2806
注 册:2009-9-17
收藏
得分:0 
不懂文件操作
2009-10-20 21:39
★☆
Rank: 2
来 自:北京
等 级:禁止访问
帖 子:23
专家分:11
注 册:2009-10-16
收藏
得分:0 
我想到了个数字输出正确的方法可是不能实现我想要的结果,char year,month,day;就可以正确的输出我真服了为

多多指教
2009-10-20 21:40
快速回复:谁过来说一下如何读写文件。
数据加载中...
 
   



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

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