我知道这个问题已经被问过很多次了,但是我根本无法理解我做错了什么。 每当我取得一些进展时,我都会收到一个新错误。 我使用的代码非常基础,因为我是新手,我们的教授要求使用scanf并获取。 到目前为止,这是我的代码: 我得到的错误是remove函数的类型冲突。 修复我的代码的任何帮 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我一直在尝试制作此示例的自己的版本:
但是我无法让我正常工作。 我对该程序的当前目标是删除该字符串及其信息(该字符串是第一个字符串下方的字符串),然后执行此操作。 但是,它也为第一行增加了一个额外的空间,使其看起来像这样:
(First Line)
(Second Line) This should be the first line.
这是代码:
infile = ifstream;
outfile = ofstream;
cout<< "What string would you like to delete";
cin>>delstr;
infile.clear();
infile.seekg(0, ios::beg);
ofstream tempfile;
tempfile.open("temp.txt",std::ios::app);
while(delreset == true){
if(delstr == fLine){
getline(infile, fLine);
cout<<"String deleted.\n";
delreset = false;
while(fLine != nothing){
getline(infile, fLine);
tempfile<<fLine<<"\n";
}
tempfile.close();
infile.close();
outfile.close();
remove("example.txt");
rename("temp.txt","example.txt");
}else{
tempfile<<fLine<<endl;
getline(infile, fLine);
}
outfile.flush();
delreset = true;
}
我删除了使其成为实际程序的精简版本的功能,希望我没有进行任何编辑以免变得毫无意义。
一个简单的版本:
... // prepare everything as before
while(getline(infile, fLine)) {
if(delstr == fLine) { // if line found do nothing
cout<<"String deleted.\n";
getline(infile, fLine); // EDIT: and read and ignore the following line
}
else
tempfile<<fLine<<"\n"; // else copy it
}
... // here infile was read and tempfile contains the filtered output
使用这种方法,您甚至可以直接写入输出文件。
顺便说一下, cin>>delstr;
只需要一个字。 它在第一个空格处停止,并忽略结尾的空格。 您可以使用getline(cin, delstr);
代替。
尝试类似这样的方法:
cout << "What string would you like to delete";
getline(cin, delstr);
bool deleted = false;
infile.clear();
infile.seekg(0, ios::beg);
ofstream tempfile;
tempfile.open("temp.txt", std::ios::app);
while (getline(infile, fLine))
{
if ((!deleted) && (fLine == delstr))
{
getline(infile, fLine);
cout << "String deleted." << endl;
deleted = true;
}
else
tempfile << fLine << endl;
}
tempfile.close();
infile.close();
outfile.close();
if (deleted)
{
remove("example.txt");
rename("temp.txt", "example.txt");
}
else
remove("temp.txt");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.