如何在多个 (.txt) 文件中输出内容
How to output content in multiple (.txt) files
我创建了一个程序,该程序可以从文件中获取整数作为输入,并生成从1到从文件中读取的整数的乘法表。例如,如果程序从文件中读取 (3(,它将输出:
1*1 = 1
1*2 = 2
... up to
1*10 = 10
and then
2*1 = 1
.....
2*10 = 10
and so on up to three suppose that the number read from the file is 3
3*1 = 1
....
3*10 = 30
现在,我正在尝试在不同的(.txt(文件中输出每个乘法表,例如table1.txt将包含1*1 = 1 .... up to 1*10 = 10
,table2.txt将包含2*1 = 2 .... up to 2*10 = 10
和表3.txt的相同过程。
我只能创建一个仅包含第一个乘法表的文件,并且我不知道如何在不同的文件中显示其余的表。
我非常感谢解决这个问题的任何帮助或见解。谢谢!
这是我所拥有的:
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
int num, a, b;
fstream inputStream;
ofstream outputStream;
inputStream.open("input.txt"); //let's say input.txt holds the number 3
while (inputStream >> num)
outputStream.open("table.txt");
for (a = 1; a <= num; a++)
{
for (b = 1; b <= 10; b++)
{
outputStream << a << " X "
<< b << " = "
<< a*b << endl;
}
inputStream.close();
outputStream.close();
}
return 0;
}
#include <iostream>
#include <string>
#include <fstream>
int main(void) {
const int Count = 10; //Count of files
std::string name = "example_"; //base pattern of file name
std::ofstream outfstr[Count]; //creating array of 10 output file streams
for(int i = 0; i < Count; ++i) { //open all file streams
outfstr[i].open(name + char('0' + i) + ".txt");
}
for(int i = 0; i < Count; ++i) { // write value of i to i-th stream
outfstr[i] << "Some rezult " << i;
}
return 0;
}
相关文章:
- 如何在 的开头<x>和结尾<y>之间更改带有文件输出的字符串的值
- C++对象数组的文件输出
- bcdedit 文件输出不起作用,但其他命令可以
- 如何从文本文件输出数据?(学生成绩单计划)
- 使用 C++文件 I/O 输出 pdf 文件
- C 文件输出垃圾
- 从堆栈中读取字符后,如何修复不正确的文件输出
- C++:具有随机名称的文件输出
- 在命令提示符下看到换行符,但在文件输出中可以看到 相同的字符串
- 生成文件不会生成预期的文件输出
- C++11 fgetc 在"r+b"模式下使用时向我的文件输出 0
- 使用函数从文件输出
- GCC.obj文件输出不具有确定性(.debug_info,PROGPITS部分)
- C cstdiofile :: Writestring给出了不正确的文件输出
- C 输出:STD :: COUT和文件输出没有相同的内容
- GCC和MSVC之间的文件输出不一致
- C++文件输出奇怪的数字,并且部分代码没有运行
- 如何将特定字符从TXT文件输出到特定点
- C 调用从TXT文件输出信息的函数
- 将交易从文本文件输出到屏幕