统计文件字符数

onion____ 2009-12-13 01:16:02
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i=0;
char ch,filename[256];
FILE *in,*out;
if((in=fopen("123.txt","r"))==NULL) \\输入文件名 复制123.TXT
{
printf("cannot open the file");
exit(0);
}
printf("Enter the name of copy\n"); \\输入文件名 复制123.TXT
scanf("%s",filename);
if((out=fopen(filename,"w"))==NULL)
{
printf("cannot open the file");
exit(0);
}
while(!feof(in))
{
fputc(fgetc(in),out);
}
fclose(out);
if((out=fopen(filename,"w"))==NULL)
{
printf("cannot open the file");
exit(0);
}
ch=fgetc(out);
while(ch!=EOF)
{
i+=1;
}
printf("%d\n",i);
fclose(in);
fclose(out);

return 0;
}

输入文件名 复制123.TXT 输入文件名 复制123.TXT 然后想统计TXT中字符个数 为何不能实现?
...全文
35 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
deng1243 2009-12-13
  • 打赏
  • 举报
回复

ch=fgetc(out);
while(ch!=EOF)
{
if(ch!='\n')
i+=1;
ch=fgetc(out);
}

这样不统计每行的最后一个回车符
vanchristin 2009-12-13
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0;
char ch,filename[256];
FILE *in,*out;
if((in=fopen("123.txt","r"))==NULL) //输入文件名 复制123.TXT
{
printf("cannot open the file");
exit(0);
}
printf("Enter the name of copy\n"); //输入文件名 复制123.TXT
scanf("%s",filename);
if((out=fopen(filename,"w"))==NULL)
{
printf("cannot open the file");
exit(0);
}
while(!feof(in))
{
fputc(fgetc(in),out);
i++;
}
fclose(out);
if((out=fopen(filename,"w"))==NULL)
{
printf("cannot open the file");
exit(0);
}
ch=fgetc(out);
// while(ch!=EOF)
// {
// i+=1;
// }
printf("%d\n",i);
fclose(in);
fclose(out);

return 0;
}
deng1243 2009-12-13
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0;
char ch,filename[256];
FILE *in,*out;
if((in=fopen("123.txt","r"))==NULL) //输入文件名 复制123.TXT
{
printf("cannot open the file");
exit(0);
}
printf("Enter the name of copy\n"); //输入文件名 复制123.TXT
scanf("%s",filename);
if((out=fopen(filename,"w"))==NULL)
{
printf("cannot open the file");
exit(0);
}
while(!feof(in))
{
fputc(fgetc(in),out);
}
fclose(out);
if((out=fopen(filename,"r"))==NULL) //"r",以读的方式打开
{
printf("cannot open the file");
exit(0);
}
ch=fgetc(out);
while(ch!=EOF)
{
i+=1;
ch=fgetc(out);
}
printf("%d\n",i);
fclose(in);
fclose(out);
return 0;
}
vanchristin 2009-12-13
  • 打赏
  • 举报
回复
while(ch!=EOF)
{
i+=1;
}
这一句是什么?
死循环吗?

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧