Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/++/# Extracting information of data in column format using C++

  1. Nov 17, 2016 #1
    Hello friends, how are you ?

    Today's question is: How do i choose the data i want to extract from a file in c++ ?

    I have this data file in columns format, something like:

    Code (Text):
    x      y      z
    1      2      3
    4      5      6
    7      8      9
    I know there is the command file.getline() , and this command can be manipulated to get the string the size i want, but there is a way of skip the space between the numbers and catch only the data of the second column?

    And plus, there are any way to get it not in str format ? but in a float or a double format ?

    Thanks for the tips :wink:
     
  2. jcsd
  3. Nov 17, 2016 #2

    DrClaude

    User Avatar

    Staff: Mentor

    What about the >> operator?
     
  4. Nov 17, 2016 #3
    Could you give any example of using >> ?
    It works like cin, right ? But how do i command it to get information of an specific line and column ?
     
  5. Nov 17, 2016 #4

    DrClaude

    User Avatar

    Staff: Mentor

    It works with cin. or any istream. Each >> will read data until it reaches a blank space, so columns are easy to take care of.

    I'm sure you can find good examples online.
     
  6. Nov 17, 2016 #5
    Got it, thanks mate, it worked for me.

    Just for finishing, how do i reset the command ? For start to read the first line again ?
     
  7. Nov 17, 2016 #6

    Mark44

    Staff: Mentor

    Close the file and open it again. The position-in-file pointer will now be at the start of the file.

    The C standard library (as opposed to the C++ Standard Library) has a rewind() function. With this function you don't have to close and then reopen the file.

    The C++ basic_filebuf class has a seekpos() function. You can use it to move the position-in-file pointer to a selected location in the file -- to the beginning or to the end, or to some point within the file. There is also a seekoff() function that can be used for a similar purpose.
     
    Last edited: Nov 17, 2016
  8. Nov 17, 2016 #7
    Thanks for the attentions, Mark44 and DrClaude. I'm grateful.
     
  9. Nov 18, 2016 #8
    Personally, I'd read the entire thing into a buffer, then parse it into a data structure. That way you can later do something like:

    Code (Text):
    item = chart.x(0);
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Extracting information of data in column format using C++
  1. C: Format Specifier %f (Replies: 1)

Loading...