Simple File Creation Algorithm
I might be going about this the wrong way, I'm fairly new to C. I don't have the code with me to post, but here is what I am trying to accomplish:
Problem: Read in a fixed length file, with fixed length records. Each field in the record is also fixed in length, but each field contains unwanted spaces, possible tabs, etc. I need to read in the records of this file, and produce a separate output file that contains 1 record output for every record input. Each output record needs to contain the same fields that were in the input record. Each field needs to be "massaged" (i.e. unwanted spaces removed, etc.) and field delimeters need to be placed in the output file between each of the fields within the output records.
I tried to do this using C, with what I know as traditional algorithm for reading/writing files.
I read a record into a structure, which was defined containing strings for each field. I was able to process the records as input ok, using fread. Then, I had a similar output structure defined, with output strings defined for each field. I moved the input fields (after messaging them) into the output fields. Then I used fwrite to write the output structure. My output was garbled. Everything looked wrong, and it looked like structures were being overlayed, so I opted to process one byte at a time. I figured that I was not clear on how to use strings inside structures, especially since there seems to be an extra byte at the end of each string for end of string marker, and I do not want that byte in my output file, as I need to use a specially defined field delimeter (my output is going to another software routine that I cannot change).
Anyway, here is what I attempted to do, byte by byte, and it works. However, it is too much IO, and I want to change this to the right way, using one write per record, but need help!
1. Read in a fixed length file of fixed length records (text).
2. For each record I read in I have defined a structure, and am using fread to read them in.
3. While there are records in the input file:
3.a For each field:
3.a.1 Examine and strip off spaces, or otherwise reformat the field
3.a.2 Write the field (one byte at time) to an output file
3.a.3 Write a delimiter at the end of each field
3.b At end of each record, place a newline delimiter, and write that field to the file
4. Close input and output files and return.
I admit this is too cumbersome and does too much IO. I need to change this so that I only build output strings into a structure and then write one structure out for each record.
Perhaps I don't understand the basics of string processing.
Any suggestions? Much appreciated ..... the books were a little confusing to me right now.