Problems with fwrite
Problems with fwrite
我正在为一个赋值做一个外部归并排序,我有两个结构体:
// This is the definition of a record of the input file. Contains three fields, recid, num and str
typedef struct {
unsigned int recid;
unsigned int num;
char str[STR_LENGTH];
bool valid; // if set, then this record is valid
int blockID; //The block the record belongs too -> Used only for minheap
} record_t;
// This is the definition of a block, which contains a number of fixed-sized records
typedef struct {
unsigned int blockid;
unsigned int nreserved; // how many reserved entries
record_t entries[MAX_RECORDS_PER_BLOCK]; // array of records
bool valid; // if set, then this block is valid
unsigned char misc;
unsigned int next_blockid;
unsigned int dummy;
} block_t;
我还得到了这个:
void MergeSort (char *infile, unsigned char field, block_t *buffer,
unsigned int nmem_blocks, char *outfile,
unsigned int *nsorted_segs, unsigned int *npasses,
unsigned int *nios)
现在,在阶段0,我像这样分配内存:
buffer = (block_t *) malloc (sizeof(block_t)*nmem_blocks);
//Allocate disc space for records in buffer
record_t *records = (record_t*)malloc(nmem_blocks*MAX_RECORDS_PER_BLOCK*sizeof(record_t));
然后,在我从二进制文件中读取记录(运行顺利)之后,我用以下命令将它们写入多个文件(当然是在排序和其他一些步骤之后):
outputfile = fopen(name.c_str(), "wb");
fwrite(records, recordsIndex, sizeof(record_t), outputfile);
和这样读:
fread(&buffer[b].entries[rec],sizeof(record_t),1,currentFiles[b])
它有效!然后,我想将其中一些文件组合起来,使用priority_queue转换为minheap生成一个更大的排序文件(经过测试,它可以工作),但是当我尝试使用以下命令写入文件时:
outputfile = fopen(outputName.c_str(), "ab"); //Opens file for appending
fwrite(&buffer[nmem_blocks-1].entries, buffer[nmem_blocks-1].
nreserved, sizeof(record_t), outputfile);
它在文件中写入无意义的内容,就好像它读取内存的随机部分一样。
我知道代码可能远远不够,但是所有的代码都相当大。我要确保在使用新名称再次打开输出文件之前关闭它。此外,我使用memset()
(而不是free()
)来清除缓冲区,然后再填充它。
最后,主要问题是我试图打开文件的方式:
outputfile = fopen(outputName.c_str(), "ab"); //Opens file for appending
我应该再用一次:
outputfile = fopen(outputName.c_str(), "wb"); //Opens file for writing to end of file
因为文件在此期间从未关闭,所以它试图打开一个已经打开的文件进行追加,并且它不能很好地工作。但你不可能知道,因为你没有完整的代码。谢谢你的帮助!:)
相关文章:
- Problems with std::cin.fail()
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 这对"With a stackless coroutine, only the top-level routine may be suspended."意味着什么
- Boost.TEST with CLion: "Test framework quit unexpectedly"
- 避免碎片化的ClientHellos with OpenSSL (DTLS)
- Issues with Win32 ReadProcessMemory API
- Qt with WinAPI MouseProc
- [[maybe_unused]] with structured_binding?
- Issue with WriteProcessMemory
- OpenCV RTP-Stream with FFMPEG
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- std::adjacent_difference with std::chrono time_point
- DLL Made with CMake 使程序崩溃
- QtCreator with C 库中的链接器问题
- SHBrowseForFolder with BIF_BROWSEFORCOMPUTER and SHGetPathFr
- specialized std::default_delete with QQmlComponent
- VS2019 - Sudo Remote Debugging on Linux with Cmake project
- Inference pytorch C++ with alexnet and cv::imread image
- Pybind11: init<> with lambda
- Problems with fwrite