Parsing a CSV file separated by semicolons.

 
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.

#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;
}

Featured Replies in this Discussion

  • ... yes, that is a better solution. But, like mine, it doesn't work when there are two or more adjacient semicolons (or some other column separator). After testing, strtok() doesn't work right either because it also skips adjacent semicolons.
    Post Contains: Quotes
  • The only problem with your program is that you need to set local for the stringstream object. ... Attached is the output file I got.
    Post Contains: Code
  • 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…
    Post Contains: Code
  • ... Simple substitution -- call find() to locate the comma and replace it with a period.
    Post Contains: Quotes
 
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:

#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';
    }
}
 
0
 

you could use getline() with the third parameter

string word;
while( getline(infile, word, ';' )
{
    cout << word << "\n";
}
Comments
Too slow, but it's nice to know that great minds think alike ;)
 
0
 

you could use getline() with the third parameter

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;
}
 
0
 

The only problem is that I don't understand how to separate the string into the separate variables I wanted.

Why don't try the piece of code I posted earlier? It does just that.

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.

The code you posted is C not C++. What language are you intending to use?

 
0
 

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

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.

Comments
Again forgotten a parenthese in the while :P (Also see post #3)
 
0
 

T
The above might have problems if there are blank columns where two or more ; in a row, such as "one;;;two"

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:

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

for (unsigned i = 0; i < all_words.size(); i++)
        cout << all_words.at(i) << '\n';

Note that all this code is untested, so it might have a bug or two that I've missed.

 
0
 

It will also stop at 10 words per line.
I would highly recommend using a vector in this case.

yes, that is a better solution. But, like mine, it doesn't work when there are two or more adjacient semicolons (or some other column separator).

After testing, strtok() doesn't work right either because it also skips adjacent semicolons.

 
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:

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

for (unsigned i = 0; i < all_words.size(); i++)
        cout << all_words.at(i) << '\n';

Note that all this code is untested, so it might have a bug or two that I've missed.

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.

Attachments Sample1.txt (7.48 KB)
2;-4,670450021;31;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;studies_map;00:00:00.693;0,693;-1;0,260684;0,925606;-0,571543319;414005,1004;5317097,583;0;0;0;0;0;000001_000001;-1;-1;2;0;12,769;0;0;414006,4739;5317102,047;6,705111848
2;-3,738655469;32;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;manpos;00:00:00.721;0,721;-1;0,260684;0,922145;-0,56940622;414005,3513;5317098,481;0;0;0;0;0;-1;-1;-1;2;0;0;0;0;414006,4508;5317102,054;6,729328402
2;-4,198294889;33;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;standing_1s;00:00:01.160;1,16;-1;0,264957;0,923875;-0,570474461;414007,0195;5317097,487;0;0;0;0;0;-1;-1;000001_000001;2;0;0;0;6,919;414008,2541;5317101,499;4,842511269
2;-3,266770307;34;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;walking;00:00:08.079;8,079;-1;0,264957;0,920415;-0,56833798;414007,2703;5317098,384;0;0;0;0;0;-1;-1;000001_000001;2;0;0;0;6,919;414008,231;5317101,506;4,866721131
2;-1,443485003;35;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;map_removed;00:00:13.462;13,462;-1;0,237179;0,913495;-0,564065017;413996,112;5317103,725;0;0;0;0;0;000001_000001;-1;-1;2;0;12,769;0;0;413996,5365;5317105,105;17,10231471
2;-1,900066769;36;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;manpos;00:00:13.939;13,939;-1;0,24359;0,915225;-0,565133258;413998,6776;5317102,458;0;0;0;0;0;-1;-1;-1;2;0;0;0;0;413999,2364;5317104,274;14,27748221
2;-0,379327603;37;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;manpos;00:00:29.855;29,855;-1;0,202991;0,911765;-0,562996776;413981,8868;5317108,538;0;0;0;0;0;-1;-1;-1;2;0;0;0;0;413981,936;5317108,914;32,21426794
2;-3,323222498;38;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;manpos;00:00:36.875;36,875;-1;0,188034;0,929066;-0,5736798;413974,3544;5317105,96;0;0;0;0;0;-1;-1;-1;2;0;0;0;0;413974,3565;5317109,283;39,81061021
2;-1,898776166;39;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;studies_map;00:00:49.628;49,628;-1;0,150641;0,922145;-0,56940622;413959,1602;5317112,529;0;0;0;0;0;000001_000002;-1;-1;2;0;2,616;0;0;413960,2077;5317114,113;55,01667225
2;-0,320299824;40;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;map_removed;00:00:52.244;52,244;-1;0,141026;0,910035;-0,561928536;413956,0022;5317116,897;0;0;0;0;0;000001_000002;-1;-1;2;0;2,616;0;0;413956,2561;5317117,093;59,98683016
2;-1,079438296;41;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;studies_map;00:00:54.524;54,524;-1;0,134615;0,897924;-0,554450234;413954,189;5317120,857;0;0;0;0;0;000001_000003;-1;-1;2;0;5,04;0;0;413955,2606;5317120,727;63,8516161
2;-0,869658095;42;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;manpos;00:00:54.612;54,612;-1;0,142094;0,913495;-0,564065017;413956,1996;5317115,864;0;0;0;0;0;-1;-1;-1;2;0;0;0;0;413956,7951;5317116,497;59,18303746
2;-1,091788733;43;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;standing_1s;00:00:55.650;55,65;-1;0,141026;0,913495;-0,564065017;413955,7513;5317116;0;0;0;0;0;-1;-1;000001_000002;2;0;0;0;3,196;413956,5521;5317116,742;59,52787362
2;-0,618155105;44;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;walking;00:00:58.846;58,846;-1;0,135684;0,897924;-0,554450234;413954,6378;5317120,721;0;0;0;0;0;-1;-1;000001_000002;2;0;0;0;3,196;413955,2524;5317120,655;63,77911125
2;-0,715900754;45;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;map_removed;00:00:59.564;59,564;-1;0,135684;0,889273;-0,549108413;413955,2649;5317122,965;0;0;0;0;0;000001_000003;-1;-1;2;0;5,04;0;0;413955,8857;5317122,608;65,8469308
2;1,380151606;46;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;studies_map;00:01:06.593;66,593;-1;0,151709;0,865052;-0,534152427;413963,7472;5317127,201;0;0;0;0;0;000001_000004;-1;-1;2;0;10,397;0;0;413962,96;5317128,335;74,99874366
2;1,361041925;47;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;manpos;00:01:07.333;67,333;-1;0,147436;0,873702;-0,53949363;413961,3266;5317125,503;0;0;0;0;0;-1;-1;-1;2;0;0;0;0;413960,5253;5317126,603;72,01102848
2;0,864530337;48;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;map_removed;00:01:16.990;76,99;-1;0,159188;0,847751;-0,523469403;413968,1407;5317130,734;0;0;0;0;0;000001_000004;-1;-1;2;0;10,397;0;0;413967,6783;5317131,465;80,66103411
2;0,276501934;49;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;manpos;00:01:17.170;77,17;-1;0,160256;0,842561;-0,520264681;413968,9652;5317131,944;0;0;0;0;0;-1;-1;-1;2;0;0;0;0;413968,8197;5317132,179;82,00763348
2;-1,110357783;50;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;manpos;00:01:31.139;91,139;-1;0,183761;0,797578;-0,492488572;413982,0923;5317140,611;0;0;0;0;0;-1;-1;-1;2;0;0;0;0;413982,5619;5317139,605;97,63679427
2;-0,373030088;51;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;manpos;00:01:38.936;98,936;-1;0,201923;0,776817;-0,479669067;413991,2209;5317143,677;0;0;0;0;0;-1;-1;-1;2;0;0;0;0;413991,3539;5317143,329;107,1871266
2;-1,131850433;52;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;studies_map;00:01:39.307;99,307;-1;0,213675;0,759516;-0,468986043;413997,408;5317146,665;0;0;0;0;0;000001_000005;-1;-1;2;0;3,42;0;0;413997,7572;5317145,588;113,9779274
2;-0,620508453;53;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;map_removed;00:01:42.727;102,727;-1;0,226496;0,747405;-0,461507741;414003,6676;5317148,169;0;0;0;0;0;000001_000005;-1;-1;2;0;3,42;0;0;414003,899;5317147,594;120,4411081
2;1,739203532;54;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;manpos;00:01:47.144;107,144;-1;0,229701;0,754325;-0,465780704;414004,5113;5317145,965;0;0;0;0;0;-1;-1;-1;2;0;0;0;0;414003,8644;5317147,58;120,4038306
2;-0,747288315;55;Hella_Bredel;None;1;Ikonische Karte;15. Okt 08;/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml;18:46:45.963;manpos;00:01:56.108;116,108;-1;0,245726;0,709343;-0,438005212;414014,4986;5317155,587;0;0;0;0;0;-1;-1;-1;2;0;0;0;0;414014,9764;5317155,012;133,8551661
 
0
 

The only problem with your program is that you need to set local for the stringstream object.

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.

Attachments Output.txt (9.17 KB)
2	
-4,670450021	
31	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
studies_map	
00:00:00.693	
0,693	
-1	
0,260684	
0,925606	
-0,571543319	
414005,1004	
5317097,583	
0	
0	
0	
0	
0	
000001_000001	
-1	
-1	
2	
0	
12,769	
0	
0	
414006,4739	
5317102,047	
6,705111848	
2	
-3,738655469	
32	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
manpos	
00:00:00.721	
0,721	
-1	
0,260684	
0,922145	
-0,56940622	
414005,3513	
5317098,481	
0	
0	
0	
0	
0	
-1	
-1	
-1	
2	
0	
0	
0	
0	
414006,4508	
5317102,054	
6,729328402	
2	
-4,198294889	
33	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
standing_1s	
00:00:01.160	
1,16	
-1	
0,264957	
0,923875	
-0,570474461	
414007,0195	
5317097,487	
0	
0	
0	
0	
0	
-1	
-1	
000001_000001	
2	
0	
0	
0	
6,919	
414008,2541	
5317101,499	
4,842511269	
2	
-3,266770307	
34	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
walking	
00:00:08.079	
8,079	
-1	
0,264957	
0,920415	
-0,56833798	
414007,2703	
5317098,384	
0	
0	
0	
0	
0	
-1	
-1	
000001_000001	
2	
0	
0	
0	
6,919	
414008,231	
5317101,506	
4,866721131	
2	
-1,443485003	
35	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
map_removed	
00:00:13.462	
13,462	
-1	
0,237179	
0,913495	
-0,564065017	
413996,112	
5317103,725	
0	
0	
0	
0	
0	
000001_000001	
-1	
-1	
2	
0	
12,769	
0	
0	
413996,5365	
5317105,105	
17,10231471	
2	
-1,900066769	
36	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
manpos	
00:00:13.939	
13,939	
-1	
0,24359	
0,915225	
-0,565133258	
413998,6776	
5317102,458	
0	
0	
0	
0	
0	
-1	
-1	
-1	
2	
0	
0	
0	
0	
413999,2364	
5317104,274	
14,27748221	
2	
-0,379327603	
37	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
manpos	
00:00:29.855	
29,855	
-1	
0,202991	
0,911765	
-0,562996776	
413981,8868	
5317108,538	
0	
0	
0	
0	
0	
-1	
-1	
-1	
2	
0	
0	
0	
0	
413981,936	
5317108,914	
32,21426794	
2	
-3,323222498	
38	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
manpos	
00:00:36.875	
36,875	
-1	
0,188034	
0,929066	
-0,5736798	
413974,3544	
5317105,96	
0	
0	
0	
0	
0	
-1	
-1	
-1	
2	
0	
0	
0	
0	
413974,3565	
5317109,283	
39,81061021	
2	
-1,898776166	
39	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
studies_map	
00:00:49.628	
49,628	
-1	
0,150641	
0,922145	
-0,56940622	
413959,1602	
5317112,529	
0	
0	
0	
0	
0	
000001_000002	
-1	
-1	
2	
0	
2,616	
0	
0	
413960,2077	
5317114,113	
55,01667225	
2	
-0,320299824	
40	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
map_removed	
00:00:52.244	
52,244	
-1	
0,141026	
0,910035	
-0,561928536	
413956,0022	
5317116,897	
0	
0	
0	
0	
0	
000001_000002	
-1	
-1	
2	
0	
2,616	
0	
0	
413956,2561	
5317117,093	
59,98683016	
2	
-1,079438296	
41	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
studies_map	
00:00:54.524	
54,524	
-1	
0,134615	
0,897924	
-0,554450234	
413954,189	
5317120,857	
0	
0	
0	
0	
0	
000001_000003	
-1	
-1	
2	
0	
5,04	
0	
0	
413955,2606	
5317120,727	
63,8516161	
2	
-0,869658095	
42	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
manpos	
00:00:54.612	
54,612	
-1	
0,142094	
0,913495	
-0,564065017	
413956,1996	
5317115,864	
0	
0	
0	
0	
0	
-1	
-1	
-1	
2	
0	
0	
0	
0	
413956,7951	
5317116,497	
59,18303746	
2	
-1,091788733	
43	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
standing_1s	
00:00:55.650	
55,65	
-1	
0,141026	
0,913495	
-0,564065017	
413955,7513	
5317116	
0	
0	
0	
0	
0	
-1	
-1	
000001_000002	
2	
0	
0	
0	
3,196	
413956,5521	
5317116,742	
59,52787362	
2	
-0,618155105	
44	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
walking	
00:00:58.846	
58,846	
-1	
0,135684	
0,897924	
-0,554450234	
413954,6378	
5317120,721	
0	
0	
0	
0	
0	
-1	
-1	
000001_000002	
2	
0	
0	
0	
3,196	
413955,2524	
5317120,655	
63,77911125	
2	
-0,715900754	
45	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
map_removed	
00:00:59.564	
59,564	
-1	
0,135684	
0,889273	
-0,549108413	
413955,2649	
5317122,965	
0	
0	
0	
0	
0	
000001_000003	
-1	
-1	
2	
0	
5,04	
0	
0	
413955,8857	
5317122,608	
65,8469308	
2	
1,380151606	
46	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
studies_map	
00:01:06.593	
66,593	
-1	
0,151709	
0,865052	
-0,534152427	
413963,7472	
5317127,201	
0	
0	
0	
0	
0	
000001_000004	
-1	
-1	
2	
0	
10,397	
0	
0	
413962,96	
5317128,335	
74,99874366	
2	
1,361041925	
47	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
manpos	
00:01:07.333	
67,333	
-1	
0,147436	
0,873702	
-0,53949363	
413961,3266	
5317125,503	
0	
0	
0	
0	
0	
-1	
-1	
-1	
2	
0	
0	
0	
0	
413960,5253	
5317126,603	
72,01102848	
2	
0,864530337	
48	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
map_removed	
00:01:16.990	
76,99	
-1	
0,159188	
0,847751	
-0,523469403	
413968,1407	
5317130,734	
0	
0	
0	
0	
0	
000001_000004	
-1	
-1	
2	
0	
10,397	
0	
0	
413967,6783	
5317131,465	
80,66103411	
2	
0,276501934	
49	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
manpos	
00:01:17.170	
77,17	
-1	
0,160256	
0,842561	
-0,520264681	
413968,9652	
5317131,944	
0	
0	
0	
0	
0	
-1	
-1	
-1	
2	
0	
0	
0	
0	
413968,8197	
5317132,179	
82,00763348	
2	
-1,110357783	
50	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
manpos	
00:01:31.139	
91,139	
-1	
0,183761	
0,797578	
-0,492488572	
413982,0923	
5317140,611	
0	
0	
0	
0	
0	
-1	
-1	
-1	
2	
0	
0	
0	
0	
413982,5619	
5317139,605	
97,63679427	
2	
-0,373030088	
51	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
manpos	
00:01:38.936	
98,936	
-1	
0,201923	
0,776817	
-0,479669067	
413991,2209	
5317143,677	
0	
0	
0	
0	
0	
-1	
-1	
-1	
2	
0	
0	
0	
0	
413991,3539	
5317143,329	
107,1871266	
2	
-1,131850433	
52	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
studies_map	
00:01:39.307	
99,307	
-1	
0,213675	
0,759516	
-0,468986043	
413997,408	
5317146,665	
0	
0	
0	
0	
0	
000001_000005	
-1	
-1	
2	
0	
3,42	
0	
0	
413997,7572	
5317145,588	
113,9779274	
2	
-0,620508453	
53	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
map_removed	
00:01:42.727	
102,727	
-1	
0,226496	
0,747405	
-0,461507741	
414003,6676	
5317148,169	
0	
0	
0	
0	
0	
000001_000005	
-1	
-1	
2	
0	
3,42	
0	
0	
414003,899	
5317147,594	
120,4411081	
2	
1,739203532	
54	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
manpos	
00:01:47.144	
107,144	
-1	
0,229701	
0,754325	
-0,465780704	
414004,5113	
5317145,965	
0	
0	
0	
0	
0	
-1	
-1	
-1	
2	
0	
0	
0	
0	
414003,8644	
5317147,58	
120,4038306	
2	
-0,747288315	
55	
Hella_Bredel	
None	
1	
Ikonische Karte	
15. Okt 08	
/home/versuchsleiter/wayTracer/madway/trunk/Design_Videologging.xml	
18:46:45.963	
manpos	
00:01:56.108	
116,108	
-1	
0,245726	
0,709343	
-0,438005212	
414014,4986	
5317155,587	
0	
0	
0	
0	
0	
-1	
-1	
-1	
2	
0	
0	
0	
0	
414014,9764	
5317155,012	
133,8551661
 
4
 

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:

#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;
  }

This will allow you to read all seven fields in a record like:

one; two;three;four;;six;

Hope this helps.

Comments
Nice :)
welcome back blender man
Haven't seen you in a while. Nice post.
Great example :)
 
0
 

The only problem with your program is that you need to set local for the stringstream object.

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.

Hi Ancient Dragon. I see that this makes sense, but the commas in the numbers are not being replaced with decimals. I think the reason for this is that things are still in strings and I need to convert them to integer and float values.

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?

 
0
 

I will actually mark this thread as solved and post it as a different problem, because it may be useful for other people. Thanks for the help everyone!

Question Answered as of 5 Years Ago by Ancient Dragon, Nick Evan and Duoas
 
0
 

How do I convert each indviudal value of my file so that I can specify it either as int, float, or string?

Simple substitution -- call find() to locate the comma and replace it with a period.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article