删除文件的内容无效
Deleting the contents of the file not working
我正在编写、读取和删除文件的内容。除了删除部分外,其他一切都很好,因为当我按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();
}
}
相关文章:
- FFMPEG配置文件级别id大小无效
- wxWidgets 拖放文件事件处理程序初始化问题(无效static_cast)
- Dll中缺少什么(致命错误LNK1107:无效或损坏的文件:)
- 配置过程项目文件中的错误可能无效
- 我在C++中无效使用不完整的类型类错误,如何正确包含头文件?
- 从带有 getline() 的文件读入一行上有多个信息得到无效参数错误
- 编译native_app_glue.c会导致库文件无效
- 将文本文件读取为 const char* 有时有效,有时无效
- 生成文件:没有规则的依赖项不会使父项无效
- 瓦尔格林德在文件*上的读取无效
- 编译项目时文件无效或损坏,包括 V8
- 获取无效或损坏的文件LNK1107:尝试链接 OpenSceneGraph 教程.dll时无法0x378读取
- 在循环内删除文件时使提升recursive_directory_iterator无效
- 通过fscanf()获取文本文件中的所有字符将返回无效的字符计数
- CMAKE 错误项目文件无效;不包含 cmakelists.txt
- 从文件读取链表C++从 void* 到 char* 的转换无效
- 通过套接字发送二进制文件.文本文件有效,其他文件无效
- 读取文本文件会导致缓冲区末尾的字符无效
- C++为什么在一种情况下保存到文件有效,而另一种情况则无效
- 执行 bash shell 命令并提取输出 -->无效文件错误