** 更新于 10 月 26 日 -> 首先感谢您所有的帮助,我现在离成功更近了,还需要更多的工作和学习,但我真的非常感激您的帮助 :-)
仍然不知道为什么 input.txt 文件中的第一个 "rain" 单词没有从 strcmp 中获得正面输出,而且从 cmd 中可以看到 "<" 符号除了最后一行以外都没有出现。
也检查了来自Removing trailing newline character from fgets() input 的突出响应
即使我更改代码如下:
while( fgets (line, sizeof line, fp)!=NULL ) {
/* remove \n from at the end of the str buffer*/
char * pos;
/*
if ((pos = strchr(line, '\n')) != NULL)
*pos = '\0';
*/
line[strcspn(line, "\n")] = 0;
我得到的结果与使用if块的结果相同。也许我会得到额外的\ 0,这可能是情况。有没有链接可以让我了解刚才使用的分隔符,或一个不错的调试器参考资料等等……我会在这里找到后尽快查看。非常感谢您的帮助!
read5.c版本:现在从那个input.txt文件中,最后一个“rain”单词上有一个额外的空格,我删除了它并且能够以真实结果的形式找到和获取该最后一个单词,执行strcmp if语句块。但这是唯一一个从该if块中获得真正阳性结果的字符串。
在cmd上,我可以看到:
$./read5 input.txt rain output.txt sun
>Maria
>rain
>manel
>Bla bla
<rain>
Found it! rain
输出到 output.txt 文件中:
Maria
rain
manel
Bla bla
sun
read5.c
#include <stdio.h>
#include <string.h>
/**
* Compile program:
* gcc read3.c -o read3
*
*/
int main (int argc, char **argv) {
FILE *fp, *fo;
char *compare, *replace;
char line[246];
if (argc <= 4){
printf(">Missing arguments on the command line.\n");
printf(">Be sure you run the program as\n\"./read3 input.txt compare outout.txt replace\"\n\n");
}
/* opening file for reading */
fp = fopen(argv[1] , "r");
if(fp == NULL){
perror("Error opening input file");
return 1;
}
compare = argv[2];
fo = fopen(argv[3], "w");
if(fo == NULL){
perror("Error opening output file");
return 1; //TODO check if: return 1 because it was expected, right?
}
replace = argv[4];
/*
printf(); made to test version 2
//printf("We are going to compare %s\n", compare);
//printf("We are going to replace it with %s\n", replace);
*/
while( fgets (line, sizeof line, fp)!=NULL ) {
/* remove \n from at the end of the str buffer*/
char * pos;
if ((pos = strchr(line, '\n')) != NULL)
*pos = '\0';
/* print str enclosed in <> so we can see what str actually contains */
//printf("Inside the loop, got the string: %s\n", line);
//printing the strings with defined delimiters
printf("<%s>\n", line);
if(strcmp(compare, line) == 0){
printf("Found it! %s \n", line);
fprintf(fo, "%s\n", replace);
}
else{
fprintf(fo, "%s\n", line);
}
}
fclose(fp);
fclose(fo);
return 0;
}
第一个无编辑的问题:25/10
我需要制作一个以这种方式运行的程序:
./read2 input.txt rain output.txt sun
它读取
input.txt
,查找rain
字符串,如果找到它就用sun
字符串替换它,并将所有文本从input.txt
输出到output.txt
。但是,根据我目前的代码,strcmp
没有比较我想要比较的字符串,也许是我在命令行上得到的额外空格,我不知道...现在做的事情是将input.txt
中的所有内容复制到output.txt
中... 它总是运行else
块...
Read2.c:
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
FILE *fp, *fo;
char str[60];
//char* token;
/* opening file for reading */
fp = fopen(argv[1], "r");
char *compare = argv[2];
fo = fopen(argv[3], "w+");
char *replace = argv[4];
if (fp == NULL) {
perror("Error opening file");
return(-1);
}
//printf("We are going to compare %s\n", compare);
//printf("We are going to replace it with %s\n", replace);
while (fgets(str, 60, fp) != NULL) {
/* writing content to stdout */
//Take the \n out
//token = strtok(str, "\n");
printf("Inside the loop, got the string: %s\n", str);
if (strcmp(compare, str) == 0) {
//puts(str);
printf("Found it! %s \n", str);
fprintf(fo, "%s", replace);
} else {
fprintf(fo, "%s", str);
}
}
fclose(fp);
return(0);
}
input.txt:
Maria
rain
manel
Bla bla
rain
Ouput.txt变成了和input.txt完全一样的内容,而且在此之前它是空的,所以代码运行正常,只有测试strcmp
的if块存在问题。
fgets
会保留输入字符串末尾的换行符。请参阅 从fgets()
输入中删除尾随的换行符。 - Weather Vane