HI!
I have two functions they are exactly the same :
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int ShowStudentsInfo () // This function reads the file "student.txt" and shows all of the informations in the sort which they appear in the file
{
fstream fstu ("student.txt" , ios :: in) ; // fstu = File Student
int num ; // Number of students in file
fstu >> num ;
cout << "\n\nThere are " << num << " students in student.txt :" ;
string line ; // This is used for reading each line from the file
while (! fstu.eof ())
{
getline (fstu , line) ;
cout << endl << line ;
}
return 1 ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int ShowProfessorsInfo () // This function reads the file "professor.txt" and shows all of the informations in the sort which is in the file
{
fstream fpro ("professor.txt" , ios :: in) ; // fstu = File Professor
int num ; // Number of Professors in file
fpro >> num ;
cout << "\n\nThere are " << num << " professors in professor.txt :" ;
string line ; // This is used for reading each line from the file
while (! fpro.eof ())
{
getline (fpro , line) ;
cout << endl << line ;
}
return 1 ;
}
The only thing they do is to show the content of files exactly as it is .
and these are my files :
student.txt
3
hamed
hamidi
1001
civil
associate Professor
91/02/15
morteza
jafari
1005
computer
assistant Professor
91/01/20
mahdi
foladi
1006
computer
professor
90/10/10
ShowProfessorsInfo () works correctly BUT ShowStudentsInfo () outputs this :
There are 0 students in student.txt :
//Infinite loop
Why is that ?
And it was working correctly till yesterday , but Today when I trien to run it I faced this problem !
I couldn't find any thing strange , check if you can please !
THNX !
Are you sure the file is there and named correctly in the second case?
In neither function do you check that the open succeeded.
If the open fails, you will get the behaviour you are experiencing.
You should check in your code that the open succeeds and if doesn't you should display an error and exit.
If the open fails, the failbit will be set and fpro >> num; won't be executed.
Thank YOU @nevermore28 and @AbstractionAnon
Yes I'm sure there is no new line and Yes I put an "if (fst.good ())" and it was good , both files are in same folder as the project and main.cpp is and still it is strange .
I think I'm missing something somewhere .
I will chek it later !
Thenks for your help , I appreciate it.