Thread: Reading from a textfile with unknown length?

  1. #1
    Registered User
    Join Date
    Oct 2010

    Reading from a textfile with unknown length?

    [c++] Need help with pointers?
    Hi I want to make a program that can read data from a text file into memory.

    Here is the format of the text file:

    So text file 1 could be:

    And text file 2 could be:


    I have created a struct called
    struct Car
    int quantity;
    char brand[20];
    double cost;
    Now how I input data into the struct if I don't even know how big the struct array size should be?

    In my main I have Car Cars[]; declared but I have left the brackets empty because I don't know what the size it should be.

    Please help me with this.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Why use an array if you don't know how many you're going to need? You should look into other containers, such as vector.

  3. #3
    Registered User
    Join Date
    Jun 2005
    No need to use pointers at all.

    Create a std::vector<Car>. Open file. Read one Car from the file, and append it to the vector. Repeat (read another Car and append) until whole file read. Close file. Done.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Ah I see. Thank you so much!

  5. #5
    Registered User
    Join Date
    May 2011
    Around 8.3 light-minutes from the Sun
    Additionally, you shouldn't be using a char array here either; just use std::string for your brand name.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading textfile into array
    By a.mlw.walker in forum C Programming
    Replies: 9
    Last Post: 08-14-2011, 07:39 PM
  2. Calculating Sum of Integers of Unknown Length
    By egnorth in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2010, 04:31 PM
  3. reading a file of unknown length
    By the bassinvader in forum C Programming
    Replies: 2
    Last Post: 07-12-2006, 03:06 PM
  4. How to declare global arrays of unknown length?
    By ajm218 in forum C Programming
    Replies: 3
    Last Post: 08-07-2003, 09:13 AM
  5. turning strings of unknown length into arrays
    By hankspears in forum C Programming
    Replies: 3
    Last Post: 04-11-2002, 12:16 PM