Hello,

I have to read from a .txt file which has info as such,

10:5:23
2:343:54
1:4:7 and so on.

3 Columns, lots of rows.

How can i read each column for example 10 to x, 5 to y, 23 to z.

Recommended Answers

All 10 Replies

Why not use strtok function to split each row, using ' : ' as separator?
http://www.cplusplus.com/reference/clibrary/cstring/strtok/

Well, there seems to be something wrong.

it gives error here

void reader()
{
	string holdInput ="";
	char *tokenizedInput;
	input.open("C:/Users/Yigit/Desktop/emre/input.txt");
	if(input.is_open())
		cout<<" opened succesfully"<<endl;
	else
		cout<<" epic fail"<<endl;
	
	while (! input.eof() )
               {
                  getline (input,holdInput);
	}
	tokenizedInput = strtok (holdInput," :");
input.close();
	
}

Do i have to do something after tokenizing to seperate the values.


As you may have noticed, i am relatively new to C++.
Thanks

I think its erroring because its expecting a cstr not a std::string

try

strtok(holdInput.cstr(), " :");

cannot convert parameter 1 from const char* to char*

cannot convert parameter 1 from const char* to char*

Is there some other way where i can read the info from a file and tokenize each line to 3 different values. In java it is deadly simple but i dont understand why its such a burden in c++

Glad to be of help :)

Glad to be of help :)

I still think there should be some function which gets number upto : then puts it in x, then reads on and gets the next up to : and puts it in to y, etc...

Take a look at this thread:http://www.daniweb.com/forums/post246417.html#post246417
Substitute ':' for ',' in the delimiter of the second getline call and you should have what you need (minus fixing it to work with your arrays,etc). You can use an N x 3 array for holding the strings or just do your parsing for ints on the fly. See this for more info on the stringstream >> operator.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.