c - 使用 windows.h、C 截断文件

标签 c struct replace binaryfiles truncate

我的任务是创建一个随机访问/二进制列表,并允许程序使用 windows.h 库及其 HANDLE 函数从中删除目标记录(结构)。 我编写了以下代码来尝试截断文件,虽然删除文件末尾的某些内容似乎有效,但中间处理文件的部分似乎遇到了麻烦,我被难住了。

void erase_record(FILE *file, stct buffer, char *target)
{
HANDLE h;
int jump=0;
int mark;
stct s;
file = fopen("list.txt","rb+");
s.artist = (char*)malloc(20*(sizeof(char)));
s.name = (char*)malloc(20*(sizeof(char)));
while(1)
{
    jump++;
    fread(&buffer,sizeof(buffer),1,file);
    if(!strcmp(target,buffer.name)) // did we find a match? 
    {
        jump--;
        fread(&buffer,sizeof(buffer),1,file); // then check one more time.
        if(feof(file)) // is the match right before the eof?
        {
            fclose(file);
            h=CreateFile(L"list.txt",GENERIC_WRITE | GENERIC_READ,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
            SetFilePointer(h,(jump)*sizeof(buffer),NULL,FILE_BEGIN); // truncate from last
            SetEndOfFile(h);
            CloseHandle(h);
            break;
        }
        else // is it in middle of the file?
        {
            mark = jump; // we mark the struct we wanted to delete
            while(1)
            {
                s.shelf = buffer.shelf;
                s.artist = buffer.artist;
                s.name = buffer.name;
                jump++;
                fread(&buffer,sizeof(buffer),1,file); // find the end of the file first
                if(feof(file))// when we DO find the end..
                {
                    break;
                }
            }
            fclose(file);
            h=CreateFile(L"list.txt",GENERIC_WRITE | GENERIC_READ,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
            SetFilePointer(h,(jump)*sizeof(buffer),NULL,FILE_BEGIN); //deleting it!
            SetEndOfFile(h);
            CloseHandle(h);
            file = fopen("list.txt","rb+");
            fseek(file,mark*sizeof(buffer),SEEK_SET); // we erase the marked struct
            fwrite(&s,sizeof(s),1,file);
            break;
        }
    }
}

}

任何人都可以分享有关问题所在的任何见解吗?

最佳答案

我认为这个问题与以下内容有关。当您说出以下内容时,一分钱都会掉下来:

Well, i checked with ferror() but it returned 0. The program just crashes after i try running the section i mentioned and after running through a debug tool i get "Unhandled exception at 0x577514cf (msvcr100d.dll) in Project 5.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd."

0xcdcdcdcd

"...a debugging value that comes from the bowels of the C runtime library. When you allocate a block of memory in a debug build, it's initialized to this spurious value in the hope of catching bugs. 0xCDCDCDCD is non-NULL and is never a valid memory pointer..." (http://www.microsoft.com/msj/1198/c/c1198.aspx).

这意味着某些东西无法分配内存。

我要做的第一件事是检查 malloc() 语句是否返回 NULL

接下来,检查fopen()的返回值是否为NULL

接下来是以下几行...

s.artist = buffer.artist;
s.name = buffer.name;

他们引入了内存泄漏。您在这里不是复制字符串,而是复制指针。因此,您之前使用 malloc()artistname 分配的内存现已丢失,您将永远无法释放() 它。例如,我认为您可能打算使用 strcpy() 将字符串从 buffer 复制到 s.name 中?

此外,您是否正在将指针写入文件?如果您读回 buffer.name 并且它是一个指针(我认为这一定是因为您这样对待它),那么您正在读回创建文件时有效的任何旧地址, 但在这个实例中,您的程序可能完全无效!尝试取消引用该指针会让您头疼!

我建议,当您将结构写入文件时,不要写入指针s.names.artist,而是写入实际的字符串到文件。然后从文件中读回字符串,然后为它们创建指针。

fwrite(&s,sizeof(s),1,file); // OOPS! You wrote pointer values to a file. 
// These pointers will be invalid addresses in the next program instance!

而是做一些类似的事情..

fwrite(all non pointer members of struct)
fwrite(&s.label, LABEL_LEN, 1, file); // Note, include the string null-terminator
fwrite(&s.name, NAME_LEN, 1, file);   // Note, include the string null-terminator

然后,当您阅读时,将标签和名称读回为实际字符串......

fread(all non pointer members of struct)
fread(&stringBuffer, LABEL_LEN, 1, file);
s.label = strdup(stringBuffer);
fread(&stringBuffer, NAME_LEN, 1, file);
s.name= strdup(stringBuffer);

然后记住将 free() s.names.label 作为 strdup()已创建一个副本,您有责任销毁该副本。

关于c - 使用 windows.h、C 截断文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16747075/

相关文章:

c++ - 对 vector 中的较高值和较低值进行排序

c - 如何在 SIGSEGV 上使用 _Unwind_Backtrace 获取 fullstacktrace

c++ - 无法在 OpenSSL 中设置公钥/私钥

c - 如何更新 X86 中 double 组中的所有元素?

Eclipse 不会在查找/替换中返回到文件的开头

php - 替换应该在 mysql 还是 php 中完成?

java - 从文本中删除回车符和特殊字符

c - 在c中使用while循环查找a1z,b2y,c3x,...,...,....,nnn的斐波那契数列的程序?

c++ - 对从文件读取到结构中的结构感到困惑

c++ - 变量的前向声明?