从Ubuntu中的CPP程序创建新文件
Creating a new file from a cpp program in Ubuntu
如何从ubuntu中的CPP程序创建一个新文件,与Windows有什么不同。
声明流类文件,并以书面模式打开该文本文件。如果文件不存在,则将创建一个新的文本文件。然后检查文件是否不存在,然后返回false,否则返回true。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Driver code
int main()
{
const char *path = "/home/user/Gfg.txt";
// fstream is Stream class to both
// read and write from/to files.
// file is object of fstream class
fstream file(path);
// opening file "Gfg.txt"
// in out(write) mode
// ios::out Open for output operations.
file.open(path, ios::out);
// If no file is created, then
// show the error message.
if (!file)
{
cout << "Error in creating file!!!" << endl;
return 0;
}
cout << "File created successfully." << endl;
// closing the file.
// The reason you need to call close()
// at the end of the loop is that trying
// to open a new file without closing the
// first file will fail.
file.close();
return 0;
}
源geeksforgeeks:C 程序创建文件
如果使用Linux与GCC/G 命令行编译工具,用:
编译程序g++ your_program.cpp -o your_program
您可以使用命令向文件添加执行权限:
sudo chmod a+x your_program
然后双击它,它将执行
或:您可以使用诸如code :: blocks/clion
相关文章:
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 从文件中读取多行.txt字符串删除空格并创建新文件进行输出
- 如何限制文件大小,以便我的程序在变大之后创建一个新文件?并编辑新创建的文件的名称
- 需要更新Qt .pro和Visual Studio .vcxproj添加新文件
- C 如何将二进制文件的一部分复制到新文件
- 将单词写入新文件
- Clang 使用 LibTooling Rewriter 生成新文件
- 从Ubuntu中的CPP程序创建新文件
- Windows Portable设备 - 创建 /复制 /删除新文件时通知
- 代码块在尝试编译或创建新文件时出现"An assertion failed!"错误
- C++,打开和编辑文本文件中的数字数据,并将结果放入新文件中
- C++读取添加到目录中的新文件
- boost::log关闭日志文件并打开一个新文件
- 可执行文件无法在双击执行时创建新文件
- 如何知道何时将新文件添加到 Windows 中的文件系统
- 流未正确创建新文件
- 使用std :: ifstream打开一个新文件
- 从一个字符串复制到另一个字符串并粘贴到新文件中
- inotify 的替代方法,用于检测何时在文件夹下创建新文件
- 此项目不会将整数读取为二进制并将其作为二进制输出到新文件