...

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



amol0010
09-07-2009, 12:09 AM
Hi,

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..


Thanks.

BWiz
09-07-2009, 03: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?

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



your_file >> number1 >> number2;

BrickInTheWall
09-07-2009, 06:43 AM
Incase you are new to I/O with C++, I think this should be able to help you:
http://cplusplus.com/doc/tutorial/files/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum