怎么统计某个字符在 文件中的所有位置

etwtwett 2012-08-09 03:23:47
想请大家帮忙写个c程序,就是统计某个字符在一个文本文件出现的位置,如在11.txt这个文本文件中的字符是"kjdfvfkjrtreyghdgkdgothjd",字符“k”在文本文件中的位置是1,7,19.就是统计“k”的位置,只输出向1,7,19这些位置数据,我之前尝试写过,编译,连接都正确但运行时会自动关闭,请各位大侠帮忙写个,谢啦
...全文
64 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
夏天__ 2012-08-09
  • 打赏
  • 举报
回复
 feof(fp)有两个返回值:如果遇到文件结束,函数feof(fp)的值为非零值,否则为0。

学习了。
夏天__ 2012-08-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

C/C++ code

#include <stdio.h>

int main(int argc, char* argv[])
{
FILE* file;
char ch;
int count = 1;

file = fopen("8.txt", "r");
if(file == NULL)
{
return -1;
……
[/Quote]
楼上写的很好,
lylm 2012-08-09
  • 打赏
  • 举报
回复
LZ自己写的程序呢?
图灵狗 2012-08-09
  • 打赏
  • 举报
回复

#include <stdio.h>

int main(int argc, char* argv[])
{
FILE* file;
char ch;
int count = 1;

file = fopen("8.txt", "r");
if(file == NULL)
{
return -1;
}

while(!feof(file))
{
ch = fgetc(file);
if(ch == 'k')
{
printf("%d\n", count);
}
count++;
}

fclose(file);

return 0;
}

69,371

社区成员

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

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