我正在使用cstdio (stdio.h)
從二進制文件讀取和寫入數據。由於遺留代碼,我必須使用這個庫,並且它必須與Windows和Linux跨平臺兼容。我有一個FILE* basefile_
,我用它來讀取變量configLabelLength
和configLabel
,其中configLabelLength
告訴我有多少內存分配給configLabel
。我可以使用無指針的fread讀取動態長度變量嗎?
unsigned int configLabelLength; // 4 bytes
char* configLabel = 0; // Variable length
fread((char *) &configLabelLength, 1, sizeof configLabelLength, baseFile_);
configLabel = new char[configLabelLength];
fread(configLabel,1, configLabelLength,baseFile_);
delete [] configLabel; // Free memory allocated for char array
configLabel = 0; // Be sure the deallocated memory isn't used
有沒有辦法在configLabel
不使用指針讀?例如,有一種解決方案,我可以使用C++矢量庫或者我不必擔心指針內存管理的問題。
有什麼理由不使用C++的文件流嗎? – Cogwheel 2010-07-15 19:19:37
@Cog:在問題的頂部。 :)和+1尋求使用向量。 – GManNickG 2010-07-15 19:21:13
@Cogwheel由於遺留代碼,我不得不使用'cstdio(stdio.h)'。我知道這並不理想。 – Elpezmuerto 2010-07-15 19:21:26