General data input question (from a beginner)::(file i/o)

This is a discussion on General data input question (from a beginner)::(file i/o) within the C++ Programming forums, part of the General Programming Boards category; General data input question (from a beginner): I am trying to read data from a file into a data class. ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    38

    General data input question (from a beginner)::(file i/o)

    General data input question (from a beginner):

    I am trying to read data from a file into a data class. Each line of the data file would be it's own data object. So if I had ten lines then I would init my data object like so: DataObject* ptrDataObj = new DataObject[10] . But how do I determine how many lines are in a file without reading through the whole file twice?

    another question extremely related:

    Do I have to initalize my data object to a specific size, what if I am reading from a file (such as a shape file [with multiple shapes] that has an "id" and then "n" x,y points). In java I would just use a vector, what should I do in c++ (do I HAVE to use a c++ vector)?
    ex. data file:
    id1
    x y
    x y
    x y
    id2
    x y
    id3
    x y
    x y
    ...
    [eof]

    -Thanks
    chris

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >But how do I determine how many lines are in a file without reading through the whole file twice?
    Use a std::vector and push_back a new object for every line. That way it doesn't matter whether you know how many lines there are or not. But if you do you can set the capacity to that number and avoid reallocations inside the vector.

    >(do I HAVE to use a c++ vector)?
    No, but it's way easier to do so.
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    One solution is a map within a map.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 12:09 PM
  4. Question about File Input
    By ChristianTool in forum C Programming
    Replies: 12
    Last Post: 04-25-2004, 03:40 PM
  5. Handling input errors, general question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-08-2001, 06:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21