0

同様の問題を探していますが、まだ問題の解決策を見つけることができません。

テキスト ファイルを取得して、別のプログラムで使用するバイナリ ファイルに変換しています。他のプログラムはすべて問題なくコンパイルして出力しますが、ファイル変換に問題があるようです。

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) );
    }
}

入力テキストファイルは次のようになります...

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

プログラムを実行して (別のプログラムを使用して) バイナリをチェックすると、変換されたファイルが表示されますが、次のようになります。

 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

これはもともと C で書かれたもので、私の教授がバイナリ ファイルを作成するために私たちに与えてくれたコードにそれが反映されているかどうか疑問に思い始めています。助言がありますか?

更新:これは、ファイルを読み取って「チェック」する他のプログラムの機能です

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';
}
}
4

1 に答える 1