string array of unknown size

This is a discussion on string array of unknown size within the C++ Programming forums, part of the General Programming Boards category; Hello all, I am trying to create an array of strings read in from a file; however having no idea ...

  1. #1
    Registered User
    Join Date
    May 2007
    Location
    Melb Australia
    Posts
    7

    string array of unknown size

    Hello all, I am trying to create an array of strings read in from a file; however having no idea how long the file is, means I can't declare the array with a set size, and naturally seg faults occur....

    Code:
    void * server_func(void *t)
    {
       cout << "Server Function: "<< (int)t << endl;
       //read in init_buffer_pages.dat
       ifstream init("test_files/init_buffer_pages.dat");
       string * mem_store;
    
       for(int i = 0; !init.eof(); i++)
       {
          getline(init, mem_store[i], '\n');
          cout << mem_store[i] << endl;
       }
    
       init.close();
    }
    (note, the parameter of the function is passed as void * because I'm doing thread programming.)

    Now, I'm sure there's some dangerously simple solution, but I can't think of it, and scouring the internet hasn't helped yet, so I'm hoping someone here can smack me on the back of the head (forum post style ) and help me out. lol.

    Thank you for your time all.

  2. #2
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    then you can load the file, get its size, build an array based on its size
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  3. #3
    Registered User
    Join Date
    May 2007
    Location
    Melb Australia
    Posts
    7
    and how would i do that? also would doing that be based on lines in the file? because thats what i have to go on..

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Use a vector and push_back each line as you go. The vector grows automatically.

  5. #5
    Registered User
    Join Date
    May 2007
    Location
    Melb Australia
    Posts
    7
    ok sweet will give it a shot!

  6. #6
    Registered User
    Join Date
    May 2007
    Location
    Melb Australia
    Posts
    7
    thanks man, worked a treat!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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