Reading into linked-list from a file

Discussion in 'C++' started by scythemk, Feb 19, 2006.

  1. scythemk

    scythemk Guest

    Hi,
    I am writing a program that, everytime it executes, first loads all
    information from a file into a linked list of nodes, using struct to
    define it. After manipulating the data and receiving user input, the
    program once again outputs the linked list information to the same
    file. My problem is that when I'm first trying to get the info from the
    file into the linked list I get a bus error. My code is as follows:
    struct nodeType
    {
    string f_name, l_name, course_dep[9], course_num[9];
    /*more declarations*/
    nodeType *next;
    };

    int load(nodeType*);

    int main()
    {
    cout<<"test0"; //never prints in program execution, bus error seems
    to occur before that
    nodeType *start;
    load(start);
    return 0;
    }

    int load(nodeType *start)
    {
    cout<<"test1"; //does not print
    ifstream inData;
    inData.open("student.dat");
    if (inData.fail())
    {
    cout<<"Could not open file! Please verify that student.dat
    exists!";
    exit(0);
    }
    nodeType *current;
    current = start;
    while (current->next != NULL)
    {
    /*reading data into the list*/
    }
    inData.close();
    }

    If anyone could give me a hint as to what the problem is I would be
    very helpful. I have tried using DDD and it throws the seg fault signal
    at the load(*start) call but I still don't understand what's wrong.
    Thanks a lot.
    Ignas
    scythemk, Feb 19, 2006
    #1
    1. Advertising

  2. scythemk

    TB Guest

    scythemk sade:
    > Hi,
    > I am writing a program that, everytime it executes, first loads all
    > information from a file into a linked list of nodes, using struct to
    > define it. After manipulating the data and receiving user input, the
    > program once again outputs the linked list information to the same
    > file. My problem is that when I'm first trying to get the info from the
    > file into the linked list I get a bus error. My code is as follows:
    > struct nodeType
    > {
    > string f_name, l_name, course_dep[9], course_num[9];
    > /*more declarations*/
    > nodeType *next;
    > };
    >
    > int load(nodeType*);
    >
    > int main()
    > {
    > cout<<"test0"; //never prints in program execution, bus error seems
    > to occur before that


    It doesn't print because it isn't flushed before the illegal pointer
    access occurs in load().

    > nodeType *start;


    And where do you allocate 'start'?
    start = new nodeType;

    > load(start);
    > return 0;
    > }
    >
    > int load(nodeType *start)
    > {
    > cout<<"test1"; //does not print


    Same as above.

    > ifstream inData;
    > inData.open("student.dat");
    > if (inData.fail())
    > {
    > cout<<"Could not open file! Please verify that student.dat
    > exists!";
    > exit(0);
    > }
    > nodeType *current;
    > current = start;
    > while (current->next != NULL)


    You're accessing illegal memory here.

    > {
    > /*reading data into the list*/
    > }
    > inData.close();
    > }
    >
    > If anyone could give me a hint as to what the problem is I would be
    > very helpful. I have tried using DDD and it throws the seg fault signal
    > at the load(*start) call but I still don't understand what's wrong.
    > Thanks a lot.
    > Ignas
    >


    --
    TB @ SWEDEN
    TB, Feb 19, 2006
    #2
    1. Advertising

  3. scythemk

    yangc Guest

    except allocate memory, you should set NULL at first such as:

    nodeType *start = malloc(sizeof(nodeType);
    start->next=NULL;
    yangc, Feb 21, 2006
    #3
  4. In message <>,
    yangc <> writes

    Please learn how to quote what you are replying to. Your message is
    almost incomprehensible.

    >except allocate memory, you should set NULL at first such as:
    >
    >nodeType *start = malloc(sizeof(nodeType);


    Why??? This is comp.lang.c++, not comp.lang.c. Why are you recommending
    the use of malloc(), which merely allocates memory and doesn't construct
    objects? C++ provides much better tools for this job.

    nodeType * start = new nodeType;

    >start->next=NULL;


    Not necessary if you use new and nodeType's constructor takes
    responsibility for correctly initialising its pointers.

    --
    Richard Herring
    Richard Herring, Feb 21, 2006
    #4
  5. scythemk

    Daniel T. Guest

    In article <>,
    "scythemk" <> wrote:

    > Hi,
    > I am writing a program that, everytime it executes, first loads all
    > information from a file into a linked list of nodes, using struct to
    > define it. After manipulating the data and receiving user input, the
    > program once again outputs the linked list information to the same
    > file. My problem is that when I'm first trying to get the info from the
    > file into the linked list I get a bus error. My code is as follows:


    It would be better to use a std::list, unless your teacher objects.

    > struct nodeType
    > {
    > string f_name, l_name, course_dep[9], course_num[9];
    > /*more declarations*/
    > nodeType *next;
    > };
    >
    > int load(nodeType*);


    There is a problem with the above, see below.

    > int main()
    > {
    > cout<<"test0"; //never prints in program execution, bus error seems
    > to occur before that


    That's because you didn't flush the stream.

    > nodeType *start;
    > load(start);


    Note, at this point the value of 'start' has not changed. You will be
    unable to access your nodes. Use the load as defined below and this will
    work:

    nodeType* start = load();

    > return 0;
    > }
    >
    > int load(nodeType *start)


    The above should be "noadType* load()"

    > {
    > cout<<"test1"; //does not print


    Again, you didn't flush the stream (by calling 'flush()' or putting
    'endl' to it.)

    nodeType* start = NULL;

    > ifstream inData;
    > inData.open("student.dat");


    You can combine the two expressions above into one:

    ifstream inData( "student.dat" );

    > if (inData.fail())
    > {
    > cout<<"Could not open file! Please verify that student.dat
    > exists!";
    > exit(0);
    > }


    What if no students have been loaded yet? You wouldn't expect a
    student.dat file to exist then. Better would be to simply return NULL.

    > nodeType *current;
    > current = start;
    > while (current->next != NULL)
    > {
    > /*reading data into the list*/
    > }


    The above is messed up, there are no nodes yet, you have to make them
    with "new".

    read your data.
    if successful:
    make a new nodeType object.
    fill it with your data
    attach it to the chain


    > inData.close();


    Not necessary. 'inData' will automatically close upon exit of the
    function.

    > }
    >
    > If anyone could give me a hint as to what the problem is I would be
    > very helpful. I have tried using DDD and it throws the seg fault signal
    > at the load(*start) call but I still don't understand what's wrong.
    > Thanks a lot.
    > Ignas




    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Feb 21, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    466
    emerth
    Jul 10, 2003
  2. Francis Bell
    Replies:
    3
    Views:
    741
    Victor Bazarov
    Jun 2, 2004
  3. Henk
    Replies:
    4
    Views:
    840
  4. fool
    Replies:
    14
    Views:
    494
    Barry Schwarz
    Jul 3, 2006
  5. joshd
    Replies:
    12
    Views:
    657
    John Carson
    Oct 2, 2006
Loading...

Share This Page