c++中无法从stringstream中获取整数
Cannot get integer from stringstream in C++
我只是搜索流(内存流或文件流),然后我发现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是四个字节吗?)和尾序问题,这可能仍然不正确。
相关文章:
- 快速扫描标准以获取整数不起作用
- 只要用户输入,如何从用户那里获取整数?
- 如何从另一个类动态获取整数?
- 控制台在获取整数后暂停
- 在某些范围更新后获取整数数组的最终状态的有效算法是什么?
- 如何从从 MPU 陀螺仪发送的字节数组中获取整数
- 如何在C 中获取整数数组的元素
- 如何打破循环以在C 中获取整数数组
- 火炬C++:使用 *.data() 获取整数张量的值<int>
- 从.txt文件中获取整数
- 从C 中的系统函数中获取整数(大于255)
- 从集合中随机给定整数 0、1 和 2 时获取整数 0、1 和 2 的快速方法
- 使用boost::random库获取整数随机值而不是实数
- 从文件 C++ 获取整数
- C++从迭代器中获取整数形式的索引值
- 如何使用zmq/zeromq从传递给python的字符串表示(字节数组)中获取整数/浮点数
- 从xml中获取整数和浮点值
- 获取整数bitset中前导1位置的最快方法
- c++中无法从stringstream中获取整数
- 如何在visual studio 2013中使用c++从文本文件中获取整数