2013-02-22 248 views
-1

我必須使用代碼進行處理,其中一方使用FILE*將信息寫入文件,另一方面使用ifstream進行讀入。使用FILE指針進行寫入,使用ifstream進行讀取

我試圖編譯僞代碼顯示了相同的行爲,原代碼:

#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
#include <fstream> 
#include <iostream> 

int main() 
{ 

    FILE* outFile = fopen("testFile", "w"); 
    char* posBuf = NULL;              
    unsigned int counter = 0;       

    posBuf = (char*) malloc(sizeof(int) + 2*sizeof(double)); 

    int iDummy = 123; 
    memcpy(posBuf+counter, (const void*) &iDummy, sizeof(int));   
    counter += sizeof(int);       

    double dDummy = 456.78; 
    memcpy(posBuf+counter, (const void*) &dDummy, sizeof(double));    

    counter += sizeof(double); 
    dDummy = 111.222; 
    memcpy(posBuf+counter, (const void*) &dDummy, sizeof(double));    

    fputs(posBuf, outFile); 
    fclose(outFile);   

    ///////////////////// 

    std::ifstream myfile; 
    myfile.open("testFile", std::ios::in|std::ios::binary); 

    myfile.seekg (0, std::ios::end);       
    unsigned int length = myfile.tellg();    
    myfile.seekg (0, std::ios::beg);       

    char* posBuf2 = (char*) malloc(length);   
    myfile.read(posBuf2, length);      

    counter = 0; 
    int idummy = 0; 
    memcpy((void*) &idummy, posBuf2+counter, sizeof(int)); 
    counter += sizeof(int);          
    printf("read integer: %u\n", idummy);    

    double ddummy = 1.0; 
    memcpy((void*) &ddummy, posBuf2+counter, sizeof(double));     
    counter += sizeof(double);          
    printf("read double: %f\n", ddummy);          

    ddummy = 1.0; 
    memcpy((void*) &ddummy, posBuf2+counter, sizeof(double));     
    counter += sizeof(double);          
    printf("read double: %f\n", ddummy);          

    myfile.close(); 

    ///////////////////// 

    FILE* inFile = fopen("testFile", "r"); 
    char* posBuf3 = NULL; 

    unsigned int c = 0; 
    while (! feof (inFile)) 
    { 
     posBuf3 = (char*) realloc((void*) posBuf3, c+4); 
     fgets(posBuf3+c, 4, inFile); 
     c += 4; 
    } 

    idummy = 0; 
    memcpy((void*) &idummy, posBuf, sizeof(int)); 
    printf("read again integer: %u\n", idummy); 

    ddummy =1.0; 
    memcpy((void*) &ddummy, posBuf+sizeof(int), sizeof(double)); 
    printf("read again double: %f\n", ddummy); 

    ddummy =1.0; 
    memcpy((void*) &ddummy, posBuf+sizeof(int)+sizeof(double), sizeof(double)); 
    printf("read again double: %f\n", ddummy); 

    return 0; 
} 

我能從中得到的輸出是:

read integer: 123 
read double: 0.000000 
read double: 0.000000 
read again integer: 123 
read again double: 456.780000 
read again double: 111.222000 

正如你所看到的,反序列化只有當我使用FILE*也可用於讀取文件時纔有效。

問題:該行爲的任何解釋?

謝謝!

修訂

1)開放ifstream使用std::ios::in|std::ios::binary

2)修復的malloc

+1

你認爲成功打開該流與**'的ios ::在檢查| IOS :: binary' **? – WhozCraig 2013-02-22 09:01:24

+0

該死的打我吧@WhozCraig – 2013-02-22 09:01:50

+0

@JasonLarke = P,我甚至突出顯示= P – WhozCraig 2013-02-22 09:02:15

回答

2

的幾個問題與發佈代碼:它是寫登天

  • 的內存分配給posBuf(1 int和2 double被複制到內存中,但只分配了sizeof(int) + sizeof(double)),這是未定義的行爲。
  • fputs()將其參數視爲空終止的字符串,因此遇到空字符時將停止寫入。以二進制模式打開文件,並使用fwrite(),而不是將其輸入視爲以空字符結尾的字符串。

該代碼還有其他幾個問題;

  • 它是C的一個可怕的混合和C++
  • 避免明確的動態內存管理(不去管malloc()realloc())。簡單替換爲:

    char posBuf[sizeof(int) + 2 * sizeof(double)]; 
    
  • while (!feof(inFile))
  • 幾乎沒有任何I/O操作的
+0

'fwrite'而不是'fputs'完成了這項工作。 – ezdazuzena 2013-02-22 10:53:28

相關問題