简体   繁体   English

如何使用fgets查找.csv文件中是否存在此行? (C)

[英]How do use fgets to find if this line exists in .csv file? (C)

Hi I'm tyring to use fgets in a C program to see whether or not a name exists in a line in a .csv file. 嗨,我打算在C程序中使用fgets来查看.csv文件中一行中是否存在名称。 If it does, then it saves it in an array then returns it. 如果是这样,则将其保存在数组中,然后将其返回。 Right now, it saves everything line from the .csv file and I'm not sure why. 现在,它保存了.csv文件中的所有行,但我不确定为什么。

C file: C文件:

void FindRecord(char *filename, char *name, char record[]) {

    char *temp = record; //temp save record

    FILE *q = fopen(filename, "r"); //check that ths inputed .csv file exists
    if (q == NULL ) { //if it doesn't, then print error message and exit
        printf("This .csv does not exist");
        exit(1); //terminate with error message
    }

    while(!feof(q)) { //while I'm not at the end of the file
        fgets(temp, 1000, q); //Reads a line @ a time
        for (int i = 0; i < 1000; i++) {
            if(temp[i] == *name) {
                record[i] = temp[i];
                name++;
            }
        }
        printf("%s", record);
    }
    fclose(q);   
}

.csv file: .csv文件:

Kevin, 123-456-7890
Sally, 213-435-6479
Megan, 415-336-8790

Right now whats happening when I run the program is that it returns the 3 lines. 现在,当我运行程序时发生的事情是它返回了3行。 I want iso that if *name points to the name "Kevin" and it comes with temp, it'll just return: Kevin, 123-456-7890 我想要iso如果*name指向名称"Kevin"并且它带有temp,它将返回: Kevin, 123-456-7890

Right now whats happening when I run the program is that it returns the 3 lines. 现在,当我运行程序时发生的事情是它返回了3行。

I don't see how that's possible. 我不知道怎么可能。 You have only one array in which to return a result. 您只有一个要在其中返回结果的数组。 I could believe that your code prints all three lines, but it will return only the last. 我可以相信您的代码会打印所有三行,但只会返回最后一行。

I want iso that if *name points to the name "Kevin" and it comes with temp , it'll just return: Kevin, 123-456-7890 我想要iso如果*name指向名称"Kevin"并且它带有temp ,它将返回: Kevin, 123-456-7890

Well, your code has several problems in that regard. 好吧,您的代码在这方面有几个问题。 Among the most significant are: 其中最重要的是:

  • Although it performs some character-by-character comparisons, it has no code anywhere to reject lines that fail to match. 尽管它执行逐个字符的比较,但是它在任何地方都没有代码来拒绝不匹配的行。

  • It sets temp to point to the same array as record , and then reads each line into that array. 它将temp设置为指向与record相同的数组,然后将每一行读入该数组。 This will overwrite that array even in the event that no match is found, and if a match is found on a line other than the last one read then the actual match will be lost. 即使没有找到匹配项,这也会覆盖该数组,并且如果在最后一次读取以外的其他行上找到了匹配项,则实际匹配项将丢失。

  • It modifies the name pointer as it attempts to match, with no mechanism for resetting it in the event of a partial match. 它会在尝试匹配name指针时修改name指针,而在部分匹配的情况下,没有用于重置它的机制。

  • When trying to match the name, it blithely scans past the , delimiter in the input line and, if it comes to them, the string terminators in the name and the input string. 当尝试匹配名称时,它将巧妙地扫描输入行中的,分隔符,如果涉及到它们,则扫描名称和输入字符串中的字符串终止符。

  • while (!feof(file)) is always wrong . while (!feof(file))总是错误的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM