c++中无法从stringstream中获取整数

Cannot get integer from stringstream in C++

本文关键字:获取 整数 stringstream c++      更新时间:2023-10-16

我只是搜索流(内存流或文件流),然后我发现fstream和stringstream

但是,我试过,但它总是给我错误的输出当得到整数,我使用二进制文件来测试这个(数据不是字符串,数据是一堆字节),这是我的代码:

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
void getinfo(char* buffer, int length)
{
    stringstream stream(ios::in | ios::out | ios::binary);
    // write data to stream
    stream.write(buffer, length);
    // reset position
    stream.seekg(0);
    // get 4 bytes (value is OMC with null-byte at the end)
    char* format = new char[4];
    stream >> format;
    // get 4 byte (value is depending of file version)
    int ojmver = 0; 
    stream.read(reinterpret_cast<char*>(&ojmver), sizeof ojmver);
    // get 2 byte (encryption signature 1 that used for this file)
    short encsign1 = 0; 
    stream.read(reinterpret_cast<char*>(&encsign1), sizeof encsign1);
    // get 2 byte (encryption signature 2 that used for this file)
    short encsign2 = 0;
    stream.read(reinterpret_cast<char*>(&encsign2), sizeof encsign2);
    // get 4 byte (samples count)
    int samplecount = 0; 
    stream.read(reinterpret_cast<char*>(&samplecount), sizeof samplecount);
    // show info
    cout << "Formatttt: " << format;
    cout << "nOJM Verttt: " << ojmver;
    cout << "nEnc Sign1tt: " << encsign1;
    cout << "nEnc Sign1tt: " << encsign2;
    cout << "nSample Counttt: " << samplecount;
    cout << "nn";
}
int main()
{
    fstream fs = fstream("D:\o2ma100.ojm", ios::in | ios::out | ios::binary);
    fs.seekg(0, fs.end);
    int length = fs.tellg();
    fs.seekg(0, fs.beg);
    char* buffer = new char[length];
    fs.read(buffer, length);
    if (fs)
        cout << "Length: " << length << "nSuccess to load datan";
    else
        cout << "Failed to load datan";
    fs.seekg(0, fs.beg);
    // get 4 bytes (value is OMC with null-byte at the end)
    char* format = new char[4];
    fs.read(format, 4);
    // get 4 byte (value is depending of file version)
    int ojmver = 0; 
    fs.read(reinterpret_cast<char*>(&ojmver), sizeof ojmver);
    // get 2 byte (encryption signature 1 that used for this file)
    short encsign1 = 0; 
    fs.read(reinterpret_cast<char*>(&encsign1), sizeof encsign1);
    // get 2 byte (encryption signature 2 that used for this file)
    short encsign2 = 0;
    fs.read(reinterpret_cast<char*>(&encsign2), sizeof encsign2);
    // get 4 byte (samples count)
    int samplecount = 0; 
    fs.read(reinterpret_cast<char*>(&samplecount), sizeof samplecount);
    cout << "nRead Data using fs:n";
    cout << "Formatttt: " << format;
    cout << "nOJM Verttt: " << ojmver;
    cout << "nEnc Sign1tt: " << encsign1;
    cout << "nEnc Sign1tt: " << encsign2;
    cout << "nSample Counttt: " << samplecount;
    fs.close();
    cout << "nnRead Data Using stringstream:n";
    getinfo(buffer, length);
    system("PAUSE");
    return 0;
}

结果如下:

Length: 4231047 // -> this correct :)
Success to load data // -> this correct :)
Read Data using fs:
Format                  : M30    // -> this correct :)
OJM Ver                 : 196608 // -> this correct :)
Enc Sign1               : 16     // -> this correct :)
Enc Sign1               : 0      // -> this correct :)
Sample Count            : 300    // -> perfect :D
Read Data Using stringstream:
Format                  : M30         // -> this correct :)
OJM Ver                 : 50331648    // -> this wrong :(
Enc Sign1               : 4096        // -> this wrong :(
Enc Sign1               : 0           // -> this wrong :(
Sample Count            : 76800       // -> this wrong :(
Press any key to continue . . .

我正在用c#创建类似的应用程序。.NET,我已经测试了文件,它给了我正确的输出(使用。NET框架2.0 c#中的FileStream和MemoryStream),这里的输出:

Length: 4231047 // -> Length is correct
Success to load data
Read Data using FileStream:
Format                  : M30     // -> this correct :)
OJM Ver                 : 196608  // -> this correct :)
Enc Sign1               : 16      // -> this correct :)
Enc Sign1               : 0       // -> this correct :)
Sample Count            : 300     // -> perfect :D
Read Data Using MemoryStream:
Format                  : M30     // -> this correct :)
OJM Ver                 : 196608  // -> this correct :)
Enc Sign1               : 16      // -> this correct :)
Enc Sign1               : 0       // -> this correct :)
Sample Count            : 300     // -> perfect :D

我需要处理内存和文件的流,所以我需要如何正确地读取数据与流…

有人能帮我吗?我是c++的新手谢谢你:D

编辑

我更改了代码,感谢john,它适用于fstream,但它仍然不适用于stringstream

您正在使用错误的I/O类型。当你说你应该使用二进制I/O时,你正在做文本I/O。

。不是

int ojmver = 0; 
stream >> ojmver;

,

int ojmver = 0; 
stream.read(reinterpret_cast<char*>(&ojmver), sizeof ojmver);

当然,由于大小问题(您确定int是四个字节吗?)和尾序问题,这可能仍然不正确。