•
•
•
•
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
![]() |
•
•
Join Date: Jul 2006
Posts: 10
Reputation:
Rep Power: 3
Solved Threads: 0
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.
Help me !
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 !
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,480
Reputation:
Rep Power: 8
Solved Threads: 98
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++;
}
}
} バルサミコ酢やっぱいらへんで
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,643
Reputation:
Rep Power: 36
Solved Threads: 867
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.
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++;
}
}•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,480
Reputation:
Rep Power: 8
Solved Threads: 98
You probably wouldn't need the temporary variable if you use something like this.
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
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.
バルサミコ酢やっぱいらへんで
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- warning:no newline at end of file (C)
- no newline at end of file error (C++)
- ERROR--Unexpected end to file (C++)
- Find word in file!! (C++)
- C++ complete binary tree using an array. Unexpected end file (C++)
- help in using End-OF-File function (C)
- about:blank hijack (can't find any .dll-file) (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: how to cast a string type variable to LPCTSTR type
- Next Thread: Constructors



Linear Mode