我正在尝试为我正在研究的项目进行一些测试。我试图重命名文件,但根本不起作用,我在做什么错了?
我已经尝试检查close()
和remove()
函数。
#include <fstream>
#include<iostream>
#include <string>
using namespace std;
int main() {
fstream guy;
guy.open("testing.txt", fstream::out);
guy << "i love halav" << endl;
fstream guy1;
guy1.open("test1.txt", fstream::out);
guy1 << "i love halav" << endl;
guy.close();
guy1.close();
remove("testing.txt");
int result = rename("testing1.txt", "testing.txt");
if (result == 0)
cout << "File successfully renamed" << endl;
else
cout << "Error renaming file" << endl;
guy1.close();
return 0;
}
编辑:我编辑了代码,这样会更加困难,再次无法使用。 我期望的结果是newFile.txt的内容将是“ this is oldFile”
#include <fstream>
#include<iostream>
#include <string>
#include <cstdio>
using namespace std;
int main() {
fstream guy;
guy.open("oldFile.txt", fstream::out );
guy << "this is oldFile" << endl;
fstream guy1;
guy1.open("newFile.txt", fstream::out );
guy1 << "this is newFile" << endl;
guy.close();
guy1.close();
std::rename("oldFile.txt", "newFile.txt");
remove("oldFile.txt");
return 0;
}
答案 0 :(得分:1)
使用std::rename
时,第一个参数应该是 old 文件名,第二个参数应该是 new 文件名。您已经交换了参数,所以请尝试:
model.compile(..., run_eagerly=True, ...)
...但是在重命名文件之前不要std::rename("testing.txt", "testing1.txt");
,因为它将删除它。
答案 1 :(得分:0)
现在就这样,谢谢大家!
#include <fstream>
#include<iostream>
#include <string>
#include <cstdio>
using namespace std;
int main() {
fstream guy;
guy.open("oldFile.txt", fstream::out );
guy << "this is oldFile" << endl;
fstream guy1;
guy1.open("newFile.txt", fstream::out );
guy1 << "this is newFile" << endl;
guy.close();
guy1.close();
remove("newFile.txt");
std::rename("oldFile.txt", "newFile.txt");
return 0;
}