View Full Version : Reading tabbed variables from a file - c++

09-07-2009, 01:09 AM

I was wondering how I could reading a set of variables from a text file. The first set of variables are separated by a tab. e.g. 45 and 7.5. Then a newline follows for a fresh set of variables.

e.g. my text file reads as:

45 7.5
50 9.0
60 11.0

I have 2 variables num1 and num2 in an array and then I set the values of nu1 and num2 from the hours and $/hr columns for each iteration.

for e.g. for the first iteration in the for loop, the value of num1=45 and num2=7.5.

For the 2nd loop, num1 = 50 and num2 = 9.0

and so on..


09-07-2009, 04:28 AM
Yes, it is possible, I can't think of the exactly how to implement it [got the cold ='(], but you would use file I/O (http://www.cplusplus.com/doc/tutorial/files/) and possibly use streams (cin.*)? Or regular expressions?

09-07-2009, 05:15 AM
It should be as simple as using the extraction operator on your file object.

your_file >> number1 >> number2;

09-07-2009, 07:43 AM
Incase you are new to I/O with C++, I think this should be able to help you: