Thread: newline

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    13

    newline

    Simple question. why isn't "\n" not working.

    Code:
    #include<stdio.h>
    
    main()
    
    {
    
    int x,max;
    
    printf("Number to count up to:");
    scanf("%d", &max);
    
    FILE *fp;
    fp=fopen("c:\\test.txt", "a+");
    
    
    
    for (x=0; x<max; x++)
    
    {fprintf(fp, "%d\n",x);}
    
    fclose(fp);
    
    
    
    
    }
    I open the txt file and it's all on one line.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    So if the input 3 and output is 012??
    Can post output? What text editor are you using to see output file?
    Last edited by Bayint Naung; 03-19-2011 at 03:11 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well since you're opening the file with "a+" (why not "w"), the output file just keeps growing.
    man page fopen section 3

    So the stuff you see at the start of the file is never changing data from old program runs.
    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
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    I just wrote the program as a test to see what was going on.

    yes it does output "0123" if the input max value is 4.

    i'm looking at making newlines between the digits so it will output as

    0
    1
    2
    3

    instead of

    0123

    but i don't know how.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Which compiler are you using?

    For your OS (I'm guessing DOS/Windows with the c:\\), the \n on output should be transformed to \r\n.

    But the symptom you describe is what you see if you only output a \n, and try to view the file with windows standard notepad.exe

    wordpad on the other hand can correctly interpret just \n, if that is all there is in the text file.

    Try attaching your test.txt file to your post if you're still stuck.
    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.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    13
    Thanks Salem. wordpad does recognize the \n.

    I'm using cygwin installed on windows 7. gcc compiler.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yeah, that would do it.
    cygwin thinks it is a lot more Unix-like than the DOS heritage platform it runs on.

    So it will create files by default that end only in \n.

    If you're using notepad to edit code, then I would suggest trying notepad++
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newline and null terminator
    By dunsta in forum C Programming
    Replies: 2
    Last Post: 04-22-2010, 07:13 AM
  2. Searching for newline on the end of line text
    By LevelOne in forum C Programming
    Replies: 7
    Last Post: 03-03-2010, 04:36 AM
  3. how to remove newline from a string
    By rupurt in forum C Programming
    Replies: 8
    Last Post: 10-01-2005, 06:01 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. fgets and a bothersome newline char
    By ivandn in forum Linux Programming
    Replies: 1
    Last Post: 11-14-2001, 01:41 PM