Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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
Can';t从二进制文件中一次读取一个Int 我正在学习C++的IO操作,从二进制文件中读取问题。我用100个随机值初始化数组,并将数组中的值写入.bin文件。我想我把那个部分搞定了——出于某种原因,虽然我的.bin文件大小是1600字节,而不是400字节——但我正在努力读取这些值_C++_Io - Fatal编程技术网

Can';t从二进制文件中一次读取一个Int 我正在学习C++的IO操作,从二进制文件中读取问题。我用100个随机值初始化数组,并将数组中的值写入.bin文件。我想我把那个部分搞定了——出于某种原因,虽然我的.bin文件大小是1600字节,而不是400字节——但我正在努力读取这些值

Can';t从二进制文件中一次读取一个Int 我正在学习C++的IO操作,从二进制文件中读取问题。我用100个随机值初始化数组,并将数组中的值写入.bin文件。我想我把那个部分搞定了——出于某种原因,虽然我的.bin文件大小是1600字节,而不是400字节——但我正在努力读取这些值,c++,io,C++,Io,我认为可以通过迭代包含读取值的memblock数组来逐个读取int,但是我的控制台输出显示一个随机数,后跟一组零。我将感谢你的指导 // reading a binary file int by int #include <iostream> #include <fstream> #include <stdlib.h> #include <stdio.h> #include <time.h> using namespace std;

我认为可以通过迭代包含读取值的
memblock
数组来逐个读取int,但是我的控制台输出显示一个随机数,后跟一组零。我将感谢你的指导

// reading a binary file int by int 
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;

int main () {
    FILE * pFile; 
    streampos size;
    int * memblock;
    srand(time(NULL));
    int array[100];

    //data to write initially stored in array
    for (int i=0;i<100;i++){
        array[i]=rand()%100;
    }

    pFile = fopen("writebinary.bin","wb");
    fwrite(array,sizeof(int),sizeof(array),pFile);
    fclose(pFile);

    ifstream readFile("writebinary.bin",ios::binary | ios::ate);
    if (readFile.is_open()){
        size = readFile.tellg();
        //allocate memory to read file contents
        memblock = new int[size];
        readFile.seekg(0,ios::beg);
        for (int i=0;i<100;i++){ //100 because there are 100 ints,I'm assuming 32 bits are read a at a time from writebinary.bin
            readFile.read((char*)memblock,sizeof(int));
        }
        readFile.close();
    }
    else {
        cout << "can't open file " << endl;
    }
    for (int i=0;i<100;i++){
        cout << memblock[i] << endl;
    }
    return 0;
}
//逐int读取二进制文件
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
文件*pFile;
streampos大小;
int*memblock;
srand(时间(空));
整数数组[100];
//要写入的数据最初存储在数组中

对于(int i=0;i那么,对于您的第一个问题,
sizeof(array)
是400,因为您的100个int的数组占用400字节,
sizeof(int)
是4,因为int是32位。这就是为什么您会得到一个1600字节的输出文件。您在其他地方硬编码数字100;您也需要在这里硬编码,或者使用
sizeof(数组)/sizeof(int)

其次,使用istream::read时不需要迭代。它需要读取缓冲区的大小(以字节为单位)。读取((char*)memblock,sizeof(array));
只需一次


另一个注释,虽然,你的代码是C风格编程和C++ IoSoad编程的一个MISH混搭。为什么要使用FOpen/F/Wrd/fCox?你应该使用STD::OFFROW,并使用成员函数,类似于你的代码> Read Field.读< /COL> < < /P> < P>,对于你的第一个问题,<代码> siZeof(数组)是400,因为100个整数的数组占用400字节;

sizeof(int)
是4,因为一个整数是32位。这就是为什么你会得到一个1600字节的输出文件。你在其他地方硬编码数字100;你也需要在这里硬编码,或者使用
sizeof(array)/sizeof(int)

其次,使用istream::read时不需要迭代。它需要读取缓冲区的大小(以字节为单位)。读取((char*)memblock,sizeof(array));只需一次


另一个注释,虽然,你的代码是一个C风格编程和C++ iSoFiod编程的MISH混搭。为什么要使用FOpen/F/Wrd/FLUX?你应该使用STD::OFFROW,并使用成员函数,类似于你的代码> Read Frask.Read < /C>。

你试过写和读一个代码> int <代码>吗?你不应该使用“IO::ATE”当从文件读取时。是的,这很好。使用ios::ate有什么问题?您正在将所有100个数字读取到
memblock
的第一个元素中。您是否尝试过写入和读取一个
int
?您不应该使用“ios::ate”当从文件中读取时。是的,这很好。使用ios::ate有什么问题?您正在将所有100个数字读取到
memblock
的第一个元素中。硬编码100在其他地方并不完全准确:
new int[tellg()]
再次犯同样的错误,分配1600个
int
对象(6400字节)@BenVoigt对,我太快了。很好。是的,谢谢。我使用循环的原因是因为我的作业需要它,例如
以二进制格式一次执行一个值的文件I/O
硬编码100其他地方不完全准确:
new int[tellg()]
再次犯了同样的错误,分配1600个
int
对象(6400字节)@BenVoigt对,我在那里太快了。捕捉得好。是的,谢谢。我使用循环的原因是因为我的任务需要它,例如
以二进制格式一次执行一个值的文件I/O