| | |
Parsing a CSV file separated by semicolons.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Sorry to respond to this late... but I wanted to post info also...
The getline() function has the obnoxious habit of returning a not good() stream for final blank fields...
For a single blank line at the end of input, that's fine... (there's no record) but for blank fields it makes a difference. You can get past the problem by checking the stream state before getting a line.
For simple CSV files (meaning you cannot use the ';' character [or whatever character you've chosen] in the field value) this is a working example:
This will allow you to read all seven fields in a record like:
Hope this helps.
The getline() function has the obnoxious habit of returning a not good() stream for final blank fields...
For a single blank line at the end of input, that's fine... (there's no record) but for blank fields it makes a difference. You can get past the problem by checking the stream state before getting a line.
For simple CSV files (meaning you cannot use the ';' character [or whatever character you've chosen] in the field value) this is a working example:
C++ Syntax (Toggle Plain Text)
#include <deque> #include <iostream> #include <sstream> #include <string> typedef std::deque <std::string> record_t; typedef std::deque <record_t> table_t; std::istream& operator >> ( std::istream& ins, table_t& table ) { std::string s; table.clear(); while (std::getline( ins, s )) { std::istringstream ss( s ); record_t record; std::string field; bool final = true; while (std::getline( ss, field, ';' )) { record.push_back( field ); final = ss.eof(); } if (!final) record.push_back( std::string() ); table.push_back( record ); } return ins; }
C++ Syntax (Toggle Plain Text)
one; two;three;four;;six;
•
•
Join Date: Sep 2008
Posts: 24
Reputation:
Solved Threads: 0
•
•
•
•
The only problem with your program is that you need to set local for the stringstream object.
C++ Syntax (Toggle Plain Text)
while (getline(infile, line)) { stringstream strstr(line); string word = ""; strstr.imbue(locale("german_germany.1252")); while (getline(strstr,word, ';')) all_words.push_back(word); }
Attached is the output file I got.
For instances when I do all_words[0] + all_words[2] (all_words[0]=2 and all_words[2]=31), I get 231.
How do I convert each indviudal value of my file so that I can specify it either as int, float, or string?
![]() |
Similar Threads
- insert csv file into mysql through php (PHP)
- converting an ASCII file to a txt(csv) file (C++)
- Parsing a csv file in C (C)
- converting data in a table into csv (comma separated) file (VB.NET)
- Parsing CSV file in partcular format (Java)
- parsing csv file (PHP)
Other Threads in the C++ Forum
- Previous Thread: Inheritance question
- Next Thread: How to disable a button without using MFC
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






