Format in put text file

Discussion in 'C++' started by victor.herasme@gmail.com, Aug 30, 2008.

  1. Guest

    Hi,

    I hava an input file structured like this:

    X XYData-1

    1. 3.08333
    2. 9.05526
    3. 3.13581
    .......

    X XYData-2

    1. 4.08322
    2. 4.02526
    3. 3.95891
    ...............

    i want to format it so i only get the second column, in order to place
    it in a mxn matrix. Let's say i want this:

    number1 number2 number3..NumberN
    number4 number5 number6
    ...........................................................

    number1 number2 number3..NumberN

    i am trying to use GETLINE to input numbers in the second column and
    IGNORE in order to omit the X etc. Yet i need a little help on how to
    size the array if i do not know how large it will be in advance. Can
    anyone ehlp me with this? Thanks,

    Victor
     
    , Aug 30, 2008
    #1
    1. Advertising

  2. Salt_Peter Guest

    On Aug 30, 1:12 pm, ""
    <> wrote:
    > Hi,
    >
    > I hava an input file structured like this:
    >
    > X XYData-1
    >
    > 1. 3.08333
    > 2. 9.05526
    > 3. 3.13581
    > .......
    >
    > X XYData-2
    >
    > 1. 4.08322
    > 2. 4.02526
    > 3. 3.95891
    > ...............
    >
    > i want to format it so i only get the second column, in order to place
    > it in a mxn matrix. Let's say i want this:
    >
    > number1 number2 number3..NumberN
    > number4 number5 number6
    > ..........................................................
    >
    > number1 number2 number3..NumberN
    >
    > i am trying to use GETLINE to input numbers in the second column and
    > IGNORE in order to omit the X etc. Yet i need a little help on how to
    > size the array if i do not know how large it will be in advance. Can
    > anyone ehlp me with this? Thanks,
    >
    > Victor



    Arrays are prehistoric, consider std::vector.
    You'll rarely ever use an array again.
    The std::vector is a sequenced container which is dynamic and holds
    elements in contiguous memory.
    The only requirement is that the type stored be copyable and
    assigneable.

    see http://www.sgi.com/tech/stl/Vector.html
     
    Salt_Peter, Aug 30, 2008
    #2
    1. Advertising

  3. Jorgen Grahn Guest

    On Sat, 30 Aug 2008 10:23:14 -0700 (PDT), Salt_Peter <> wrote:
    > On Aug 30, 1:12 pm, ""
    > <> wrote:
    >> Hi,
    >>
    >> I hava an input file structured like this:

    ....
    >> i am trying to use GETLINE to input numbers in the second column and
    >> IGNORE in order to omit the X etc. Yet i need a little help on how to
    >> size the array if i do not know how large it will be in advance. Can
    >> anyone ehlp me with this? Thanks,
    >>
    >> Victor

    >
    >
    > Arrays are prehistoric, consider std::vector.
    > You'll rarely ever use an array again.


    Actually, I think he wants std::string, to read a line of text into.

    std::getline(std::istream&, std::string&)

    I couldn't understand the formulation of the problem, but it seems
    like a traditional one-line-in, one-line-out filter, so there's
    nothing to store.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
    \X/ snipabacken.se> R'lyeh wgah'nagl fhtagn!
     
    Jorgen Grahn, Sep 8, 2008
    #3
  4. Salt_Peter wrote:
    > Arrays are prehistoric, consider std::vector.
    > You'll rarely ever use an array again.


    std::vector has its disadvantages, though.

    For example, if you have a pointer to an element, and then you add new
    elements to the vector, your pointer might get invalidated (or not,
    there's no way of knowing). Also the memory usage of std::vector might
    not alway be optimal, especially if you are constantly adding new
    individual elements to it.

    In this respect std::deque is better: Adding elements (at the
    beginning or the end) doesn't invalidate pointers pointing to existing
    elements, and the memory usage is often better in the latter situation.
    (OTOH the memory is not contiguous.)
     
    Juha Nieminen, Sep 8, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page