doing a break while reading a .txt
Dec 23, 2014 at 6:56pm UTC
I am reading a Textfile and It looks like this:
1 2 3 4 5 6 7 8
//*test.txt*//
brick1
10,11
20,20
30,30
/////////////
I want to break the read of the file, if it reads this character: ","
My code looks like this at the moment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
vector<string> cout_text;
ifstream fileMap("test.txt" );
string line;
while (getline(fileMap, line))
{
cout_text.push_back(line);
}
/*now i can open the stored value i want*/
cout << cout_text[3] << endl;
cout << cout_text[0] << endl;
cout << cout_text[1] << endl;
getchar();
}
I want to save the values in front of and after the "," in the vector, after that it should go to the next line.
To that I want to check if I have saved in one of these a letter or a word.
So I can only give back the whole line. Does anyone know a solution?
Last edited on Dec 23, 2014 at 7:16pm UTC
Dec 24, 2014 at 4:03am UTC
you just need to parse the line that you read.
i would create two vectors one for the left values and one for the right values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// for example you read line
string line = "10,11" ;
string delimiter = "," ;
std::string token;
size_t pos = 0;
while ((pos = line.find(delimiter)) != std::string::npos)
{
// extract the first value
token = line.substr(0, pos);
std::cout << token << std::endl;
line.erase(0, pos + delimiter.length());
// insert token to vector
// example left.push_back(token)
// do extra check here for letters etc.
}
// insert 2nd value to vector
// example right.push_back(line)
std::cout << line;
Last edited on Dec 24, 2014 at 4:04am UTC
Dec 24, 2014 at 10:23am UTC
I can only read the right side of the values if I read a document
Dec 24, 2014 at 10:25am UTC
sorry i don't understand what you want to do.
Dec 24, 2014 at 11:10am UTC
Im sorry, thank's for help.
1 2
I have forgotten to write "pos =" in
while ((pos = line.find(delimiter)) != std::string::npos).
Last edited on Dec 24, 2014 at 11:10am UTC
Topic archived. No new replies allowed.