0

私はかなり簡単なタスクを実行しようとしていますが、FILE* の仕組みに困惑しています。そのため、ヘッダーとバイナリ形式のポイントを持つバイナリ ファイル (las ファイル) を作成しています。これは巨大なファイルなので、部分的にやっています。しかし、問題は、ファイル ポインタがファイルのほぼ 3/4 を正しく書き込んでから、完全に間違ったファイル位置ポインタを与える場合に発生します。

//struct points written to 
struct las_points{
    float x;
    float y;
    float z;
    float time;
};
int writepoints(){
     outfilename = "pointcloud_13.las";
     fout = fopen(outfilename,"wb+");
     // numPts - total number of points to be processed
     // numRec - number of records processed every time
     numRec = numPts/4;
     las_points *lp;
     // ending size of the file
     int endSize = 0,j=0;
     // header is written 
     // endSize = 233
     for(i=0;i<numPts;i+=numRec){
        lp = (las_points*)malloc(sizeof(las_points)*numRec);
        // some processing done 
        printf("Iteration - %d\n",j);
        lasWritePoints(lp,fout,numRec,endSize);
        fflush(fout);
        free(lp);
        lp = NULL;
     }
     fclose(fout);
}
int lasWritePoints(las_points*lp, FILE* fout,int numRec,int &endSize){
     if(!fout || !lp){
          printf("File couldnt be written \n");
          exit(1);
     }
     printf("endSize - %d \n",endSize);
     fseek(fout,endSize,SEEK_SET);
     for(int i=0;i<numRec;i++) {
         fwrite(&lp[i].x,sizeof(float),1,fout);
         fwrite(&lp[i].y,sizeof(float),1,fout);
         fwrite(&lp[i].z,sizeof(float),1,fout);
         fseek(fout,8,SEEK_CUR);
         fwrite(&lp[i].time,sizeof(float),1,fout);
     }
     endSize = ftell(fout);
     printf("endSize - %d \n",endSize);
}

バイナリファイルの書き込みのみが再現されます。問題は、ファイルの最初の 4 回の繰り返しで、スムーズに実行されることです。次に、最後の反復の終わりに、それが与える endSize は最初の endSize よりも小さくなります。

 Output:
 Total size of file to be read: 1258456752
 Iteration : 0
 endSize : 233
 endSize : 550575041
 Iteration : 1
 endSize : 550575041
 endSize : 1101149849
 Iteration : 2
 endSize : 1101149849
 endSize : 1651724657
 Iteration : 3
 endSize : 1651724657
 endSize : 54815783

誰かが私が間違っていることを指摘できますか?

4

3 に答える 3

1

int32 ビット(約 20 億 = 2 GB)で表現できるよりも多くのバイトを書き込んでいます。a を使用しlongて の結果を保存しますftell()

long endSize = ftell(fout);
printf("endSize - %ld\n", endSize);
于 2013-07-02T20:50:07.870 に答える
0

逆参照する必要がありますendSize

ファイルのサイズ/位置をポインターに割り当てているため、予期しない動作が発生します。

endSize = ftell(fout);

s/b

*endSize = ftell(fout);

これは、C で最もよくある間違いの 1 つです。

于 2013-07-03T19:13:10.850 に答える
0

おそらく、32 ビット版のファイル アクセス機能に行き詰まっているでしょう。どのプラットフォーム/コンパイラ/ファイル システムを使用していますか?

オンになっている場合:

  • gcc を使用する Linux では、コンパイラ コマンドに -D_FILE_OFFSET_BITS=64 を追加してみてください。
  • Windowsでmingwを使用している場合は、fseeko と ftello を使用してみてください
  • Visual C++ を使用する 32 ビット Windows では、_openを使用してみてください(ドロップインの置き換えではありません)。
  • 2GB のファイル サイズ制限があるファイル システムでは、お悔やみを申し上げます
于 2013-07-03T03:01:18.843 に答える