删除文件的内容无效

Deleting the contents of the file not working

本文关键字:无效 文件 删除      更新时间:2023-10-16

我正在编写、读取和删除文件的内容。除了删除部分外,其他一切都很好,因为当我按y时,它显示已删除,但不显示任何记录。

typedef struct ch
{
    char str[10];
};
void disp(ch d)
{
    cout<<"n"<<d.str<<"n";
}
//delete part
cout<<"nwant to delete??";
char c;
cin>>c;
if(c=='y')
{
    char s[10];
    cout<<"nter - ";
    cin>>s;
    file.seekg(0);
    int found=0;
    fstream temp("temp.dat",ios::in|ios::out|ios::app);
    while(file.read((char *)&dta,sizeof(dta)))
    {
        if(strcmp(dta.str,s)==0)
        {
            found=1;
            cout<<"deleted";
        }
        else
        temp.write((char *)&dta,sizeof(dta));
    }
    if(!found)
        cout<<"not found";
    remove("new.dat");
    rename("temp.dat","new.dat");
    temp.close();
    file.open("new.dat",ios::in|ios::out|ios::app);
}

编辑:再次查看您的代码,我发现问题是您正在使用ios::应用程序,尽管您也在中传递了ios::。

ios::app——所有输出操作都在文件末尾执行,并附加将内容转换为文件的当前内容。此标志只能是用于仅对输出操作开放的流中。

http://www.cplusplus.com/doc/tutorial/files/

旧帖子:

看看下面的代码示例:

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
using namespace std;
const char g_szTestData[] = "This is some test da$$$$######ta and some more"
                            " tes$$$$######ting";
int main(int argc, char** argv)
{
    fstream file("new.dat", ios::in|ios::out);
    file << g_szTestData << flush;
    cout << "Do you want to delete all $$$$######?";
    if (cin.get() == 'y')
    {
        char szBuffer[10];                  //< File read buffer
        char szString[11] = "$$$$######";   //< 10 characters + ''
        bool bFound = false;
        fstream temp("temp.dat", ios::out);
        file.seekg(0, file.beg);
        while (file.read(szBuffer, sizeof(szBuffer)))
        {
            if (strncmp(szBuffer,szString,10) == 0) bFound = true;
            else temp.write(szBuffer, sizeof(szBuffer));
        }
        temp.flush();
        temp.close();
        if (bFound)
        {
            file.close();
            remove("new.dat");
            rename("temp.dat", "new.dat");
            file.open("new.dat",ios::in|ios::out);
        }
        else cout << "Pattern Not Found!" << endl;
    }
    /* Do something with the contents of file. */
    // Lets clean up at the end
    file.close();
    return 0;
}

在本例中,创建了一个文件并添加了内容g_szTestData,您可以通过打开文件(在按"y"之前)来验证这一点。

然后询问用户是否要从文件中删除一个由10个字符组成的字符串$$$$######。如果用户希望继续,则打开一个新文件temp.dat。程序逐渐遍历现有的new.dat文件(每次10个字符)。如果程序从new.dat读取的字符串不是目标字符串,则将该字符串写入临时文件。

如果找到目标字符串,则关闭两个文件,删除旧文件,并将新文件重命名为旧文件的名称。然后打开新文件,以便程序可以对其内容进行额外的处理。

可以使用cin >> szString向用户询问他们希望删除的字符串,而不是固定的10个字符的字符串,但该字符串需要10个字符长。

您的代码按预期工作,它只显示deleted,因为disp()在任何地方都没有被调用。

#include <iostream>
#include <string.h>
#include <fstream>
#include <stdio.h>
using namespace std;
typedef struct {
    char str[16];
} ch;
static void disp(ch d) {
    cout<<d.str<<"n";
}
int main(int argc, char **argv) {
    fstream file;
    ch dta;
    file.open("new.dat",ios::in|ios::out|ios::trunc);
    for (int i=0; i<3; i++) {
        snprintf((char *)&dta.str, sizeof(dta.str)-1, "rec%d", i);
        file.write((char *)&dta,sizeof(dta));
    }
    //delete part
    {
        char s[16] = "rec1";
        file.seekg(0);
        int found=0;
        fstream temp("temp.dat",ios::out);
        while(file.read((char *)&dta,sizeof(dta))) {
            if(strcmp(dta.str, s)==0) {
                found=1;
                cout<<"deleted * ";
                disp(dta);
            } else {
                temp.write((char *)&dta,sizeof(dta));
                disp(dta);
            }
        }
        if(!found)
            cout<<"not found";
        file.close();
        remove("new.dat");
        rename("temp.dat","new.dat");
        temp.close();
    }
}