Thread: File I/O Reading from file

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    22

    File I/O Reading from file

    Hi, what am I doing wrong I'm reading from a file with words and trying to get the output properly. I know it's reading, and counting the amount of words in it. But when I try to output I get a bunch of gibberish. Any help would be appreciated.

    In main I call the load function as follows:

    Code:
    Myobject newobject;
    string filename;cout << "enter filename ";
    cin >> filename; newobject.loadthisfile(filename);
    
    
    Then the function is as follows:

    Code:
    SIZE=4
    
    
    void Myobject::loadthisfile(const string & filename)
    {
        ifstream input(filename.c_str());
        string newword;
        if (input.fail())
        {
        cout << "couldn't not open file " << filename << endl;
        exit(1);
        }
        
        while (getline(input,newword))              // read one line
        {
        newword = newword.substr(0,SIZE);     // 
        if (isalpha(newword[0]))                //
        {
    
    
           if (wordscount >= wordlist.size())  // double  if needed
                {
                    wordlist.resize(wordlist.size() * 2);
                }
                wordlist[wordscount] = new Myobject(newword);
                cout<<wordlist[wordscount]<<endl;                 //// produces gibberish ERROR here
                wordscount++;
                cout<<wordscount<<endl;                ///echoes proper number of words from textfile
            
        }
        }
    }

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    What is 'wordlist' ? It seems you're storing 'MyObject' instances into 'wordlist' and then trying to output that ?

    Have you overloaded the output operators for class 'MyObject' at least ?

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    22
    wordslist is of type vector<Myobject*>mywordslist

  4. #4
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by ubmattpangolin View Post
    wordslist is of type vector<Myobject*>mywordslist
    In that case you might want to overload the output operators for your 'MyObject' class.

    Another easier way would be instead of having a vector of 'MyObject', assuming you only want to store the strings, why not just have a vector of strings ?

    Code:
    std :: vector<std :: string> mywordlist;
    Then instead of creating new 'Myobject' instances based on the current string - just push the string into the vector

    Code:
    mywordlist.push_back(newword);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-27-2013, 12:24 PM
  2. Replies: 0
    Last Post: 03-21-2013, 03:31 PM
  3. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  4. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  5. Replies: 13
    Last Post: 05-31-2009, 11:30 AM