Reading into linked-list from a file

S

scythemk

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
 
T

TB

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

yangc

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

nodeType *start = malloc(sizeof(nodeType);
start->next=NULL;
 
R

Richard Herring

In message <[email protected]>,

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

Daniel T.

"scythemk said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top