Thread: fprintf and printf ?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    30

    fprintf and printf ?

    what's the difference between fprintf & printf ?
    if we read directly from a file, can we use printf to write the content?

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    fprintf lets you select which file stream to print to. When you use printf, stdout is automatically used as the file stream.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by vsovereign View Post
    what's the difference between fprintf & printf ?
    printf is fprintf(stdout,

    Quote Originally Posted by vsovereign View Post
    if we read directly from a file, can we use printf to write the content?
    how the first half of the sentence is connected to the second half?

    Where you reading info from and what functions you use to output results seems to me unrelated at all
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    30
    sorry, I was confused...
    is it correct to say that fprintf is when you want to write something to an external file?

  5. #5
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    printf is the same as fprintf(stdout, char *format, ...);

    So printf puts the format string into the screen (STDOUT).

    And fprintf gives you an option to output what you want into an file; fprintf(FILE *f, char *format, ...);

    By the way, you can write fprintf(stdout, char *format, ...); because STDOUT (Screen) is treated as a file.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    Smile

    printf() function sends output to stdout and fprintf() is used to specify the output stream.
    fprintf() we can use this to print into a file.

    Code:
    printf("Hai friends\n"); //This will print output in stdout.
    
    
    FILE *f=fopen("test.txt","w");
    fprintf(f,"%s","HELLO");    // This will print the output in test.txt.
    Last edited by rekha_sri; 02-28-2010 at 10:52 PM.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    printf and fprintf are same , but the difference is printf has a default stream STDOUT , that means
    it can only print it in the monitor.
    but fprintf can have any user defined stream , so that only we prefer fprintf to write a file.

    For ex :
    if you want to print the content in the STDERR , you cannot in printf , but we can do that using fprintf , since
    it allows us to choose the stream. like that we can choose normal files too.

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    30
    thank you for the help everyone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  5. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM