Thread: fstream problem (or it's the vector?)

  1. #1
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470

    fstream problem (or it's the vector?)

    Look at this piece of code please:
    Code:
    ......
    ......
    int main(int argc, char* argv[])
    {
    vector<char*> Elibdat;
    fstream Efs, Sfs, sfs, ofs;
    Efs.open("Elib.txt", ios::in);
    ......
    ......
    // load the words in the "Elib.txt" file into the vector
    while(true)
    {
    char wrds[20] ;
    Efs>>wrds;
    if(!strcmp(wrds, "\0")) break;
    Elibdat.push_back(wrds);
    }
    ......
    ......
    // compare a text string with all the strings stored in the vector
    int vecindex=0;
    while(vecindex != Elibdat.size())
    {
    if(!strcmp(wrdsrc, Elibdat.at(vecindex))) //?
    {
    found=true;
    break;
    ......
    }
    vecindex++;
    ......
    ......
    }
    The program stores text strings found in "Elib.txt" file, in a vector.
    Elib.txt file looks something like this:

    The
    Truth
    Is
    Out
    There
    ....
    .....
    ......


    The problem is that, at the if-statement where I've put an question mark, Elibdat.at(vecindex) does not return anything. And the VC++s' variables window indicates that there is nothing inside the vector, after the storing. Does anyone know why is that, please?
    Thanks.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    vector<char*> Elibdat;
    
    while(true)
    {
        char wrds[20] ;
        Efs>>wrds;
        if(!strcmp(wrds, "\0")) break;
        Elibdat.push_back(wrds);
    }
    The only address you are ever pushing onto the vector is the address of the wrds array, the same exact address over and over and over. And that address is only even valid within the braces above. Once you break out of the loop, the memory location for that address can be used by something else.

    You need to either dynamically allocate some new memory every time through the loop and copy the string in wrds into that memory and then push that pointer onto the vector, or better yet, use a vector<string> container and read in and push back string objects from the file and onto the vector.

    [edit]
    Here's how I would do it with strings:
    Code:
    #include <string>
    #include <algorithm>
    #include <iterator>
    
    using namespace std;
    
    ...
    
    vector<string> Elibdat;
    
    // Copy all strings from Efs file into Elibdat vector, this replaces your whole while loop
    copy(istream_iterator<string>(Efs),istream_iterator<string>(),back_inserter(Elibdat));
    [/edit]
    Last edited by hk_mp5kpdw; 03-01-2005 at 07:13 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector Addition Using OOP?
    By everydaybh in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2009, 05:09 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  4. Vector problem
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 11-06-2007, 12:41 PM
  5. vector problem with erase()
    By JukeBoxHero in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2006, 07:02 PM