| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 604 人关注过本帖
标题:帮忙! 自己写的计数函数出错!
只看楼主 加入收藏
Hellish
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2007-2-28
收藏
 问题点数:0 回复次数:4 
帮忙! 自己写的计数函数出错!
#include<stdio.h>
#include<iostream.h>
void main()
{ int c=0;
FILE *fp;
if((fp=fopen("c:\\count.txt","wt+"))==NULL)
{
cout<<"can not open file"<<endl;
getchar();
}
c=fgetc(fp);
c++;
fputc(c,fp);
cout<<c<<endl;
fclose(fp);
}
能通过编译,但是文件不能写入, 达人帮忙啊!
搜索更多相关主题的帖子: 函数 计数 
2007-03-15 22:49
Hellish
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2007-2-28
收藏
得分:0 
那位能帮帮忙啊,
2007-03-15 23:14
cfnxy
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2007-2-17
收藏
得分:0 

楼主什么意思啊?是想从一个文件中读出字符再写入原文件吗?本来那个文件就是空的,您什么也没读出来,当然什么也不会写入了。而且要有一个循环语句才能不停的读和写啊。
#include<stdio.h>
#include<iostream.h>
void main()
{ int c=0;char cc;
FILE *fp;
if((fp=fopen("c:\\count.txt","wt+"))==NULL)
{
cout<<"can not open file"<<endl;
getchar();
}
c=fgetc(fp);
c++;
while ((cc=getchar())!='\n')
fputc(cc,fp);
cout<<c<<endl;
fclose(fp);
}

2007-03-17 09:13
litcatyx
Rank: 1
等 级:新手上路
威 望:1
帖 子:151
专家分:0
注 册:2006-10-27
收藏
得分:0 
int main()
{
const int MAXLEN=100;
const char filename[]="f:\\count.txt";

int count=0;
char buf[MAXLEN]={'0','\0'};

FILE *fp=0;
if(fp=fopen(filename,"r"))
{
char c;
while((c=fgetc(fp))!=EOF)
count=count*10+c-'0';
++count;
itoa(count,buf,10);
fclose(fp);
}
if(fp=fopen(filename,"w"))
{
char *p=buf;
while(*p!='\0')
fputc(*p++,fp);
fclose(fp);
}
else
{
cout<<"Can't not open file!"<<endl;
}
return 0;
}

2007-03-17 09:44
Hellish
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2007-2-28
收藏
得分:0 
以下是引用cfnxy在2007-3-17 9:13:06的发言:

楼主什么意思啊?是想从一个文件中读出字符再写入原文件吗?本来那个文件就是空的,您什么也没读出来,当然什么也不会写入了。而且要有一个循环语句才能不停的读和写啊。
#include<stdio.h>
#include<iostream.h>
void main()
{ int c=0;char cc;
FILE *fp;
if((fp=fopen("c:\\count.txt","wt+"))==NULL)
{
cout<<"can not open file"<<endl;
getchar();
}
c=fgetc(fp);
c++;
while ((cc=getchar())!='\n')
fputc(cc,fp);
cout<<c<<endl;
fclose(fp);
}

我的意思是生成执行文件后, 点击一次(调用一次), 文件里面的数就自动加一, 而不是将输入的数保存在文件中

2007-04-01 08:48
快速回复:帮忙! 自己写的计数函数出错!
数据加载中...
 
   



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

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