0

I've been looking for similar problems but am still unable to find a solution to my problem.

I am taking a text file and converting it to a binary file to be used in another program. The other program compiles and outputs everything just fine but the problem seems to lay in my file conversion.

void makefile( ifstream & fi, ofstream & fo ){
    struct{
        int acct;
        char name[25];
        float dolr;
    } x;

    int i;
    char line[81], *p, *q;

    while ( fi.getline( line, 81 ) ) {
        x.acct = atoi( line );
        p = line;
       while ( *p++ != ' ' );
        for ( q = x.name, i = 24 ; i-- ; ){
            *q++ = *p++;                    // *q = '\0';
        }
        while ( *--q == ' ' );              // find end of string

        *++q = '\0';                        // mark end of name string
        x.dolr = atof(p);
        fo.write( (char*)&x, sizeof(x) );
    }
}

The input text file looks like this...

000027183 Hun, Attila The       1234.56
000012345 Dooright, Dudley      211.22
000031416 Whiplash, Snidely     1200.00
000014142 Jekyll, Doctor        1500.00
000031623 Hyde, Mister          1500.00
000010203 Bear, Smokey The      2000.00
000020103 Dumpty, Humpty        3000.00
000030102 Woman, Wonder         3824.36
000030201 Hulnk, Incredible     9646.75
000022345 Snowman, Abominable   2496.24

When I run the program and I check the binary (using a separate program) I can see the converted file but it looks like this:

 27183  Hun, Attila The         1234.56         0.00
 12345  Dooright, Dudley        211.22         0.00
 31416  Whiplash, Snidely       1200.0        0.00
 14142  Jekyll, Doctor          1500.00          0.00
 31623  Hyde, Mister                1500.00           0.00
 10203  Bear, Smokey The        2000.00        0.00
 20103  Dumpty, Humpty          3000.00          0.00
 30102  Woman, Wonder               3824.36          0.00
 30201  Hulnk, Incredible       9646.7        5.00
 22345  Snowman, Abominable     2496        0.24

This was originally written in C and I'm beginning to wonder if that reflects in the code that my professor has given us to make the binary file. Any suggestions?

Update: This is the the function in the other program that reads the file to "check it"

void
chkfile( ifstream & f ){

struct {
    int acct;
    char name[25];
    float dolr;
} x;


while ( f.read( (char *)&x, sizeof(x) ) ){
    cout << setfill('0') << setw(9) << x.acct << "  ";
    cout.setf( ios::left, ios::adjustfield );
    cout << setfill(' ') << setw(26) << x.name;
    cout.setf( ios::right, ios::adjustfield );
    cout.setf( ios::fixed, ios::floatfield );
    cout.setf( ios::showpoint );
    cout.precision(2);
    cout << setw(10) << x.dolr << '\n';
}
}
Yuchen
  • 30,852
  • 26
  • 164
  • 234
Mary Martinez
  • 314
  • 5
  • 12
  • in order to debug your program you should add debug output after you write the binary file ("this and thas was written to the binary file"), so you can be sure that you didn't do anything wrong while reading. finally, you should look into the code of the program that does read from the binary file and provides unexpected output. there's nothing about a fourth field in your original code so I can't suggest anything. – Pavel May 24 '14 at 21:09
  • 1
    I would say there's a problem in the "separate program", because while maybe not the best the code reading the text and outputting the "binary" structure seems fine. – Some programmer dude May 24 '14 at 21:12
  • Thanks Pavel, I did try that and this was the results of outputting just before the call to atom. mmac:cs3240 mlmartinez85$ ./ritefiles masfilt masfil ** "masfil" exists ** overwrite? y this is p: this is x.dolr: 0 this is p: this is x.dolr: 0 this is p: 0 this is x.dolr: 0 this is p: 0 this is x.dolr: 0 this is p: 0 this is x.dolr: 0 this is p: this is x.dolr: 0 this is p: this is x.dolr: 0 this is p: this is x.dolr: 0 this is p: 5 this is x.dolr: 5 this is p: . this is x.dolr: 0.24 – Mary Martinez May 24 '14 at 21:13
  • Please post how you opened the input and output files. A "binary" file must be opened with the `ios:binary` flag, but you didn't show this. – PaulMcKenzie May 24 '14 at 21:14
  • I am using the Terminal, I am on Mac OSX 10.9.2 and typing g++ ritefiles.cpp -o ritefiles then calling the program ./ritefiles masfilt masfil. Where "masfilt" is the name of my text file and "masfil" is the name I will give to the binary output file. – Mary Martinez May 24 '14 at 21:18
  • Print separators (not space but something obvious) around the `x.name` field in your verification program. I'm pretty sure you will find a clue there. – molbdnilo May 24 '14 at 21:26
  • Of course, you can also output the `name` field in your conversion program. – molbdnilo May 24 '14 at 21:44
  • Thanks @molbdnilo I tried that with an exclamation mark and this is my result `000027183 Hun, Attila The 1234,56!!!!!!!!!0.00 000012345 Dooright, Dudley 211,22!!!!!!!!!0.00 000031416 Whiplash, Snidely 1200,0!!!!!!!!0.00 000014142 Jekyll, Doctor 1500,00!!!!!!!!!!0.00 000031623 Hyde, Mister 1500,00!!!!!!!!!!!0.00 000010203 Bear, Smokey The 2000,00!!!!!!!!0.00 000020103 Dumpty, Humpty 3000,00!!!!!!!!!!0.00 000030102 Woman, Wonder 3824,36!!!!!!!!!!0.00 000030201 Hulnk, Incredible 9646,7!!!!!!!!5.00 000022345 Snowman, Abominable 2496!!!!!!!!0.00` itt seems to me like it's parsing wrong – Mary Martinez May 24 '14 at 23:12
  • Like it's taking the amount as part of the name and the float precision is off. Any tips on how to correct that? – Mary Martinez May 24 '14 at 23:13

1 Answers1

0

UPDATE: Turns out the problem was not in the code but in the text file used to create the binary file. I was using tabs to space between names and amounts so the parsing was off when converting to binary. I used blank spaces and that solved my problem!

Mary Martinez
  • 314
  • 5
  • 12