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 ?
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.
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:
Error checking and all the rest omitted for clarity!
fgets(buf, sizeof(buf), stdin);
fprintf ("%-100s", buf);
thanks Hammer, I'll give it a go :)