Thread: changing file formats

  1. #1
    Registered User
    Join Date
    Aug 2003

    changing file formats


    I wanted to change a variable size record into a fixed length record. I am using fread and fwrite, but have come unstuck. What I am getting is the original record with the structure tagged on the end as spaces, the same number as the record, rather that the individual fields padded.

    I would post the code, but its a bit large.

    int fread and size_t fwrite are both returning 1, which shows no error.

    Is this the right way to go about changing a delimited file into a fixed length ?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Maybe. Maybe not. There are many ways to solve such a problem. If that technique does what it's supposed to do, then it's well and good. But the smaller the code, the better.

    Besides, you're not any code.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    fread() isn't any good for reading variable length records (unless you can work out that length before doing to read).

    I'd suggest using fgets() to read a line, the fprintf() to put it back out to a new file.

    This will fix the width to 100:
    fgets(buf, sizeof(buf), stdin);
    fprintf ("%-100s", buf);
    Error checking and all the rest omitted for clarity!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Aug 2003
    thanks Hammer, I'll give it a go

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Deleting / Changing a record in a text file
    By clearrtc in forum C Programming
    Replies: 9
    Last Post: 08-21-2006, 12:09 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM