Thread: newline problem

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    43

    newline problem

    I have a program that is supposed to add a '\n' to move down to the next line. However when I open the file that this is output to I see a square box where the new line should be. Why is this? And is there an alternative char I can use instead of \n for a new line?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    51
    For people to be able to help you, you will need to post the actual code that you are having problems with.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    It probably means you used feof() in your control loop, and now you have (char)EOF stored in your file.

    Read the FAQ on why feof() is bad in loops.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Windows is very picky (stupid) about newlines. Try placing \r\n instead of just \n.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    A '\n' in C is really translated behind the scenes as mentioned above into a \r\n <control return line feed> on Windows. Your problem is the same that I can get a lot looking at textfiles made on a *nix system, where only \n's are used and all the newlines are little <unknown char> boxes. If you are only doing regular fopen with a "w" then the \n is translated behind the scenes, but with binary file I/O ("wb") then you will need to actually write the \r\n, along with things like WriteFile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem storing a newline character
    By mdekom12 in forum C Programming
    Replies: 8
    Last Post: 04-25-2010, 10:51 PM
  2. Newline problem
    By thinice16 in forum C Programming
    Replies: 6
    Last Post: 08-26-2008, 02:38 PM
  3. Cin newline space problem... or something
    By Baqualish in forum C++ Programming
    Replies: 10
    Last Post: 10-17-2007, 03:49 AM
  4. Help with newline problem
    By nafix in forum C Programming
    Replies: 2
    Last Post: 10-06-2007, 02:52 AM