2

all.

After reading up on segmentation faults, I still can't figure out just where this one's coming from. I know that it's coming from this specific function; everything else in my driver program works.

Worth noting is that all of the styles are of the enumerated data type, StyleT.

The function being called:

openList(&list, "List.txt");

The function's definition:

void openList(VehicleListT *list, char *infilename)
{   
    FILE *infile;
    int i = 0;
    char styleString[20];

    newList(list);

    if((infile = fopen(infilename, "r")) == NULL)
    {
        fprintf(stderr, "ERROR: Cannot open source file!\n");
        exit(1);
    }

    fscanf(infile, "%s\n", list->vehicles[i].vin);
    while(!feof(infile))
    {
        fscanf(infile, "%i\n", list->vehicles[i].year);
        fscanf(infile, "%lf\n", list->vehicles[i].price);
        fscanf(infile, "%s\n", list->vehicles[i].make);

        fscanf(infile, "%s\n", styleString);

        if((strcmp(styleString, "TWO_DOOR")) == 0)
        {
            list->vehicles[i].style = TWO_DOOR;
        }
        if((strcmp(styleString, "FOUR_DOOR")) == 0)
        {
            list->vehicles[i].style = FOUR_DOOR;
        }

        if((strcmp(styleString, "CONVERTIBLE")) == 0)
        {
        list->vehicles[i].style = CONVERTIBLE;
        }

        if((strcmp(styleString, "TRUCK")) == 0)
        {
                list->vehicles[i].style = TRUCK;
        }

        if((strcmp(styleString, "SUV")) == 0)
        {
            list->vehicles[i].style = SUV;
        }

        fscanf(infile, "%s\n", list->vehicles[i].color);
        fscanf(infile, "%s\n", list->vehicles[i].vin);

        i++;
        list->count++;
    }

    fclose(infile);
    return;
}
6
  • How do you know your vehicle list has enough memory to hold the number of records you are reading? Commented Apr 25, 2013 at 2:22
  • The max limit is set to 20 in the .h file and in my test program I have only made 4 entries that were saved to the file "List.txt" which is the file this function is opening. Commented Apr 25, 2013 at 2:23
  • 1
    Run it through a debugger, and find out the line , where it seg faults... Commented Apr 25, 2013 at 2:27
  • I tried that, and it would only take me to the line where this function is. Of course, I also only just found out how to debug using CygWin and gdb. But, yeah. Should I try stepping the program after that? Commented Apr 25, 2013 at 2:30
  • what happens in newList? Commented Apr 25, 2013 at 2:32

2 Answers 2

1

Among other problems , which i can't find out since i don't have the full code , one obvious mistake , which gives you a segmentation fault in your program is

fscanf(infile, "%i\n", list->vehicles[i].year);
fscanf(infile, "%lf\n", list->vehicles[i].price);

The above lines should be,

fscanf(infile, "%i\n",  &list->vehicles[i].year);
fscanf(infile, "%lf\n", &list->vehicles[i].price);
Sign up to request clarification or add additional context in comments.

3 Comments

You are absolutely correct! Man, I should have known! It's always such a simple error.. A missing parenthesis, an ampersand.. Thank you!
And I also noticed that I only needed the ampersand for the ones pointing to integers/doubles.. Because otherwise, the variables I was using were strings which makes the 'list->vehicles[i].make', etc. pointers that don't need the '&'. Again, I thank you!
yes, you need to use only '&' in scanf , only while dealing with integers,floats or character bytes to indicate their address , strings and arrays on the other hand have a special property where their names acts as the address of the string/array itself
0

A few ideas:

  • Looks like you read the VIN twice (right before the while loop then while in it)? Could you be skipping a line?
  • Could a string be overrunning? IE a 30 char VIN number?
  • Or you're running past the array bounds? Too many cars on the lot? :) I suggest adding a safety check for that. Or doing as a linked list.

I'd check how the data is loading in or print it out at the end of each iteration of that while loop. Also recommend the safety check of erroring out if 'i' gets too big.

2 Comments

In order! 1. I read it before the loop begins and then at the end of the loop. It's the first bit of data in each set. I do this because when it tries to scan the VIN of the first car that isn't there (as in, the first car that doesn't exist past the list), it will return that there isn't a car there and will have the while loop break. I also tried just scanning the VIN once in the loop to no avail. 2. No error testing, but no. All vins < 30 characters. (I should error test!) 3. I can confirm the array isn't going out of bounds (though, again, I should error test!)
I also tried to print out the values of 'i' per iteration, among other things.. But it wouldn't even print! It seemed as if as soon as the openList function began, I would get a segfault! Even tried putting in little "Yay, the function made it thsi far!" comments to cheer myself up, but again.. to no avail. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.