Strcmp未比较argv中的字符串
**更新26/10->首先感谢你们的帮助,我现在离你们越来越近了,我需要更多的工作和学习,但我真的很感谢你们对我的帮助:-)Strcmp未比较argv中的字符串,c,string,argv,strcmp,C,String,Argv,Strcmp,**更新26/10->首先感谢你们的帮助,我现在离你们越来越近了,我需要更多的工作和学习,但我真的很感谢你们对我的帮助:-) 仍然不知道为什么input.txt文件中的第一个“rain”字没有从strcmp获得正输出,从cmd我可以看到“问题在于str缓冲区末尾的\nfgets在它读取的行的末尾添加\n,您需要在比较之前除去它 这就是您需要的: while (fgets(str, 60, fp) != NULL) { /* remove \n from at the end of
仍然不知道为什么input.txt文件中的第一个“rain”字没有从strcmp获得正输出,从cmd我可以看到“问题在于
str
缓冲区末尾的\n
fgets
在它读取的行的末尾添加\n
,您需要在比较之前除去它
这就是您需要的:
while (fgets(str, 60, fp) != NULL) {
/* remove \n from at the end of the str buffer*/
char *pos;
if ((pos = strchr(str, '\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", str);
if (strcmp(compare, str) == 0) {
printf("Found it! %s\n", str);
fprintf(fo, "%s\n", replace);
}
else {
fprintf(fo, "%s\n", str);
}
}
while(fgets(str,60,fp)!=NULL){
/*从str缓冲区结尾处删除\n*/
char*pos;
如果((pos=strhr(str,'\n'))!=NULL)
*pos='\0';
/*打印包含在中的str,以便我们可以看到str实际包含的内容*/
printf(“在循环中,得到字符串:\n”,str);
if(strcmp(比较,str)==0){
printf(“找到了!%s\n”,str);
fprintf(fo,“%s\n”,替换);
}
否则{
fprintf(fo,“%s\n”,str);
}
}
请查看代码中的注释以获取解释。您的方法失败,因为从输入文件读取的行包含一个使比较返回非零的尾随换行符
'\n'
在与搜索字符串进行比较之前,可以去掉换行符
请注意,还有其他问题:
- 您应该通过测试
来验证是否传递了足够的命令行参数argc>4
- 无需在更新模式下打开输出文件,
,“w+”
更简单、更好“w”
- 60字节对于行数组来说有点小,将正确处理的最长行限制为58字节
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
FILE *fp, *fo;
char *compare, *replace;
char line[256];
if (argc <= 4) {
printf("missing command line arguments\n");
return 1;
}
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;
}
replace = argv[4];
while (fgets(line, sizeof line, fp) != NULL) {
line[strcspn(line, "\n")] = '\0';
if (strcmp(line, compare) == 0) {
printf("fount it!);
fprintf(fo, "%s\n", replace);
} else {
fprintf(fo, "%s\n", line);
}
}
fclose(fp);
fclose(fo);
return 0;
}
从
man fgets
char*fgets(char*s、int大小、文件*流)
fgets()从流中最多读入一个小于大小的字符,并将它们存储到缓冲区中
由s。EOF或换行符后,读取停止如果读取换行符,则将其存储到缓冲区。a
终止空字节('\0')存储在缓冲区中最后一个字符之后
因此,您需要从缓冲区s
中删除换行符,类似于:
c[strlen(c) - 1] = 0 when c[strlen(c) - 1] == '\n'
读.c
#include <stdio.h>
#include <string.h>
/**
* How to compile program:
* gcc read.c -o read
*
* How to run the program:
* .> ./read input.txt rainy output.txt sunny
* (On Windows MinGW compiler, simply:
* .> read input.txt rainy output.txt sunny - without ./)
*
*/
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\"./read input.txt compare outout.txt replace\"\n\n");
}
/* Opening files 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;
}
replace = argv[4];
while( fgets (line, (sizeof line), fp)!=NULL ) {
line[strcspn(line, "\n")] = 0;
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;
}
/*
Important info
strcspn ::
Locate first occurrence of character in string,
after locating the first occurrence of \n, replaces it by 0.
Sources::
https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221
Used to debug:
.>printf("1st: Reads input.txt, removes '\\n' from fgets, and prints it \n");
.>printf("2nd: Compares each line with 'rainy' \n");
.>printf("<%s>\n", line);
*/
不检查
fo
是否为空。并且不检查argc
是否小于5。请提供一个最小的input.txt
文件,以便重现问题。这种方法仅在输入文件中每行文本有一个单词时有效。但是您忽略了fgets
在输入字符串的末尾保留了换行符。请看标题上的注释:你应该找一个更好的。你让它听起来好像strcmp()
无法正常工作,但事实并非如此。。。此外,作为一名程序员,您需要提供一个示例文件和一个示例输入,以及程序的预期和实际输出。@JessicaPereira可能这不是问题所在,但无论如何您都应该检查。用户可以使用无效参数调用您的程序,并且可能由于任何原因无法创建输出文件。“可能它在命令行上有额外的空间,我不知道”——为什么不使用定义的分隔符打印字符串以查找,例如printf(“\n”,str)
?或者使用一个调试器,它将为您比较的strigs提供C表示?这样的事情很容易发现。
./read2 input.txt rain output.txt sun
#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);
}
Maria
rain
manel
Bla bla
rain
while (fgets(str, 60, fp) != NULL) {
/* remove \n from at the end of the str buffer*/
char *pos;
if ((pos = strchr(str, '\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", str);
if (strcmp(compare, str) == 0) {
printf("Found it! %s\n", str);
fprintf(fo, "%s\n", replace);
}
else {
fprintf(fo, "%s\n", str);
}
}
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
FILE *fp, *fo;
char *compare, *replace;
char line[256];
if (argc <= 4) {
printf("missing command line arguments\n");
return 1;
}
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;
}
replace = argv[4];
while (fgets(line, sizeof line, fp) != NULL) {
line[strcspn(line, "\n")] = '\0';
if (strcmp(line, compare) == 0) {
printf("fount it!);
fprintf(fo, "%s\n", replace);
} else {
fprintf(fo, "%s\n", line);
}
}
fclose(fp);
fclose(fo);
return 0;
}
int c;
int pos = 0;
int cmplen = strlen(compare);
for (;;) {
c = getc(fp);
if (c == '\n' || c == EOF) {
if (pos == cmplen) {
fprintf(fo, "%s", replace);
} else
if (pos > 0) {
fprintf(fo, "%*s", pos, compare);
}
pos = 0;
if (c == EOF)
break;
} else {
if (pos >= 0) {
if (compare[pos] == (char)c) {
pos++;
continue;
}
if (pos > 0) {
fprintf(fo, "%*s", pos, compare);
}
pos = -1;
}
}
putc(c, fo);
}
c[strlen(c) - 1] = 0 when c[strlen(c) - 1] == '\n'
#include <stdio.h>
#include <string.h>
/**
* How to compile program:
* gcc read.c -o read
*
* How to run the program:
* .> ./read input.txt rainy output.txt sunny
* (On Windows MinGW compiler, simply:
* .> read input.txt rainy output.txt sunny - without ./)
*
*/
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\"./read input.txt compare outout.txt replace\"\n\n");
}
/* Opening files 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;
}
replace = argv[4];
while( fgets (line, (sizeof line), fp)!=NULL ) {
line[strcspn(line, "\n")] = 0;
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;
}
/*
Important info
strcspn ::
Locate first occurrence of character in string,
after locating the first occurrence of \n, replaces it by 0.
Sources::
https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221
Used to debug:
.>printf("1st: Reads input.txt, removes '\\n' from fgets, and prints it \n");
.>printf("2nd: Compares each line with 'rainy' \n");
.>printf("<%s>\n", line);
*/
cloudy
rainy
chilly
rainy
rainy