1

ich habe ein seltsames verhalten, wenn ich ein Struktur in einer Datei schreibe.

ファイルに書き込むと、従業員 13 と 26 (およびそれ以降の従業員) の ID2 /ID3 が常に間違っています。(出力を参照)

私は何を間違っていますか?

アドバイスありがとうございます。

typedef  struct Employee
{
    char name[128];
    int ID1;
    int ID2;
    int ID3;
}employee;


void  __fastcall TMainForm::LogHeader(char* filename)
{
   char cBuffer[2048]={0};
   FILE *pFile;
   int n = 0;
    Employee header;
    if(FileExists(filename)){
        pFile = fopen(filename,"r");
        fread(&header,sizeof(struct Employee),1,pFile);
        fclose(pFile);
    }
    memset(cBuffer,sizeof(cBuffer),0);
    n = 0;
    n+= sprintf(cBuffer+n,"%s\t",header.name);
    n+= sprintf(cBuffer+n,"ID1: %d\t",header.ID1);
    n+= sprintf(cBuffer+n,"ID2: %d\t",header.ID2);
    n+= sprintf(cBuffer+n,"ID3: %d\t",header.ID3);
    log(cBuffer);
}

void __fastcall TMainForm::Save3Click(TObject *Sender)
{
    FILE *pFile;
    int iWritten = 0;
    Employee header;
    char fn[MAX_PATH]={0};
     __int16 *mesauredValues;
    header.ID1 = 1280;
    mesauredValues        = new __int16[header.ID1];
    for(int i = 1;i<32;i++){
        setmem(header.name,sizeof(header.name),0);
        sprintf(header.name,"employee number: %.2d",i);
        header.ID2= i;
        header.ID3= i;
        sprintf(fn,"D:\\temp\\Testfile_%.4d.dat",i);
        for(int j = 0;j<header.ID1;j++){mesauredValues[j] = j;}
        pFile = fopen(fn,"wb");
        assert(pFile != NULL);
        iWritten =  fwrite(&header,sizeof(struct Employee),1,pFile);
        iWritten += fwrite(mesauredValues,sizeof(__int16),header.ID1,pFile);
        fclose(pFile);
        LogHeader(fn);
  }
    delete mesauredValues;  mesauredValues = NULL;
}
//---------------------  output:
employee number: 01 ID1: 1280   ID2: 1  ID3: 1  
employee number: 02 ID1: 1280   ID2: 2  ID3: 2  
employee number: 03 ID1: 1280   ID2: 3  ID3: 3  
employee number: 04 ID1: 1280   ID2: 4  ID3: 4  
employee number: 05 ID1: 1280   ID2: 5  ID3: 5  
employee number: 06 ID1: 1280   ID2: 6  ID3: 6  
employee number: 07 ID1: 1280   ID2: 7  ID3: 7  
employee number: 08 ID1: 1280   ID2: 8  ID3: 8  
employee number: 09 ID1: 1280   ID2: 9  ID3: 9  
employee number: 10 ID1: 1280   ID2: 10 ID3: 10 
employee number: 11 ID1: 1280   ID2: 11 ID3: 11 
employee number: 12 ID1: 1280   ID2: 12 ID3: 12 
employee number: 13 ID1: 1280   ID2: 0  ID3: 0  
employee number: 14 ID1: 1280   ID2: 14 ID3: 14 
employee number: 15 ID1: 1280   ID2: 15 ID3: 15 
employee number: 16 ID1: 1280   ID2: 16 ID3: 16 
employee number: 17 ID1: 1280   ID2: 17 ID3: 17 
employee number: 18 ID1: 1280   ID2: 18 ID3: 18 
employee number: 19 ID1: 1280   ID2: 19 ID3: 19 
employee number: 20 ID1: 1280   ID2: 20 ID3: 20 
employee number: 21 ID1: 1280   ID2: 21 ID3: 21 
employee number: 22 ID1: 1280   ID2: 22 ID3: 22 
employee number: 23 ID1: 1280   ID2: 23 ID3: 23 
employee number: 24 ID1: 1280   ID2: 24 ID3: 24 
employee number: 25 ID1: 1280   ID2: 25 ID3: 25 
employee number: 26 ID1: 1280   ID2: 2089983994 ID3: 0  
employee number: 27 ID1: 1280   ID2: 27 ID3: 27 
employee number: 28 ID1: 1280   ID2: 28 ID3: 28 
4

1 に答える 1

1

「rb」と「wb」で開くと、CTRL-Z (ASCII 26) がファイルの終わりとして解釈されているように見えます。

于 2013-04-02T16:06:19.653 に答える