newline with fprintf()

This is a discussion on newline with fprintf() within the C Programming forums, part of the General Programming Boards category; I'm using fprintf to print strings to a file, but I can't get it to make a new line. Here's ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    2

    newline with fprintf()

    I'm using fprintf to print strings to a file, but I can't get it to make a new line.

    Here's my code:
    Code:
    char buffer[80];
    FILE *f;
    f = fopen ("tester.txt", "wb");
    fprintf(f, "HELLO\nWORLD\nIt's Good\nTo See\nYou");
    fclose (f);
    The output I'm hoping for is:
    HELLO
    WORLD
    It's Good
    To See
    You
    But instead I'm getting:
    HELLOWORLDIt's GoodTo SeeYou
    thanks for your help

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,887
    Try changing
    f = fopen ("tester.txt", "wb");
    to
    f = fopen ("tester.txt", "w");
    and see if it changes anything.
    Last edited by Elysia; 09-28-2010 at 02:59 PM. Reason: Clarity
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,101
    Change "wb" to "w" since you do not intend binary mode.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    2
    haha, thanks that sorted it. I can't remember why I used the 'b' in the first place...

    cheers
    Last edited by carderne; 09-28-2010 at 04:09 PM.

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. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  3. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM
  4. fprintf to stderr crash programs
    By jlai in forum Windows Programming
    Replies: 2
    Last Post: 04-12-2005, 09:51 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM

Tags for this Thread


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