Thread: ofstream

  1. #46
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ehem, that makes sense, thanks CornedBee!

  2. #47
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    If i use EOF as the delimiting character for istream::getline , will it read the whole file at once?

    getline(TheFile,str,EOF)

    If yes , will it still swallow the '\n' character?
    Or anyway is it possible to do what i want with istream::getline (namely to detect the end of line in some way )
    or shall i look for another method?
    Cheers

  3. #48
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    EOF isn't necessarily a valid char value. You can't pass it to getline().
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #49
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ok i understand.
    So maybe i have to go about it something like that:
    Code:
    while (!eof())
     {getline(TheFile,str,)}

  5. #50
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's simply a syntax error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #51
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or read() with a fixed buffer. It's considerably more efficient than scanning the stuff you read for newlines.

    Another thing: see the FAQ for eof(). Never loop until eof() is true. If there's an I/O error, it won't ever be.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #52
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thanks Cornedbee, you gave me the final solution!

    I let alone getline() and i rewrite the whole program with read()

    Code:
    int main () {
      int length;
      char * str;
      
      cout << "Enter file name : ";
      string   FileName;
      cin >>   FileName;
      ifstream TheFile(FileName.c_str());
      ofstream TheCopy;
      TheCopy.open ("Your_Uppercased_Copy.srt",ios::app);
      
      // get length of file:
      TheFile.seekg (0, ios::end);
      length = TheFile.tellg();
      TheFile.seekg (0, ios::beg);
    
      // allocate memory:
      str = new char [length];
    
      // read data as a block:
      TheFile.read (str,length);
      TheFile.close();
      str[0] = static_cast<char>( toupper(str[0]));
       for(int i = 0; str[i]; i++)
              if (str[i] == '.'|| str[i] == '!'|| str[i] == '?')
                  str[i+1] = static_cast<char>( toupper(str[i+1]));
       for(int j = 0; str[j]; j++)
              if (str[j] == '.'|| str[j] == '!'|| str[j] == '?')
              if (str[j+1] == '\n') 
                  str[j+2] = static_cast<char>( toupper(str[j+2]));
       for(int k = 0; str[k]; k++)
              if (str[k] == '.'|| str[k] == '!'|| str[k] == '?')
              if (str[k+1] == ' ') 
                  str[k+2] = static_cast<char>( toupper(str[k+2]));
              
                  TheCopy.write (str,length);
      TheCopy.close();
      delete[] str;
      return 0;
    }

  8. #53
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You shouldn't use seekg() and tellg() with non-binary streams. You could get corrupted data on Windows.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #54
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ok thanks, but if i add "ios::binary" it doesnt work.

    ifstream TheFile(FileName.c_str(), ios::binary)

  10. #55
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I know. The two simply don't mix. There's no fast way to determine how many characters there are in a file. Best thing seekg and tellg can do for you is give you a guideline for allocating, but you still need to be prepared for 1) getting less than that and b) getting more than that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ofstream and FILE behaviour
    By MrLucky in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2007, 05:45 PM
  2. Ofstream... broke as a result of ifstream fix
    By tms43 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2006, 11:40 AM
  3. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 01:10 PM
  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. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 08:34 AM