How to find End of file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 10
Reputation: rdubey_jsr is an unknown quantity at this point 
Solved Threads: 0
rdubey_jsr rdubey_jsr is offline Offline
Newbie Poster

How to find End of file

 
1
  #1
Jul 26th, 2006
I am reading a CSV file and parsing its content into tokens. Now i want to know how to check for End of File. I am giving my code to be more clear.
ifstream file(filename);
if (file)
{
char ch ;
do
{ file.getline(line, SIZE);
tmp =static_cast<string>(line); 
vector <string> array; 
string token; 
istringstream iss(tmp);
int i=1;
while ( getline(iss, token, ',') )
{
array.push_back(token);

v_List.SetItemText(nItem, i, token.c_str());
i++;
}
// }//cin.get();
}while(ch=EOF); // Here i have to check for EOF but this one enters an infinite loop.

Help me !
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: How to find End of file

 
1
  #2
Jul 26th, 2006
try
  1. ifstream file(filename);
  2. if (file)
  3. {
  4. while ( file.getline(line,SIZE) )
  5. {
  6. tmp =static_cast<string>(line);
  7. vector <string> array;
  8. string token;
  9. istringstream iss(tmp);
  10. int i=1;
  11. while ( getline(iss, token, ',') )
  12. {
  13. array.push_back(token);
  14. v_List.SetItemText(nItem, i, token.c_str());
  15. i++;
  16. }
  17. }
  18. }
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: How to find End of file

 
2
  #3
Jul 26th, 2006
Echo that and...

-don't use EOF or any of its derivatives.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,350
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: How to find End of file

 
1
  #4
Jul 26th, 2006
here is a pure c++ version that replaces the C-style char array with std::string and removes the tmp variable altogether.

From the looks of this function it must be just a code snippet of a larger more complex function, is that right? If it isn't, then you can remove vector array because it serves no purpose.

  1. ifstream file(filename);
  2. if (file)
  3. {
  4. std::string line;
  5. while ( getline(file,line) )
  6. {
  7. vector <string> array;
  8. istringstream iss(line);
  9. int i=1;
  10. while ( getline(iss, token, ',') )
  11. {
  12. array.push_back(token);
  13. v_List.SetItemText(nItem, i, token.c_str());
  14. i++;
  15. }
  16. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: How to find End of file

 
1
  #5
Jul 26th, 2006
You probably wouldn't need the temporary variable if you use something like this.

  1.  
  2. iifstream file(filename);
  3. string line;
  4. if (file)
  5. {
  6. string token;
  7. stringstream iss;
  8. int i;
  9. while ( getline(file, line) )
  10. {
  11. iss << line;
  12. i = 1;
  13. while ( getline(iss, token, ',') )
  14. {
  15. cout << token << endl;
  16. i++;
  17. }
  18. iss.clear();
  19. }
  20. }
edit:
Dragon has posted a more or less equal version before me. I considered deleting this but decided otherwise because of a subtle difference. That difference is that the stringstream variable is created before the while ( getline(file, line) ) block. I only wanted to refraining from calling the constructor over and over again. But the additional overhead (if any )due to the call of iss << line;and iss.clear(); should be inspected.
Last edited by WolfPack; Jul 26th, 2006 at 11:14 am.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC