| | |
How to find End of file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2006
Posts: 10
Reputation:
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 !
try
C++ Syntax (Toggle Plain Text)
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++; } } }
バルサミコ酢やっぱいらへんで
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.
C++ Syntax (Toggle Plain Text)
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++; } }
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
C++ Syntax (Toggle Plain Text)
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(); } }
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.
バルサミコ酢やっぱいらへんで
![]() |
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
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






