Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-07-2009, 12:09 AM   PM User | #1
amol0010
New Coder

 
Join Date: Jul 2007
Posts: 61
Thanks: 10
Thanked 0 Times in 0 Posts
amol0010 is an unknown quantity at this point
Reading tabbed variables from a file - c++

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.
amol0010 is offline   Reply With Quote
Old 09-07-2009, 03:28 AM   PM User | #2
BWiz
Regular Coder

 
BWiz's Avatar
 
Join Date: Mar 2006
Location: Sol System
Posts: 471
Thanks: 7
Thanked 21 Times in 21 Posts
BWiz is an unknown quantity at this point
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 and possibly use streams (cin.*)? Or regular expressions?
__________________
BWiz :: Happy Coding!
2006
2007 2008 2009
2010 2011
Irrational numbers make no sense.
BWiz is offline   Reply With Quote
Old 09-07-2009, 04:15 AM   PM User | #3
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
It should be as simple as using the extraction operator on your file object.


Code:
your_file >> number1 >> number2;
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Old 09-07-2009, 06:43 AM   PM User | #4
BrickInTheWall
Regular Coder

 
BrickInTheWall's Avatar
 
Join Date: Mar 2009
Location: Munich, Germany
Posts: 139
Thanks: 1
Thanked 13 Times in 13 Posts
BrickInTheWall is on a distinguished road
Incase you are new to I/O with C++, I think this should be able to help you:
http://cplusplus.com/doc/tutorial/files/
BrickInTheWall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:34 PM.


Advertisement
Log in to turn off these ads.