问题描述
我创建了一个程序,它可以从一个文件中获取一个整数作为其输入,并生成从1到从该文件读取的整数的乘法表。例如,如果程序从文件读取(3),它将输出:
I created a program that can take an integer as its input from a file and generate multiplication tables from 1 up to that integer read from the file. For example, if the program reads (3) from the file, it will output:
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 ....高达1 * 10 = 10
,table2.txt将包含 2 * 1 = 2 ....最多2 * 10 = 10
和table3.txt中的相同过程。
Now, I am trying to output each multiplication tables in different (.txt) files where for example table1.txt would contain 1*1 = 1 .... up to 1*10 = 10
and table2.txt would contain 2*1 = 2 .... up to 2*10 = 10
and the same procedure for table3.txt.
文件,只包含第一个乘法表,我不知道如何显示其他表在不同的文件。
I can only create one file that only contains the first multiplication table and I do not know how to display the rest of the tables in different files.
我真的很感谢任何帮助或见解解决这个问题。谢谢!
I would really appreciate any help or insights to solve this problem. Thank you!
这是我有的:
#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;
}
这篇关于如何输出多个(.txt)文件中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!