gpt4 book ai didi

c++ - 将 fwrite() 和 fread() 与 malloc 和 realloc 一起使用

转载 作者:行者123 更新时间:2023-11-28 08:10:11 24 4
gpt4 key购买 nike

我的类(class)项目遇到了问题。我会马上说我不是要求为我完成它,我只是想更清楚地了解如何使用这些功能。看来我正在以正确的方式使用它们,因为它最多可以添加 16 个元素,但随后它会卡在 fclose() 或段错误上。

这是我正在处理的内容。这是一个 FAT16 文件系统:

directoryTable 是结构的“数组”。

void writeDirectory(int FATTable[], directoryEntry* directoryTable)
{
int currentCluster = 1;
directoryEntry* currentWrite = directoryTable;

FILE* theFile = fopen (fileSystemName, "rb+");
if(theFile != NULL)
{
cout << fseek (theFile, (clusterSize * currentCluster), SEEK_SET) << endl;
cout << fwrite(currentWrite, clusterSize, 1, theFile) << endl ;

while(FATTable[currentCluster] != 0xFFFF)
{
currentWrite = currentWrite + numberOfEntries;
currentCluster = FATTable[currentCluster];
cout << fseek (theFile, (clusterSize * currentCluster), SEEK_SET) << endl;
cout << fwrite(currentWrite, clusterSize, 1, theFile) << endl;
}

fflush(theFile);
cout << "Closing.." << errno << endl;
fclose(theFile);
}
else
cout << "FILE COULDN'T OPEN." << endl;

//Clean up that pointer
free(directoryTable);
}

基本上,一个指针 (currentWrite) 从另一个指针 (directoryTable) 的开头开始,并一次将一个字节的 clusterSize 写入文件的各个部分。如果还有更多的数组/指针(directoryTable)剩余,它将递增 currentWrite up clusterSize 字节并再次写入。

然后它将读取并使用此构建数组/指针:

directoryEntry* readDirectory(int FATTable[])
{
int currentCluster = directoryIndex;

//Allocate a clusterSize of memory
directoryEntry* directoryTable;
directoryTable = (directoryEntry*) malloc(clusterSize);

if(directoryTable == NULL)
cout << "!!! ERROR: Not enough memory!" << endl;

//A pointer to a part of that array
directoryEntry* currentRead = directoryTable;
numberOfDirTables = 1;

FILE* theFile = fopen (fileSystemName, "rb+");
if(theFile != NULL)
{
//Seek to a particular cluster in the file (
cout << fseek (theFile, (clusterSize * currentCluster), SEEK_SET) << endl;
cout << fread(currentRead, clusterSize, 1, theFile) << endl;

while(FATTable[currentCluster] != 0xFFFF)
{
numberOfDirTables++;
currentCluster = FATTable[currentCluster];
directoryTable = (directoryEntry*) realloc(directoryTable, (clusterSize*numberOfDirTables));
if(directoryTable == NULL)
cout << "!!! ERROR: Not enough memory!" << endl;
currentRead = currentRead + numberOfEntries;
cout << fseek (theFile, (clusterSize * currentCluster), SEEK_SET) << endl;

cout << fread(currentRead, clusterSize, 1, theFile) << endl;
}
cout << "Closing..." << errno << endl;
fclose(theFile);
cout << "Closed." << endl;
}
else
cout << "FILE COULDN'T OPEN." << endl;
return directoryTable;
}

基本上它会从第一个簇中读取并将其放入一个数组中。然后它将增加 currentRead clusterSize 字节数并从另一个集群读取。但它首先重新分配,因此数组扩展了另一个 clusterSize 字节。

所以我的问题是,我是否正确使用了 fwrite、fread、malloc 和 realloc?它会出现,因为它会一直工作到某个时候。但是我在 C++ 方面不是太强,所以我想我遗漏了一个导致主要内存问题的小东西。

另外,既然我正在构建一个结构数组,我应该改用 calloc 吗?

最佳答案

当您 realloc() directoryTable 时,它​​(可能)获得与以前不同的内存空间(地址)。那么,realloc之后,currentRead指向哪里呢? (主要问题..)

关于c++ - 将 fwrite() 和 fread() 与 malloc 和 realloc 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9344796/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com