Thread: Compiler not taking insertion operator with ofstream.

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    106

    Compiler not taking insertion operator with ofstream.

    Code:
    void save_file(string filename, vector<string>to_save, int truncate)
    {
      if(truncate == 0)
      {
        ofstream save_file(filename.c_str(),ios::trunc);
      }
      else
      {
        ofstream save_file(filename.c_str(),ios::app);
      }
      
      string write;
      
      for(int x=0; x<to_save.size(); x++)
      {
        write = to_save[x];
        save_file << write << "\n";
      }
      save_file.close();
      return;
    }
    I'm assuming I screwed up the syntax somewhere, although it looks alright to me. I tried using .c_str() on write, and also putting it in "'s (so that, like, it'd literally write the word "write" to the file. I'm not sure what THIS was supposed to solve XD), but neither changed the errors.

    44 generic_functions.cpp no match for 'operator<<' in 'save_file << write'

    Also getting
    46 generic_functions.cpp request for member `close' in `save_file', which is of non-class type `void ()(std::string, std::vector<std::string, std::allocator<std::string> >, int)'

    Do I need to pass some additional parameters to the ofstream (or fewer?)

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    the stream save_file is not in scope. You have to do it this way.
    Code:
    void save_file(string filename, vector<string>to_save, int truncate) {
        ofstream save_file;
        if(truncate == 0) {
           save_file.open(filename.c_str(),ios::trunc);
        }
        else   {
           save_file.open(filename.c_str(),ios::app);
        }
       
       string write;
      
       for(int x=0; x<to_save.size(); x++)  {
          write = to_save[x];
          save_file << write << "\n";
       }
        // save_file.close(); // not necessary the destructor will close the stream
        // return; // not necessary 
    }
    Kurt

    EDIT: It would be more efficient if you would pass the vector to_save by reference.
    Like this
    Code:
    void save_file(string filename, vector<string> & to_save, int truncate)
    Last edited by ZuK; 01-22-2006 at 04:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Though implementation problem
    By Elysia in forum C++ Programming
    Replies: 296
    Last Post: 05-31-2008, 01:02 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 10:06 PM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM