Strcmp没有比较argv中的字符串

问题描述 投票:3回答:4

** 26/10更新 - >首先感谢大家的帮助,我现在越来越近了,我需要更多的工作和学习,但我真的很感谢你帮助了我很多:-)

仍然不知道为什么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 if block的真正积极结果。

在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.txtoutput.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 

Output.text完全和input.txt一样,在它为空之前,所以代码工作正常,除了用strcmp测试的if块。

c string argv strcmp
4个回答
1
投票

问题是在\n缓冲区末尾的strfgets在它读取的行的末尾添加了\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);
    }
  }

查看代码中的注释以获得解释。


0
投票

您的方法失败,因为从输入文件中读取的行包含一个尾随换行符'\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;
}

请注意,长行将被分成适合line数组的块,因此上述天真方法可能存在误报。

您可以使用此内循环完全删除此限制:

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);
}

0
投票

来自man fgets

char * fgets(char * s,int size,FILE * stream);

fgets()从流中读取最多一个小于大小的字符,并将它们存储到s指向的缓冲区中。读数在EOF或换行符后停止。如果读取换行符,则将其存储到缓冲区中。终止空字节('\ 0')存储在缓冲区中的最后一个字符之后。

所以你需要从缓冲区s中删除换行符,例如:

c[strlen(c) - 1] = 0 when c[strlen(c) - 1] == '\n'

0
投票

read.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);

*/

input.txt中

cloudy
rainy
chilly
rainy
rainy
© www.soinside.com 2019 - 2024. All rights reserved.