User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,794 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,359 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 5744 | Replies: 4
Reply
Join Date: Jul 2006
Posts: 10
Reputation: rdubey_jsr is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
rdubey_jsr rdubey_jsr is offline Offline
Newbie Poster

How to find End of file

  #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 !
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,480
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 98
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: How to find End of file

  #2  
Jul 26th, 2006
try
ifstream file(filename);
if (file)
{
    while ( 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++;
        }
    }
}
バルサミコ酢やっぱいらへんで
Reply With Quote  
Join Date: Aug 2005
Posts: 4,697
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 17
Solved Threads: 308
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: How to find End of file

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

-don't use EOF or any of its derivatives.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,643
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 867
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: How to find End of file

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

ifstream file(filename);
if (file)
{
    std::string line;
    while ( getline(file,line) )
    {
       vector <string> array;
        istringstream iss(line);
        int i=1;
        while ( getline(iss, token, ',') )
        {
            array.push_back(token);
            v_List.SetItemText(nItem, i, token.c_str());
            i++;
        }
 }
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,480
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 98
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: How to find End of file

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

    
iifstream    file(filename);
string      line;
if (file)
{
    string token;
    stringstream iss;
    int i;
    while ( getline(file, line) )
    {
        iss << line;
        i = 1;
        while ( getline(iss, token, ',') )
        {
            cout << token << endl;
            i++;
        }
        iss.clear();
    }
}

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 10:14 am.
バルサミコ酢やっぱいらへんで
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:44 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC