使用外部文件作为 C++ 中的输入
如何使用名为 bados.txt 的文件作为两个数组相乘的输入数据? 即我不想使用cin,而是读取数据文件dados.txt,
#include <iostream>
#include <fstream>
#define MAX 100
using namespace std;
int main()
{
int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX];
int m, n, p, i, j, k, aux;
ifstream arquivo;
arquivo.open("dados.txt");
while (arquivo >> aux)
{
//cin >> m >> n >> p;
//read dados.txt
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin >> A[i][j];
for (i = 0; i < n; i++)
for (j = 0; j < p; j++)
cin >> B[i][j];
for (i = 0; i < m; i++)
for (j = 0; j < p; j++)
{
C[i][j] = 0;
for (k = 0; k < n; k++)
C[i][j] += A[i][k] * B[k][j];
}
for (i = 0; i < m; i++)
{
for (j = 0; j < p; j++)
cout << C[i][j] << " ";
cout << endl;
}
}
arquivo.close();
return 0;
}
dados.txt文件包含以下数据(示例):
3 5 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
How do I use a file called dados.txt as input data for multiplication of two arrays?
That is, I do not want to use cin, but read the data file dados.txt
#include <iostream>
#include <fstream>
#define MAX 100
using namespace std;
int main()
{
int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX];
int m, n, p, i, j, k, aux;
ifstream arquivo;
arquivo.open("dados.txt");
while (arquivo >> aux)
{
//cin >> m >> n >> p;
//read dados.txt
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
cin >> A[i][j];
for (i = 0; i < n; i++)
for (j = 0; j < p; j++)
cin >> B[i][j];
for (i = 0; i < m; i++)
for (j = 0; j < p; j++)
{
C[i][j] = 0;
for (k = 0; k < n; k++)
C[i][j] += A[i][k] * B[k][j];
}
for (i = 0; i < m; i++)
{
for (j = 0; j < p; j++)
cout << C[i][j] << " ";
cout << endl;
}
}
arquivo.close();
return 0;
}
dados.txt The file contains the following data (example):
3 5 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您已经通过声明的文件流从文件中读取文件,arquivo。而不是做 cin >> A[i][j],你只需执行 arquivo >>> A[i][j]。但你还需要拿出arquivo>>来自 while 条件的 aux。按照您的设置方式,您似乎确切知道循环的长度。您实际上甚至不需要 while 循环。这部分代码可以是:
You're already reading from the file through the filestream that you declared, arquivo. Instead of doing cin >> A[i][j], you just do arquivo >> A[i][j]. But you also need to take out arquivo >> aux from the while condition. The way you have it setup, it seems you know exactly how long the loop will be. You don't really even need the while loop. That part of the code can just be:
您正在寻找此参考:
http://www.cplusplus.com/reference/iostream/ fstream/
fstream 对象将允许您打开文件,并以各种方式读取其内容。
You're looking for this reference:
http://www.cplusplus.com/reference/iostream/fstream/
The fstream object will let you open a file, and read its contents in various ways.
将代码中的 cin 替换为 arquivo 就完成了(前提是逻辑没问题)。
例如,
为该对象提供更常规的名称(例如
fin
或filein
)不会有什么坏处。Replace
cin
in the code witharquivo
and you are done (provided that logic is OK).Like,
Giving this object more conventional name (like
fin
orfilein
) would not harm.您的
ifstream arquivo
实际上与std::cin
属于类似的类,因此您可以以完全相同的方式使用它: `arquivo >>>米>> n>> p;"。不过我不明白 aux 变量,你想用它做什么?
Your
ifstream arquivo
is actually of a similar class asstd::cin
, so you can use it exactly the same way: `arquivo >> m >> n >> p;".I don't understand the
aux
variable though, what do you want to do with it?克里斯的链接是值得一看的正确链接。
您可以执行以下操作:(查看 cplusplus.com 网站上的示例)
Chris' link is the right one to look onto.
You can do something like this: (checkout the example on the cplusplus.com site)
您可以像这样从文本文件中读取数字。
打开文件流
读取并存储一行输入
然后您可以使用各种方法(例如操作字符串、将 cstring 转换为 int/float 的方法)从该字符串中提取数字。这取决于您对文本文件的外观了解多少。
You can read numbers from a text file like this.
Open a file stream
Read and store a line of input
And then you can extract the number from that string using various methods (such as those that manipulates string, convert cstring to int/float). It depends on how much you know about how the text file will look like.