simple C++ fstream problems,May anyone help me?

I am in trouble for the simple C++ fstream problems,may anyone help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void SinglyList::load(){
 ifstream fin;
 Node* currNode=head;
 Node* newNode = new Node;
 cout<<"File loaded Successfully..."<<endl;
 fin.open("abc.txt");
  while(!fin.eof()&&currNode){
 fin>>newNode->number;
 fin>>newNode->name;
 fin>>newNode->age;
 fin>>newNode->lib_id;
 newNode->next = currNode->next;
 currNode->next = newNode;

  }
fin.close();
}



However there are no output from screen.May anyone help me?Many thanks.
Your problem isn't with streams, it's with your linked list stuff.

What happens if head is null?

Also, you really should post declarations too, it would be really helpful to see the declaration of Node.
Shouldn't you test if the file was opened successfully before printing "File loaded Successfully..."?
actually it is the add function of my real thing want to do,

my whole function included Node are actually like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef NODE_H
#define NODE_H


class Node
{
   public:			
     // data	
	 int order;
	 int number;
	 string name;
	 int age;
         int lib_id;
     //pointer to next
     Node* next;
};

#endif 





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Node* List::insertNode(int index) {
  cout<<"<Add Data>"<<endl;
  if(index < 0)
  return NULL;
  int currIndex = 1;
  Node* currNode = head;
 
  while(currNode && index > currIndex) {
    currNode = currNode->next;
    currIndex++;
  }

  if(index > 0 && currNode == NULL)
  return NULL;

  Node* newNode = new Node;
  newNode->number=index;
  cout<<"Please input the name:";
  cin>>newNode->name;
  cout<<"Please input his age:";
  cin>>newNode->age;
  cout<<"Please input lib_id:";
  cin>>newNode->lib_id;
  

  if(index == 0) {
    newNode->next = head;
    head = newNode;
  }
  else {
    newNode->next = currNode->next;
    currNode->next =newNode;
  }

  cout<<"data successful added";
  cout<<endl;
  return newNode;
}



the function run such like that,but if there has no running problem at all,however it cannot load data after closing screen,is there have any problems contains from my codes???
also for cout the statement from cin,the whole explanation of load() could like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void SinglyList::load(){
 ifstream fin;
 Node* currNode=head;
 Node* newNode = new Node;
 cout<<"File loaded Successfully..."<<endl;
 fin.open("abc.txt");
  while(!fin.eof()&&currNode){
 fin>>newNode->number;
 fin>>newNode->name;
 fin>>newNode->age;
 fin>>newNode->lib_id;
 newNode->next = currNode->next;
 currNode->next = newNode;

  }
cout<<currNode->number<<" "<<currNode->name;
currNode = currNode->next;
fin.close();
}
Topic archived. No new replies allowed.