我想從包含100個double值的二進制文件中讀取數值數據,並將其存儲在名爲myArray的數組中。當我打印myArray時,沒有顯示任何內容,就好像myArray從未填充過。任何幫助表示讚賞。將數值從二進制文件存儲到數組中 - C++
int main()
{
int file_len;
ifstream infile;
infile.open("mybinfile.bin", ios::binary | ios::in);
if (!infile.is_open())
{
cout << "Error in opening the file. " << endl;
}
else
{
const int file_len = 100;
std::vector<char> myVect;
myVect.resize(file_len);
int i = 0;
infile.read((char *) (&temp), sizeof(char));
myVect[i] = temp;
while(!infile.eof())
{
myVect[i] = temp;
i++;
infile.read((char *) (&temp), sizeof(char));
}
for (int i = 0; i < 100 ; i++)
{cout << i << ": " << myVect[i]<< endl;}
}
infile.close();
return 0;
}
'temp'未定義。 – greggo 2014-11-04 16:37:43