2013-08-17 64 views
0

我在写,读取和删除文件的内容。除了删除部分以外,其他所有工作都正常,因为当我按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); 
} 
+0

您可能想要在调试器中运行程序,然后逐行执行。你也应该提供'dta'的声明。最后,从文件中删除条目时,应从* original *文件中读取并写入临时文件。 –

+0

删除部分按预期工作,new.dat文件将包含删除的所有记录。 – Alex

+0

你的'typedef'是错的应该是'typedef struct {char str [16]; } ch;',用'-Wall'启用所有警告。 – Alex

回答

1

编辑:寻找你的代码再次,我看到的问题是,你正在使用的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 + '\0' 
     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个字符长。

+0

thnx它工作!!!!! – Vaibhav

1

您的代码按预期工作,它只显示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(); 
    } 
}