简体   繁体   English

用atof C ++转换char数组中的float

[英]convert float in char array with atof C++

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. 这最初是用C语言编写的,我开始怀疑这是否反映在我的教授为我们提供的制作二进制文件的代码中。 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';
}
}

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! 我使用了空格,这解决了我的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM