C++ 使用Ubuntu gedit写入文件
我正在尝试编写一个简单的程序,它可以写入一个已经存在的文件。我得到这个错误:C++ 使用Ubuntu gedit写入文件,c++,ubuntu,gedit,C++,Ubuntu,Gedit,我正在尝试编写一个简单的程序,它可以写入一个已经存在的文件。我得到这个错误: hello2.txt:无法识别文件:文件被截断 collect2:ld返回了1个退出状态 我做错了什么?(我尝试了两种方法,但仍然得到相同的错误。) #包括 #包括 使用名称空间std; int main() { 流外流; exptream.open(hello3.txt); 流出有两个错误: hello3.txt是一个字符串,因此应该用引号括起来 std::ofstream::close()是一个函数,因此需要括号
hello2.txt:无法识别文件:文件被截断
collect2:ld返回了1个退出状态
我做错了什么?(我尝试了两种方法,但仍然得到相同的错误。)
#包括
#包括
使用名称空间std;
int main()
{
流外流;
exptream.open(hello3.txt);
流出有两个错误:
hello3.txt是一个字符串,因此应该用引号括起来
std::ofstream::close()是一个函数,因此需要括号
更正后的代码如下所示:
#include <iostream>
#include <fstream>
int main()
{
using namespace std; // doing this globally is considered bad practice.
// in a function (=> locally) it is fine though.
ofstream outStream;
outStream.open("hello3.txt");
// alternative: ofstream outStream("hello3.txt");
outStream << "testing";
outStream.close(); // not really necessary, as the file will get
// closed when outStream goes out of scope and is therefore destructed.
return 0;
}
#包括
#包括
int main()
{
使用名称空间std;//全局执行此操作被认为是错误的做法。
//不过,在函数(=>局部)中,这很好。
流外流;
exptream.open(“hello3.txt”);
//备选方案:ofstream EXTREAM(“hello3.txt”);
流出有两个错误:
hello3.txt是一个字符串,因此应该用引号括起来
std::ofstream::close()是一个函数,因此需要括号
更正后的代码如下所示:
#include <iostream>
#include <fstream>
int main()
{
using namespace std; // doing this globally is considered bad practice.
// in a function (=> locally) it is fine though.
ofstream outStream;
outStream.open("hello3.txt");
// alternative: ofstream outStream("hello3.txt");
outStream << "testing";
outStream.close(); // not really necessary, as the file will get
// closed when outStream goes out of scope and is therefore destructed.
return 0;
}
#包括
#包括
int main()
{
使用名称空间std;//全局执行此操作被认为是错误的做法。
//不过,在函数(=>局部)中,这很好。
流外流;
exptream.open(“hello3.txt”);
//备选方案:ofstream EXTREAM(“hello3.txt”);
外流我不知道<代码> GEDIT <代码>是怎么进入的,除非你用它写代码,写代码为<代码> HeloL3.TXT < /Cord>你说的是什么斜杠?代码中没有一个……这不是Ubuntu真正的专属,这是普通的C++,它只适用于C++编译器的任何东西。无法识别:文件被截断
看起来像是生成系统消息。您应该从IDE项目中删除hello2.txt,因为它不是源代码文件。我不确定gedit
是如何产生的,除非您使用它来编写写入hello3.txt
的代码。您在说什么斜杠?您的代码中没有斜杠代码……这不是Ubuntu的专属,这是普通C++,它将处理C++编译器的任何东西。<代码> Helo2.txt:文件未被识别:文件截断
看起来像一个构建系统消息。您应该从IDE项目中删除HelLo.txt,因为它不是源代码文件。