Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    61
    Thanks
    10
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts
    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.

  • #3
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    It should be as simple as using the extraction operator on your file object.


    Code:
    your_file >> number1 >> number2;
    OracleGuy

  • #4
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    Incase you are new to I/O with C++, I think this should be able to help you:
    http://cplusplus.com/doc/tutorial/files/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •