如何使用命令行参数写入和输出文件
How to write and output file using command line arguments
我正在尝试使用命令行参数将一行文本传递到输出文件中。我知道你可以用一个输入文件来做这件事。我正在使用unix运行一个程序,我编译它并像这样运行:
g++ -o program program.C
./program
那么,我该如何运行程序,将一行文本"Something like this"写入out.txt输出文件中呢。
因此,如果您的命令行看起来像./program <filename> <text_to_append>
,则以下内容将起作用:
#include <fstream>
int main(int argc, char * argv [])
{
// first argument is program name
if (argc == 3)
{
std::ofstream ofs;
ofs.open (argv[1], std::ofstream::out | std::ofstream::app);
ofs << argv[2];
ofs.close();
}
return 0;
}
相关文章:
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- VI工作室代码错误无法打开输出文件主.exe
- 在代码块中查找输出文件
- 将未签名的字符写入输出文件
- 为什么我的代码不写入输出文件?
- 完成搜索任务时为空输出文件
- 如何将地图<列表<string>、矢量<string>>输出到输出文件?
- 无法在代码块 IDE 中打开输出文件.exe
- C++:更改 IF 语句中的输出文件
- 编译 gsoap 输出文件 (onvif) 时出现问题
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- while 循环中的流输出文件() 通常生成空文件
- 如何使用 Docker 访问输出文件
- 提升进程间共享内存删除、权限和输出文件
- 查找素数,然后将质数放入输出文件中
- GDB / C++ - 根据循环条件将多个变量记录到输出文件中
- 带有文件结束函数的 while 循环重复输出文件中的最后一个数字两次
- 我的输出文件未显示解决方案
- 输入文件中的第一个字符是输出文件中的最后一个字符,反之亦然
- C++,我正在尝试创建一个有效的.bmp输出文件