【发布时间】:2016-01-30 09:41:06
【问题描述】:
所以现在我正在尝试通过执行以下操作来处理标准输入和输入文件:
./a.exe input.txt 文件只是有很多随机测试用例,所以我可以看看我这样做是否正确。我试图通过使用标准输入从我的输入文件中删除一些标点符号。你会注意到我有几个 printf 躺在那里说“这里”。我把它放在那里看看程序是否到达它。 所以每当我运行它时,我都会输入“./a.exe
谁能解释我为什么会出现这些错误? 这是我的代码:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
char textin[80], wordout[80];
int i, j;
int count = 0;
printf("%d", argc);
while (count != argc)
{
scanf("%s", textin);
if (strcmp(textin, ".") == 0)
printf("here");
printf("here2");
strcpy(wordout, textin);
for (i = 0 ; i < strlen(wordout) ; i++)
{
if (wordout[i] == '.' || wordout[i] == ',' ||
wordout[i] == '"' || wordout[i] == ';' ||
wordout[i] == '!' || wordout[i] == '?' ||
wordout[i] == '(' || wordout[i] == ')' ||
wordout[i] == ':')
{
for (j = i ; j < strlen(wordout) - 1 ; j++)
wordout[j] = wordout[j + 1];
wordout[j] = '\0';
}
printf("here3");
}
printf("%s\n", wordout);
count++;
}
return 0;
}
【问题讨论】:
-
尝试添加input.txt的完整路径然后回复!
-
for (i=0; i<strlen(wordout); i++)非常非常非常糟糕和常见的初学者或 PHP 程序员错误。strcmp(textin,".") == 0->*textin == '.',您也可以使用switch代替if。 -
CygWin?否则,您的可执行文件上的
exe后缀可能会引起更多经验丰富的 UNIX 开发人员的嘲笑:-)
标签: c input while-loop