0

I'm trying to make a program in C for reading in lines of text from a file, and creating nodes to build a tree. these nodes are structs. To do this, I'm trying to read six lines at a time. whenever my program gets to the fscanf line however, it doesn't seem to read anything, setting my int to EOF and exiting the function. I've tried a great deal of format combinations, removing and adding spaces, \n and the like. I've even tried making a separate fscanf line to attempt to read in a single string, and even that seems to scan nothing. I have no idea why this is happening. here's the relevant code:

member_ptr readAndCreate(FILE * file){
    member_node * temp;
    temp = calloc(1, sizeof(member_node));
    //char temp_char_array[50] = {0,0,0,0,0};
    //char *overflow;

    int isEnd;
    //isEnd = fscanf(file, " %s", temp_char_array);

    //isEnd =
    isEnd = fscanf(file, " %[^\n] %[^\n] %d %[^\n] %[^\n] %[^\n]",
            temp -> family,
            temp -> personal,
            &temp ->ID,
            temp -> email,
            temp -> boatClass,
            temp -> boatName
            );

    //temp->ID =  (int)strtol(temp_char_array, &overflow, 10);
    if (isEnd == EOF){
        printf("Something went wrong, please try again \n");
        return NULL;
    } else {
        return temp;
    }
}

and this is the main function

int main() {
    char pathname[100];
    FILE * file;
    member_ptr top;
    member_ptr temp;

    printf("input file path\n");
    scanf("%[^\n]", pathname);
    file = fopen(pathname, "r");

    if (file == NULL){
        printf("file cannot be found, closing program...");
        exit(1);
    }

    top = readAndCreate(file);
    genTree(top, file);
    printOutTree(top);

    printf("Hello, World!\n");
    return 0;
}
Arkku
  • 37,604
  • 10
  • 57
  • 79
  • 2
    This cannot be compiled to test it and we don't have the input file you are using, but I cannot spot any obvious errors. One possibility is that it's due to the input, e.g., the file is misformatted or the lines are too long, causing undefined behaviour since you are not limiting the length of the strings being read. I would also suggest replacing the "Something went wrong" `printf` with `perror("fscanf")`. And please see https://stackoverflow.com/help/minimal-reproducible-example – Arkku Nov 27 '19 at 12:41
  • 2
    The `scanf` family functions are only a poor man's parser. I try hard to not use them for complex or serious processing and **never** for line oriented input. As your input is line oriented, `fgets` would be a more robust choice – Serge Ballesta Nov 27 '19 at 12:43
  • Post definition of `member_node`,. A [mcve] is even better. – chux - Reinstate Monica Nov 27 '19 at 12:48
  • `scanf` is barely okay for "toy" programs, and not at all for real programs. Rule of thumb: if you find yourself using `%[...]`, it's time to abandon `scanf` and [learn how to use something better](https://stackoverflow.com/questions/58403537/what-can-i-use-to-parse-input-instead-of-scanf). It'll be more functional *and* easier in the long run. – Steve Summit Nov 27 '19 at 14:03
  • Please make an MRE, the problem can e.g. easily be within genTree. – Yunnosch Nov 27 '19 at 14:58

1 Answers1

0

I see a problem with your scan codes. The s designating string input seems to be missing.

Try changing

isEnd = fscanf(file, " %[^\n] %[^\n] %d %[^\n] %[^\n] %[^\n]",

into

isEnd = fscanf(file, " %[^\n]s %[^\n]s %d %[^\n]s %[^\n]s %[^\n]s",

You have the same bug in the main() function.

Klas Lindbäck
  • 32,158
  • 4
  • 51
  • 77