问题描述
您好,我正在尝试完成我的程序,我需要它从文本文件中删除一行。我尝试了其他人的多个代码,试图找出为什么我的 temp.txt 文件不会重命名和替换我的fruit.txt 文件,这是我从中删除一行的文件。任何帮助将不胜感激。
主要
error[E0515]: cannot return value referencing local variable `string`
--> src/main.rs:27:5
|
23 | let lines: Split<&str> = string.split("\n");
| ------ `string` is borrowed here
...
27 | v
| ^ returns a value referencing data owned by the current function
文件处理.h
#include <iostream>
#include "filehandling.h"
using namespace std;
int main()
{
program run;
run.askUser();
return 0;
}
解决方法
问题是你有out_stream.open ("fruit.txt");
,但是你直到程序结束才关闭它,所以你不能remove()
一个打开的文件。您应该始终测试文件操作的返回值,例如 in the example here。
一般来说,您不能“删除一行”,而是在没有您不再需要的数据的情况下重写文件。
如果您不再需要的数据恰好是一行文本,那么生成的文件将显示该行已删除。
,试试:
line.replace(line.find(eraseFruit),eraseFruit.length(),"");
http://www.cplusplus.com/reference/string/string/find/
您也可以尝试擦除。虽然我不确定这是否会有所帮助。