新手简单的 改写 问题

huanhuan30 2009-01-05 04:30:42
例子1.。。。。。

#include <stdio.h>
#include <stdlib.h>
#define SIZE 256
int has_ch (char ch, const char *temp);
int main (int argc, char *argv[])
{
FILE *fp;
char temp[SIZE];
int i = 0;
char ch = argv[1][0];
if (argc != 3)
{
printf("filename s1 s2.....\n");
exit(1);
}
else
{
if ((fp = fopen(argv[2], "r")) == NULL)
{
printf("Can't open %s \n", argv[1]);
exit(2);
}
while (fgets(temp, SIZE, fp) != NULL)
{
if (has_ch(ch, temp))
{
fputs(temp, stdout);
}
}
}
fclose(fp);
return 0;
}
int has_ch (char ch, const char *line)
{
int i = 0;
while (line[i] != '\0')
{
if (ch == line[i])
{
return (1);
}
i++;
}
return 0;
}


例子2.。。。。。
#include <stdio.h>
#include <stdlib.h>
#define SIZE 256
int main (int argc, char *argv[])
{
FILE *fp;
char temp[SIZE];
int i = 0;
char ch = argv[1][0];
if (argc != 3)
{
printf("filename s1 s2.....\n");
exit(1);
}
else
{
if ((fp = fopen(argv[2], "r")) == NULL)
{
printf("Can't open %s \n", argv[1]);
exit(2);
}
while (fgets(temp, SIZE, fp) != NULL)
{

while (temp[i] != '\0')
{
if (temp[i] == ch)
{
fputs(temp, stdout);
i = 0;
break;
}
i++;
}

}
}
fclose(fp);
return 0;
}

例子1 为什么 不能 用 例子2 改写。。。。哪里错了。。。。。
...全文
63 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoyisnail 2009-01-05
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>
#define SIZE 256
int main (int argc, char *argv[])
{
FILE *fp;
char temp[SIZE];
int i = 0;
char ch = argv[1][0];
if (argc != 3)
{
printf("filename s1 s2.....\n");
exit(1);
}
else
{
if ((fp = fopen(argv[2], "r")) == NULL)
{
printf("Can't open %s \n", argv[1]);
exit(2);
}
while (fgets(temp, SIZE, fp) != NULL)
{
i=0;
while (temp[i] != '\0')
{
if (temp[i] == ch)
{
fputs(temp, stdout);
i = 0;
break;
}
i++;
}

}
}
fclose(fp);
return 0;
}
hhyttppd 2009-01-05
  • 打赏
  • 举报
回复
查找之前要重置
i = 0;
while (temp[i] != '\0')
...

69,371

社区成员

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

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