Thread: File I/O Manipulation

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    5

    Question File I/O Manipulation


    Hi,

    I am writing data to *.dat file in columns. How can I manipulate the output so every number in the first column is 6 characters wide and the second column is 5 characters wide? In C++, I used setw(6) to set the width of each output. How can I do this in C?

    This didn't work.
    Code:
    fprintf(outfile, "%03.2lf %2.2lf", data1[i], data2[i]);


    I try to use strncpy to acquire the second column's data. Since the first column has different widths, based on the number, I index at the wrong position.

    Basically I need to read ascii data into the program.

    Thanks!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Saying something "didn't work" really doesn't tell us what about it you didn't like other than that it obviously didn't fit whatever you're trying to achieve.

    What does this do for you?

    Code:
    fprintf(outfile, "%06.2lf %05.2lf", data1[i], data2[i]);

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Change the %3 to a %6, and the %2 to %5, that's your width of field settings. I have never seen an "lf" data designation for printf. Try just "f'.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    5

    width of 8 for double?

    This seems to work when I output double's
    Code:
    	fprintf(OutTable, "%8.3lf%8.2lf\n",ChtTimes[k],ChtValues[k]);
    and this does not
    Code:
    	fprintf(OutTable, "%7.3lf%8.2lf\n",ChtTimes[k],ChtValues[k]);
    Does the width need to be 8 for outputing a double?

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    My guess is that you're not taking the decimal point into account.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And what does it mean? "not work" is not good enough explanation of the problem...
    It cannot be compiled?
    It crashes during run?
    It outputs the values in a way a don't expect?
    post expected output and the actual output for the first and the second sample of the code...
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary File Manipulation Speeds
    By Shonumi in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2009, 02:07 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM