java读取4个字节_一次读取4个字节 (Reading in 4 bytes at a time)
发布日期:2021-08-20 05:18:43 浏览次数:61 分类:技术文章

本文共 4972 字,大约阅读时间需要 16 分钟。

最佳答案

英文原文

To read by 4 bytes from ifstream you could overload operator>> as follows (it is actually a partial specialization of the basic_istream class template so istream_iterator could use operator>> from it. Class basic_ifstream is used here to inherit all input file stream functionality from it):

#include

typedef unsigned int uint32_t;

struct uint32_helper_t {};

namespace std {

template

class basic_istream : public basic_ifstream {

public:

explicit basic_istream(const char* filename,

ios_base::openmode mode ) : basic_ifstream( filename, mode ) {}

basic_istream& operator>>(uint32_t& data) {

read(&data, 1);

return *this;

}

};

} // namespace std {}

Then you could use it in the following way:

std::basic_istream my_file( FILENAME, std::ios::in|std::ios::binary );

// read one int at a time

uint32_t value;

my_file >> value;

// read all data in file

std::vector data;

data.assign( std::istream_iterator(my_file),

std::istream_iterator() );

中文翻译

要从ifstream读取4个字节,您可以按如下方式重载operator>>(它实际上是basic_istream的部分特化类模板,所以istream_iterator可以使用operator>>。这里使用Class basic_ifstream来继承它的所有输入文件流功能):

#include< fstream>

typedef unsigned int uint32_t;

struct uint32_helper_t {};

namespace std {

模板< class traits>

class basic_istream< uint32_helper_t,traits> :public basic_ifstream< uint32_t> {

上市:

显式basic_istream< uint32_helper_t,traits>(const char * filename,

ios_base :: openmode mode):basic_ifstream< uint32_t>(filename,mode){}

basic_istream< uint32_helper_t,traits>&运算符>>(uint32_t& data){

读(& data,1);

返回*这个;

}

};

} // namespace std {}

然后您可以通过以下方式使用它:

的std :: basic_istream< uint32_helper_t> my_file(FILENAME,std :: ios :: in | std :: ios :: binary);

//一次读一个int

uint32_t值;

my_file>>值;

//读取文件中的所有数据

的std ::矢量< uint32_t的>数据;

data.assign(std :: istream_iterator< uint32_t,uint32_helper_t>(my_file),

std :: istream_iterator< uint32_t,uint32_helper_t>());

To read by 4 bytes from ifstream you could overload operator>> as follows (it is actually a partial specialization of the basic_istream class template so istream_iterator could use operator>> from it. Class basic_ifstream is used here to inherit all input file stream functionality from it):

#include

typedef unsigned int uint32_t;

struct uint32_helper_t {};

namespace std {

template

class basic_istream : public basic_ifstream {

public:

explicit basic_istream(const char* filename,

ios_base::openmode mode ) : basic_ifstream( filename, mode ) {}

basic_istream& operator>>(uint32_t& data) {

read(&data, 1);

return *this;

}

};

} // namespace std {}

Then you could use it in the following way:

std::basic_istream my_file( FILENAME, std::ios::in|std::ios::binary );

// read one int at a time

uint32_t value;

my_file >> value;

// read all data in file

std::vector data;

data.assign( std::istream_iterator(my_file),

std::istream_iterator() );

要从ifstream读取4个字节,您可以按如下方式重载operator>>(它实际上是basic_istream的部分特化类模板,所以istream_iterator可以使用operator>>。这里使用Class basic_ifstream来继承它的所有输入文件流功能):

#include< fstream>

typedef unsigned int uint32_t;

struct uint32_helper_t {};

namespace std {

模板< class traits>

class basic_istream< uint32_helper_t,traits> :public basic_ifstream< uint32_t> {

上市:

显式basic_istream< uint32_helper_t,traits>(const char * filename,

ios_base :: openmode mode):basic_ifstream< uint32_t>(filename,mode){}

basic_istream< uint32_helper_t,traits>&运算符>>(uint32_t& data){

读(& data,1);

返回*这个;

}

};

} // namespace std {}

然后您可以通过以下方式使用它:

的std :: basic_istream< uint32_helper_t> my_file(FILENAME,std :: ios :: in | std :: ios :: binary);

//一次读一个int

uint32_t值;

my_file>>值;

//读取文件中的所有数据

的std ::矢量< uint32_t的>数据;

data.assign(std :: istream_iterator< uint32_t,uint32_helper_t>(my_file),

std :: istream_iterator< uint32_t,uint32_helper_t>());

参考答案2

To read a single integer, pass in the address of the integer to the read function and ensure you only read sizeof int bytes.

int myint;

//...

fstr.read(reinterpret_cast(&myint), sizeof(int));

You may also need to open the file in binary mode

fstr.open("table.dat", std::ios::binary);

参考答案3

your can do:

int i;

fstr.read((int*)&i, sizeof(int));

参考答案4

Is this what you mean? By the way, your code (and this) assumes native endianness of the data.

const int HRSIZE = 129951336; //The size of the table

int dhr[HRSIZE/sizeof(int)]; //The table

int main()

{

ifstream fstr;

/* load the handranks.dat file */

std::cout << "Loading table.dat...\n";

fstr.open("table.dat");

fstr.read((char*)dhr, HRSIZE);

fstr.close();

}

参考答案5

You could try this:

const int HRSIZE = 129951336/sizeof(int); //The size of the table

int bhr[HRSIZE]; //The table

int main(int argc, char *argv[])

{

ifstream fstr;

/* load the handranks.dat file */

std::cout << "Loading table.dat...\n";

fstr.open("table.dat");

for (int i=0; i

{

fstr.read((char *)(bhr+i), sizeof(int));

}

fstr.close();

// for correctness

return 0;

}

转载地址:https://blog.csdn.net/weixin_33501587/article/details/114714486 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:JAVA串联射频技术_rf-idf的java实现
下一篇:java 二级循环_计算机二级java循环语句

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年03月31日 20时16分46秒