Like Tree1Likes
  • 1 Post By Salem

newline

This is a discussion on newline within the C Programming forums, part of the General Programming Boards category; 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); ...

  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 04: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
    32,848
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    32,848
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    32,848
    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++
    Bayint Naung likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 08: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, 07: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

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