ofstream

This is a discussion on ofstream within the C++ Programming forums, part of the General Programming Boards category; Ehem, that makes sense, thanks CornedBee!...

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

  2. #47
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747
    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,892
    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
    Location
    France
    Posts
    747
    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
    20,981
    That's simply a syntax error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,892
    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
    Location
    France
    Posts
    747
    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,892
    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
    Location
    France
    Posts
    747
    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,892
    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

Page 4 of 4 FirstFirst 1234
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, 10:40 AM
  3. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 12:10 PM
  4. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 09:06 PM
  5. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 07:34 AM

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