使用 Ubuntu gedit 写入文件
我正在尝试编写一个简单的程序来写入已存在的文件。我收到此错误:
hello2.txt:无法识别文件:文件被截断
collect2:ld返回1退出状态
我做错了什么? (我尝试了两种斜杠,但仍然遇到相同的错误。)
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream outStream;
outStream.open(hello3.txt);
outStream<<"testing";
outStream.close;
return 0;
}
I am trying to write a simple program that writes to a file that already exists. I am getting this error:
hello2.txt: file not recognized: File truncated
collect2: ld returned 1 exit status
What am I doing wrong? (I tried the slashes both ways and I still get the same error.)
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream outStream;
outStream.open(hello3.txt);
outStream<<"testing";
outStream.close;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
其中有两个错误:
hello3.txt 是一个字符串,因此应该用引号引起来。
std::ofstream::close() 是一个函数,因此需要括号。
更正后的代码如下所示:
请注意:此代码会覆盖该文件中先前存在的任何内容。
There are two errors in it:
hello3.txt is a string and should therefore be in quotes.
std::ofstream::close() is a function, and therefore needs parenthesis.
The corrected code looks like this:
And beware: This code overwrites anything that was previously in that file.