1

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

C file:

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:

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. I want iso that if *name points to the name "Kevin" and it comes with temp, it'll just return: Kevin, 123-456-7890

Sam
  • 495
  • 3
  • 11
  • 19
  • 3
    Please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/a/26557243/1983495). – Iharob Al Asimi Nov 06 '17 at 22:53
  • Why 1000 in `fgets(temp, 1000, q)`? `record` is not shown to be that big. Need to see the calling code. – chux - Reinstate Monica Nov 06 '17 at 22:55
  • `record` and `temp` point to the same memory so `record[i] = temp[i]` is useless. `fgets(temp, 1000, q);` loads whatever from the file into `temp`, and also `record`. – yano Nov 06 '17 at 23:02
  • 1
    If this is like the 3 other versions of the same question asked recently, you are also failing to remove the trailing `'\n'` from `name` before passing it to your function (just a guess as to another complicating factor) Generally, you will also want to open the file in the calling function and pass a `FILE *` pointer as a parameter to your function. If this is the only function that reads from the file, it doesn't matter, it is just a non-standard way of doing it (especially with a return type `void` that can provide no indication of success/failure in return ). – David C. Rankin Nov 06 '17 at 23:09
  • How do you know your `fgets()` call actually works? – Andrew Henle Nov 06 '17 at 23:15

1 Answers1

2

Right now whats happening when I run the program is that it returns the 3 lines.

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

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

  • 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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157