如何通过脚本将输出写入C++中的文本文件
How can I write output to text file in C++ by script
我写了一段代码来创建两个给定范围的随机数。我想写输出结果(两个随机数)。在脚本文件中,我想将每次迭代的输出写在同一行中,作为我的预期结果
我当前的输出是
1 0
1 0
2 1
2 3
0 0 //output of second iteration
1 1
2 2
1 3
我的预期结果是
1 0 0 0
1 0 1 1
2 1 2 2
2 3 1 3
这是我的完整代码
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <time.h> /* time */
#define random(x) (rand()%x)
int main(int argc, char **argv) {
unsigned int time_ui = static_cast<unsigned int>( time(NULL) );
srand( time_ui );
int number1;
int number2;
int range=atoi(argv[1]);
number1 = random(range);
number2 = random(range);
//std::cout<< number1 << "t" <<number2;
std::ofstream myfile;
myfile.open ("report.txt", std::ios::app);
myfile << number1 << "t" <<number2<<'n';
myfile.close();
return 0;
}
我的脚本文件是
#!/bin/bash
a=1
iter=0
for ((iter; iter <= 1; iter++))
do
a=1
while [ $a -lt 4]
do
./random_num $a
a=`expr $a + 1`
done
done
请注意,您在C++代码中输出了一个换行符,这使得几乎不可能"取消输出"。我建议将逻辑从bash转移到C++代码中,反之亦然。C++有一些选项,比如setfill,可以帮助你控制它。
相关文章:
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 如何创建一个QTableWidgetItem,用长文本右对齐,左边有省略号
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- 如何根据单词在文本中出现的概率输出单词
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端