Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C+;以二进制模式读取整个文件+; 我试图用Visual C++读取二进制模式下的整个JPG文件。代码如下: FILE *fd = fopen("c:\\Temp\\img.jpg", "rb"); if(fd == NULL) { cerr << "Error opening file\n"; return; } fseek(fd, 0, SEEK_END); long fileSize = ftell(fd); int *stream = (int *)malloc(fileSize); cout << fileSize << '\n'; fseek(fd, 0, SEEK_SET); int bytes_read = fread(stream, fileSize, 1, fd); printf("%i\n", bytes_read); fclose(fd); FILE*fd=fopen(“c:\\Temp\\img.jpg”、“rb”); 如果(fd==NULL){ 瑟尔_C++_Visual C++ - Fatal编程技术网

使用C+;以二进制模式读取整个文件+; 我试图用Visual C++读取二进制模式下的整个JPG文件。代码如下: FILE *fd = fopen("c:\\Temp\\img.jpg", "rb"); if(fd == NULL) { cerr << "Error opening file\n"; return; } fseek(fd, 0, SEEK_END); long fileSize = ftell(fd); int *stream = (int *)malloc(fileSize); cout << fileSize << '\n'; fseek(fd, 0, SEEK_SET); int bytes_read = fread(stream, fileSize, 1, fd); printf("%i\n", bytes_read); fclose(fd); FILE*fd=fopen(“c:\\Temp\\img.jpg”、“rb”); 如果(fd==NULL){ 瑟尔

使用C+;以二进制模式读取整个文件+; 我试图用Visual C++读取二进制模式下的整个JPG文件。代码如下: FILE *fd = fopen("c:\\Temp\\img.jpg", "rb"); if(fd == NULL) { cerr << "Error opening file\n"; return; } fseek(fd, 0, SEEK_END); long fileSize = ftell(fd); int *stream = (int *)malloc(fileSize); cout << fileSize << '\n'; fseek(fd, 0, SEEK_SET); int bytes_read = fread(stream, fileSize, 1, fd); printf("%i\n", bytes_read); fclose(fd); FILE*fd=fopen(“c:\\Temp\\img.jpg”、“rb”); 如果(fd==NULL){ 瑟尔,c++,visual-c++,C++,Visual C++,返回文件大小的块数。在本例中为1 参见C标准第7.21.8.1节: (第334页) 因此,您需要将n_read与fileSize相乘,以获得读取的字节数 RETURN VALUE fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or

返回文件大小的块数。在本例中为1

参见C标准第7.21.8.1节: (第334页)

因此,您需要将
n_read
fileSize
相乘,以获得读取的字节数

RETURN VALUE
       fread() and fwrite() return the number of items  successfully  read  or
       written  (i.e.,  not the number of characters).  If an error occurs, or
       the end-of-file is reached, the return value is a short item count  (or
       zero).
返回文件大小的块数。在本例中为1

参见C标准第7.21.8.1节: (第334页)

因此,您需要将
n_read
fileSize
相乘,以获得读取的字节数

RETURN VALUE
       fread() and fwrite() return the number of items  successfully  read  or
       written  (i.e.,  not the number of characters).  If an error occurs, or
       the end-of-file is reached, the return value is a short item count  (or
       zero).
您让它读取一个大小为
fileSize
的项目,它照做了


您告诉它读取一个大小为
fileSize
的项目,它确实读取了。

如果您想要读取的字节数,您需要如下切换参数:

int bytes_read = fread(stream, 1, fileSize, fd);

如果要读取字节数,则需要如下切换参数:

int bytes_read = fread(stream, 1, fileSize, fd);

man 3p fread

fread()和fwrite()返回成功读取或写入的项目数(即,不是字符数)。如果发生错误,或到达文件末尾,则返回值为短项目 计数(或零)


您告诉它读取1个文件长度,它就是这样读取的。

来自
man 3p fread

fread()和fwrite()返回成功读取或写入的项目数(即,不是字符数)。如果发生错误,或到达文件末尾,则返回值为短项目 计数(或零)


<>你告诉它读1个文件长度,这就是它所读的。C++中的

< P>,你可以使用<代码> STD::IFSturis/Cuff>。
std::ifstream file("file.bin", std::ifstream::binary); //binary mode!
std::istream_iterator<char> begin(file), end);  //setup iterators!
std::vector<char> v(begin,end);  //read all data into a vector!
//v contains the binary data, which you can use from now on

//you can get the pointer to the data as
char *buffer = &v[0];
size_t sizeOfBuffer = v.size();
//you can use buffer and sizeOfBuffer instead of v.

//just remember that the lifetime of buffer is tied with the lifetime of v
//which means, if v goes out scope, the pointer `buffer` will become invalid
std::ifstream文件(“file.bin”,std::ifstream::binary);//二进制模式!
std::istream_迭代器开始(文件),结束);//设置迭代器!
std::vector v(begin,end);//将所有数据读入一个向量!
//v包含二进制数据,从现在起可以使用
//您可以按如下方式获取指向数据的指针
char*buffer=&v[0];
size_t sizeOfBuffer=v.size();
//您可以使用buffer和sizeOfBuffer代替v。
//请记住,缓冲区的生存期与v的生存期是绑定的
//这意味着,如果v超出范围,指针'buffer'将无效

<>希望你阅读上面的片段中的注释:-(+)C++中的

< p>,你可以使用<代码> STD::IFSturt,它以惯用的阅读风格来作为:

std::ifstream file("file.bin", std::ifstream::binary); //binary mode!
std::istream_iterator<char> begin(file), end);  //setup iterators!
std::vector<char> v(begin,end);  //read all data into a vector!
//v contains the binary data, which you can use from now on

//you can get the pointer to the data as
char *buffer = &v[0];
size_t sizeOfBuffer = v.size();
//you can use buffer and sizeOfBuffer instead of v.

//just remember that the lifetime of buffer is tied with the lifetime of v
//which means, if v goes out scope, the pointer `buffer` will become invalid
std::ifstream文件(“file.bin”,std::ifstream::binary);//二进制模式!
std::istream_迭代器开始(文件),结束);//设置迭代器!
std::vector v(begin,end);//将所有数据读入一个向量!
//v包含二进制数据,从现在起可以使用
//您可以按如下方式获取指向数据的指针
char*buffer=&v[0];
size_t sizeOfBuffer=v.size();
//您可以使用buffer和sizeOfBuffer代替v。
//请记住,缓冲区的生存期与v的生存期是绑定的
//这意味着,如果v超出范围,指针'buffer'将无效

希望您能阅读上述片段中的评论。:-

在您给fread的电话中,您告诉它读取1字节


它应该是:fread(stream,1,filesize,fd);

在您对fread的调用中,您告诉它读取1字节


它应该是:fread(stream,1,filesize,fd);

相反,
fread(stream,1,filesize,fd)
返回读取的字节数。是的。我认为这个答案+您的注释对pokiman应该足够了。相反,
fread(stream,1,filesize,fd)
返回读取的字节数。是的。我认为这个答案+你的评论对pokiman来说应该足够了。除了
istream\u迭代器
读取格式化输入,跳过空白。
istreambuf\u迭代器
,也许?@Rob:我想,不是。
istream\u迭代器
不读取数据;它只是迭代读取的数据通过流对象本身,在本例中,它也将读取空白,因为它是以
std::ifstream::binary
模式打开的。除了
istream\u迭代器
读取格式化输入,跳过空白。
istreambuf\u迭代器
,可能?@Rob:我想,不。
istream\u迭代器
不读取数据,它只是读取数据操作流对象本身读取的数据,在本例中,当使用
std::ifstream::binary
模式打开时,流对象本身也将读取空白。另请参见: