Thread: newline with fprintf()

  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
    Location
    Inside my computer
    Posts
    24,654
    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 01: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
    28,413
    Change "wb" to "w" since you do not intend binary mode.
    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

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