2014-01-18 55 views
1

以下代碼在Windows和GNU C++,VS10,VS12,Intel C++ 14.0下不起作用。下面的代碼可以在Linux和GNU C++ 4.7,4.8,Intel C++ 14,Open64 5.0下工作。在內部測試for-loop中用DIMEN-256替換DIMEN ...作品!?任何想法?C++ ofstream寫入在Windows下不起作用。在Linux下工作正常

//============================// 
// Read and Write binary file // 
// using buffers    // 
//============================// 

#include <iostream> 
#include <fstream> 
#include <string> 
#include <cmath> 

using namespace std; 

int main() 
{ 
    // 1. variables and parameters 

    const long int DIMEN = static_cast<long int>(pow(10.0,8.0)); 
    const long int I_DO_MAX = 100; 
    const string fileName = "my_file.bin"; 
    ofstream fileOUT; 
    ifstream fileIN; 
    double* myArrayAlpha = new double [DIMEN]; 
    double* myArrayBeta = new double [DIMEN]; 
    long int i; 
    long int j; 

    // 2. build the array with some data 

    cout << " 1 --> Build the array with some data" << endl; 

    for (i = 0; i < DIMEN; i++) 
    { myArrayAlpha[i] = static_cast<double>(i); } 

    for (i = 0; i < I_DO_MAX; i++) 
    { 
    // 3. open the file stream 

    cout << "-------------->>> " << i << endl; 
    cout << " 2 --> Open the file stream" << endl; 

    fileOUT.open(fileName.c_str(), ios::out | ios::binary | ios::trunc); 
    fileIN.open(fileName.c_str(), ios::in | ios::binary); 

    // 4. test if the file stream is opened 

    cout << " 3 --> Test if the file stream is opened with success" << endl; 

    if (!fileOUT.is_open()) 
    { cout << "Error! The output file stream is not opened. Exit." 
      << endl; return -1; } 

    if (!fileIN.is_open()) 
    { cout << "Error! The input file stream is not opened. Exit." 
      << endl; return -1; } 

    // 5. write the contents of myArrayAlpha[] to a file 

    cout << " 4 --> Write and then Read to the file" << endl; 

    fileIN.seekg(0, fileIN.beg); 
    fileOUT.seekp(0); 

    fileOUT.write(reinterpret_cast<char*>(&myArrayAlpha[0]), 
        DIMEN * sizeof(double)); 
    fileIN.read(reinterpret_cast<char*>(&myArrayBeta[0]), 
       DIMEN * sizeof(double)); 

    // 6. test that I am writting and reading correctly 

    for (j = 0; j < DIMEN; j++) // replace DIMEN 
    {       // with DIMEN-256 to work under Windows 
     if (myArrayAlpha[j] != myArrayBeta[j]) 
     { cout << myArrayAlpha[j] << endl; 
     cout << myArrayBeta[j] << endl; 
     cout << "j = " << j << endl; 
     cout << "Error!"; return -1; } 
    } 

    cout << " 5 --> Read and Write with success" << endl; 
    cout << " 6 --> Close the I/O streams" << endl; 

    // 7. close the file stream 

    fileIN.close(); 
    fileOUT.close(); 
    } 

    // 8. free up the RAM 

    delete [] myArrayAlpha; 
    delete [] myArrayBeta; 

    return 0; 
} 
+0

這可能是未定義行爲的一種情況。 – 0x499602D2

+1

什麼意思是「不起作用」? –

+0

可能是因爲您在執行寫入同一文件而不同步兩個流之後直接執行讀取操作。嘗試在write()調用後添加'fileOUT << std :: flush'。 – 0x499602D2

回答

2

的問題是,你的數據沒有被刷新到write調用後外部序列,所以還是定位在內部緩衝區。在write()之後添加此行:

fileOUT << std::flush; 
+0

0x499602D2謝謝!添加fileOUT << std :: flush;解決了Windows和VS10,VS12,Intel C++ 13.0下的問題。 – paulsepolia

相關問題