从文件C读取字符串的内容和比较平等 [英] C reading string content from file and compare equality

查看:179
本文介绍了从文件C读取字符串的内容和比较平等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读从文件的内容,我希望找一个特定的值,说伦敦。为了这个,我正在读的内容,以\\ N标记化它,然后比较反对伦敦的使用的strcmp每个值。

I am reading content from a file and I am expecting to look for a particular value, say "london". for this I am reading the content, tokenize it by "\n", then compare each value against "london" using "strcmp".

不过我觉得我还是不如何C存储数据理解和做比较,因此,我希望我的低于code不起作用。我想我缺少的C一些基本知识,在这里,请帮助:

But I think I still do not understand how C stores data and does comparison and therefore my code below does not work as i expect. I guess I am missing some fundamental knowledge of C here, please help:

测试命令行:

./myprogram datafile.txt "london"

输入datafile.txt:

INPUT datafile.txt:

london
manchester
britain
...

code myprogram.c:

CODE myprogram.c:

int main(int argc, char **argv) {
  FILE* fl;
  char st1[2000];
  char * buffer = 0;
  long length;

  fl = fopen (argv[1], "r"); //the data file content is shown above
  if (fl){
    fseek (fl, 0, SEEK_END);
    length = ftell (fl);
    fseek (fl, 0, SEEK_SET);
    buffer = malloc (length+1);
    if (buffer){
        //fread (buffer, 1, length, fl);
        fread(buffer, sizeof(char), length, fl);
        buffer[length] = '\0';
    }
  fclose (fl);    
  }else{
    printf("data file not found");
    return -1;
  }  


  //firstly let's compare the value passed by command line with "london"
  strcpy(st1, argv[2]); 
  if(strcmp(st1,"london")==0)
    printf("equals\n");  //as expected, I get "equals" printed
  else
    printf("unequal\n");


  //now let's compare the values extracted from the data file, 
  char* entity = strtok(buffer, "\n"); 
  while (entity != NULL) {
    strcpy(st1, entity); //copy the value from the char pointer entity to the char array st1 so we can compare with other strings

    printf("%s\n", st1);  //this prints london, ....

    if(strcmp(st1,"london")==0)
        printf("equals\n");  //I was expecting this..
    else
        printf("unequal\n"); //but i got this...
    entity = strtok(NULL, "\n");
  }
  return 0;

}

我期待输出是:

equals
london
equals
manchester
unequal
britain
unequal
...

但我不明白为什么我收到

but I dont understand why I am getting

equals
london
unequal  <=============== why and how to fix?
manchester
unequal
britain
unequal
...

和我应该怎么改变它,从文件中读取值伦敦等于实际的串,伦敦?

And how should I change it so the value "london" read from the file equals the actual "string" of "london"?

非常感谢

推荐答案

我试试下面的事情,我被替换的strcpy 的号召函数strncpy 。您最初的问题是'\\ 0'在 ST1 变量的末尾。所以,你可以这样做:

I try the following things and I replace the call of strcpy by strncpy. Your initial problem was the '\0' at the end of st1 variable. So you can do this :

  while (entity != NULL) {
    strncpy(st1, entity,strlen(entity)-1); //copy the value from the char pointer entity to the char array st1 so we can compare with other strings

    printf("%s\n", st1);  //this prints london, ....

    if(!strcmp(st1,"london"))
        printf("equals\n");  //I was expecting this..
    else
        printf("unequal\n"); //but i got this...
    entity = strtok(NULL, "\n");
  }

或者是其他的解决办法是把'\\ 0'自己在 ST1 像这样结束:

strcpy(st1, entity);
st1[strlen(entity)-1] = '\0';

我测试都和它的工作。

I test the both and it's working.

这篇关于从文件C读取字符串的内容和比较平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆