Thread: sprintf + fprintf

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    114

    sprintf + fprintf

    Hi, I have a question that may be dealing with efficiency in programming:

    What is the conceptual difference between writing to files in the two steps:

    Code:
    fp = fopen(full_filename, "w+");
    
    char str[128];
    sprintf(str, "%d ", Variable);
    fprintf(fp, str);
    and using directly fprintf in the following way:

    Code:
    fprintf(fp ," %d", Variable);
    Is the first better for memory saving in some way? I usually use the second option, but starting wondering when I saw other people using option 1.

    Thank you in advance
    CFD

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    first could have some performance gain in case when you build a buffer with several sprintfs and then fwrite it to the disk with one call...

    calling for each sprintf - one fprintf - I do not see any benefits in it
    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

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Thanks for the quick reply

    That helped
    regards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  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. sprintf and fprintf segmentation error
    By kona1 in forum C Programming
    Replies: 5
    Last Post: 06-21-2005, 10:55 AM
  5. Multithreading problem
    By Bacardi34 in forum C Programming
    Replies: 5
    Last Post: 09-02-2004, 02:26 PM

Tags for this Thread