Thread: notepad doesn't recognize new line character

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    7

    notepad doesn't recognize new line character

    Hi,

    I'm not sure this is really a programming issue, but I will give it a try here. I'm outputting data formatted kind of like a table to a file. When opened with notepad, the data is all on one line and where there should be a new line a block character is present (showing that it's an unrecognized character or something). When opened with wordpad, it displays just fine. Would there be something wrong in my code? I'm simply output '\n' when i want a new line.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you open the file as binary or text? If it's binary, then yes, I would expect it to "not work right". Newline in Windows is CR+LF, whilst C itself considers LF as a newline, so in a binary file, newlines stay as LF, but in text-files, it converts to CR+LF.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    Thanks. How would I know how the file is opened? The output file is given as a command line argument and ends with .txt...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Presumably your application uses fopen() to open the file. The second argument to fopen() is a "mode" parameter, usually containing "a", "r" or "w". If it also has "b" in it, then it's a binary file (e.g. fopen("blah.txt", "wb") would open blah.txt as a binary file).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    No, i only have a w in the file, so I don't know what's going on.

    Here is the function that outputs it to the file:
    Code:
    void output_maze(char *maze, int xWidth, int yWidth, FILE *mazeFile)
    {
    	int new_line_counter = 1;  		//-index tracker
    	int full_size = xWidth*yWidth - 1;	//-full size of array	
    	for (int i=full_size; i>=0; i--)
    	{
    		//-if at the end of row, print new line after char
    		if (new_line_counter%xWidth==0) 
    		{
    			fprintf(mazeFile, "%c\n", maze[i]);
    		}
    		else
    		{
    			fprintf(mazeFile, "%c ", maze[i]);
    		}
    		new_line_counter++;		//-increment the counter
    	}
    }

    opening the file:

    Code:
    FILE *mazeFile = fopen(fileName,"w");

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Which compiler are you using - cygwin-gcc ?
    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.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    Yes! cygwin-gcc

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    Try opening the file in textpad. Notepad wont recognize that the file was in linux, but textpad will.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    I'm running cygwin on windows. If the same program was run and compiled on linux, would it work? And yes, it works perfectly in wordpad.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    When you installed cygwin, you had the choice of UNIX or DOS line endings. So even despite it being a windows machine, you end up with files which are just \n terminated.

    A quick-fix would be to use
    unix2dos oldfile newfile
    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.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    7
    This is an assignment, so do you think when i turn it in it won't matter (as in they have it set up correctly)?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is an assignment, so do you think when i turn it in it won't matter (as in they have it set up correctly)?
    If your marker compiles the program for himself or herself, or uses Unix or Max OS X, or uses a line ending aware text editor, it might not matter at all.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Very strange error...
    By Blackroot in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 01:37 AM
  3. Character problem!!
    By cBegginer in forum C Programming
    Replies: 3
    Last Post: 09-02-2005, 11:51 PM
  4. Output text containing '\r' as new line character.
    By anonytmouse in forum C Programming
    Replies: 4
    Last Post: 11-17-2003, 08:47 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM