Thread: writing a newline to a file doesn't actually output a new line

  1. #1
    Banned
    Join Date
    Jan 2003
    Posts
    1,708

    writing a newline to a file doesn't actually output a new line

    for some reason no matter when I put endl or \n a new line is not created in the text file. Instead it just puts a little box where the new line character is, but it doesn't actually go to a newline in the text file. This is in notepad

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Does this work for you? It should be this simple...

    Code:
    #include <fstream>
    using namespace std;
    
    int main( void )
    {
      ofstream fout( "MyFile.txt" );
    
      fout << "Hello World!! \n";
      fout << "This is line #2" << endl;
      fout << "This is line #3";
    
      fout.close();
    
      return 0;
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Yeah that works. I'm doing it differently because you cannot call ios::read and store it an unsigned char * array, but at least I know why its doing it

    Code:
    #include <fstream.h>
    
    
    int main( void )
    {
    	ofstream fout( "MyFile.txt", ios::out | ios::binary );
    
      fout << "Hello World!! \n";
      fout << "This is line #2" << endl;
      fout << "This is line #3";
    
      fout.close();
    
      return 0;
    }

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You open the file as binary, not text. That's your problem!
    (newlines are for textfiles only)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>You open the file as binary, not text.

    It might work on some OSes, but on Windows a newline is actually two characters in a row, carriage return and line feed. So if you use '\n' in a binary file, it will most likely just output the carriage return.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>So if you use '\n' in a binary file, it will most likely just output the carriage return
    \n is a newline, \r is for carriage return. To do what the OP wants, use \r\n instead of just \n, but of course this isn't portable.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM