| | |
Parsing a CSV file separated by semicolons.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 24
Reputation:
Solved Threads: 0
Hello! I am trying to open this CSV file separated by semicolons. I know how to open a text file and I tried searching the way to open a CSV, but most methods seemed extremly complicated.
Does any one have a simple suggestion that would work with what I already have.
Does any one have a simple suggestion that would work with what I already have.
#include <iostream>
#include <fstream>
#include <string>
#include <locale>
#include <sstream>
using namespace std;
int main()
{
ifstream in(filename.csv);
ofstream out(...............);
int RID;
int RID_2;
string line;
while( in >> RID >> RID_2)
{
cout << RID << RID_2 << endl;
}
return 0;
} I'd read the file one line at a time and then parse this line to search for semicolons. Getline with a delimiter should do the trick. Here's a sample:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> #include <string> #include <fstream> using namespace std; int main(){ ifstream infile("c:/in.txt"); // for example string line = ""; while (getline(infile, line)){ stringstream strstr(line); string word = ""; while (getline(strstr,word, ';')) cout << word << '\n'; } }
Last edited by niek_e; Jul 17th, 2009 at 10:26 am.
you could use getline() with the third parameter
C++ Syntax (Toggle Plain Text)
string word; while( getline(infile, word, ';' ) { cout << word << "\n"; }
•
•
Join Date: Sep 2008
Posts: 24
Reputation:
Solved Threads: 0
•
•
•
•
you could use getline() with the third parameter
C++ Syntax (Toggle Plain Text)
string word; while( getline(infile, word, ';' ) { cout << word << "\n"; }
Ancient Dragon, your solution was simple and helpful. The only problem is that I don't understand how to separate the string into the separate variables I wanted.
It seems that the strtok() function can be a viable method?
The code below is something I found on how to implement strtok() function. Only I do not quite understand all of the steps that are taken.
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="- This, a sample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
return 0;
}•
•
•
•
The only problem is that I don't understand how to separate the string into the separate variables I wanted.
The code you posted is C not C++. What language are you intending to use?
The problem you posted in #4 is not the same as the problem you originally posted in #1. Are they the same or two different problems? If different problems then you need to tell us that to avoid confusion.
What different variables do you want? If each column of the csv variable represents a string, then just use an array of strings
The above might have problems if there are blank columns where two or more ; in a row, such as "one;;;two" If there are lines like that then it becomes much more complicated and you can't use getline() with that third parmeter.
What different variables do you want? If each column of the csv variable represents a string, then just use an array of strings
C++ Syntax (Toggle Plain Text)
string line; string arry[10]; int i; while( getline(infile, line) ) // Oos! missed a ) { stringstream str(line); for(i = 0; i < 10; i++) getline(str, arry[i], ';'); }
The above might have problems if there are blank columns where two or more ; in a row, such as "one;;;two" If there are lines like that then it becomes much more complicated and you can't use getline() with that third parmeter.
Last edited by Ancient Dragon; Jul 17th, 2009 at 12:16 pm.
•
•
•
•
T
The above might have problems if there are blank columns where two or more ; in a row, such as "one;;;two"
I would highly recommend using a vector in this case. I've made a small adjustment to the code I posted earlier:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> #include <string> #include <fstream> #include <vector> using namespace std; int main(){ ifstream infile("c:/in.txt"); string line = ""; vector<string> all_words; while (getline(infile, line)){ stringstream strstr(line); string word = ""; while (getline(strstr,word, ';')) all_words.push_back(word); } }
After you run this code, all the words will be in the vector. To show the vector use something like:
C++ Syntax (Toggle Plain Text)
for (unsigned i = 0; i < all_words.size(); i++) cout << all_words.at(i) << '\n';
•
•
•
•
It will also stop at 10 words per line.
I would highly recommend using a vector in this case.
After testing, strtok() doesn't work right either because it also skips adjacent semicolons.
Last edited by Ancient Dragon; Jul 17th, 2009 at 12:11 pm.
•
•
Join Date: Sep 2008
Posts: 24
Reputation:
Solved Threads: 0
•
•
•
•
It will also stop at 10 words per line.
I would highly recommend using a vector in this case. I've made a small adjustment to the code I posted earlier:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> #include <string> #include <fstream> #include <vector> using namespace std; int main(){ ifstream infile("c:/in.txt"); string line = ""; vector<string> all_words; while (getline(infile, line)){ stringstream strstr(line); string word = ""; while (getline(strstr,word, ';')) all_words.push_back(word); } }
After you run this code, all the words will be in the vector. To show the vector use something like:
Note that all this code is untested, so it might have a bug or two that I've missed.C++ Syntax (Toggle Plain Text)
for (unsigned i = 0; i < all_words.size(); i++) cout << all_words.at(i) << '\n';
Sorry it has taken me a while to come back to this problem, but I have been away for the weekend. I have decided to go with this solution here because Ancient Dragon also seems to agree that it is better.
As far as the empty spaces and adjacent semicolons go, I did have some, but I redesigned my file to have the empty slots filled with "-1".
I have attached my file, so you can see the way it looks. I am trying to read the file and be able to call the numbers as numbers and the words as strings.
So here is the way things look right now. I use "infile.imbue(locale("german_germany.1252"));" to try and have the program read the commas in as decimal points rather than commas. This worked when I was reading the file the following way:
while( infile >> Var1 >> Var2 ...........), but with using getline, I cannot get this to work. Any suggestions?
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
#include <locale>
using namespace std;
int main()
{
ifstream infile("C:\\Dokumente und Einstellungen\\Yaser\\Eigene Dateien\\Internship\\C++_Code\\All_Data.csv");
ofstream outfile("C:\\Dokumente und Einstellungen\\Yaser\\Eigene Dateien\\Internship\\C++_Code\\Output.txt");
infile.imbue(locale("german_germany.1252"));
string line = "";
vector<string> all_words;
while (getline(infile, line))
{
stringstream strstr(line);
string word = "";
while (getline(strstr,word, ';')) all_words.push_back(word);
}
infile.imbue(locale("german_germany.1252"));
for (unsigned i = 0; i < all_words.size(); i++)
outfile << all_words.at(i) << "\t" << endl;
}Is there anyway to set the file up as a two-dimensional array instead? so that I can have "all_words[....][....]"
Your help is very well appreciated.
The only problem with your program is that you need to set local for the stringstream object.
Attached is the output file I got.
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.
Last edited by Ancient Dragon; Jul 21st, 2009 at 10:25 am.
![]() |
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 |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






