sprintf + fprintf

This is a discussion on sprintf + fprintf within the C Programming forums, part of the General Programming Boards category; Hi, I have a question that may be dealing with efficiency in programming: What is the conceptual difference between writing ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 04:42 PM
  2. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 03:19 PM
  3. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 09: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


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