fscanf issue

This is a discussion on fscanf issue within the C Programming forums, part of the General Programming Boards category; 1) Code: // read salesman name field from text file fscanf(salesmanPtr,"%s\t\t",buffer); // write salesman name field to text file fprintf(tempSalesmanPtr,"%s",buffer); ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    fscanf issue

    1)
    Code:
    // read salesman name field from text file
    fscanf(salesmanPtr,"%s\t\t",buffer);
    // write salesman name field to text file
    fprintf(tempSalesmanPtr,"%s",buffer);
    2)
    Code:
    // skip salesman name field from text file
    fscanf(salesmanPtr,"%*s\t\t",buffer);
    // write salesman name field to text file
    fprintf(tempSalesmanPtr,"Name");
    which one consumes more processing power and/or memory?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > which one consumes more processing power and/or memory?
    1. It depends on your compiler, OS and library implementation.
    2. The difference is almost certainly tiny compared to everything else going on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Version one takes about 0.0000001 second on my machine, and version two takes about 0.0000001, give or take.

    To answer the question meaningfully, you should have a test case with a large amount of data that is measurable. For example, make a "salesman" file that is at least 100MB in size, and then you can more easily measure any difference. Use a program like Windows Task Manager or top(1) to measure the memory usage.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    The two code snippets do two different things--one reads a string into a buffer, and the other reads a string but discards it. Perhaps you should be concerned with using code that does what you need it to do, instead of using code that possibly runs faster but doesn't do what you need it to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf issue!
    By JM1082 in forum C Programming
    Replies: 2
    Last Post: 05-17-2011, 07:38 AM
  2. Fscanf segmentation fault issue (I think)
    By zouru in forum C Programming
    Replies: 5
    Last Post: 04-03-2011, 03:00 PM
  3. fscanf() issue...
    By smoking81 in forum C Programming
    Replies: 12
    Last Post: 09-08-2008, 06:56 AM
  4. fscanf Issue
    By Maser in forum C Programming
    Replies: 3
    Last Post: 04-23-2007, 07:42 PM
  5. FSCANF format string issue
    By INFERNO2K in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2005, 05:52 PM

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