使用外部文件作为 C++ 中的输入

发布于 2025-01-07 20:18:09 字数 1256 浏览 1 评论 0原文

如何使用名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风筝有风,海豚有海 2025-01-14 20:18:09

您已经通过声明的文件流从文件中读取文件,arquivo。而不是做 cin >> A[i][j],你只需执行 arquivo >>> A[i][j]。但你还需要拿出arquivo>>来自 while 条件的 aux。按照您的设置方式,您似乎确切知道循环的长度。您实际上甚至不需要 while 循环。这部分代码可以是:

arquivo.open("dados.txt");
for (i = 0; i < m; i++)
    for (j = 0; j < n; j++)
        arquivo >> A[i][j];
for (i = 0; i < n; i++)
    for (j = 0; j < p; j++)
        arquivo >> 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();

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:

arquivo.open("dados.txt");
for (i = 0; i < m; i++)
    for (j = 0; j < n; j++)
        arquivo >> A[i][j];
for (i = 0; i < n; i++)
    for (j = 0; j < p; j++)
        arquivo >> 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();
池予 2025-01-14 20:18:09

您正在寻找此参考:

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.

世态炎凉 2025-01-14 20:18:09

将代码中的 cin 替换为 arquivo 就完成了(前提是逻辑没问题)。

例如,

arquivo >> m >> n >> p;

为该对象提供更常规的名称(例如 finfilein)不会有什么坏处。

Replace cin in the code with arquivo and you are done (provided that logic is OK).

Like,

arquivo >> m >> n >> p;

Giving this object more conventional name (like fin or filein) would not harm.

一江春梦 2025-01-14 20:18:09

您的 ifstream arquivo 实际上与 std::cin 属于类似的类,因此您可以以完全相同的方式使用它: `arquivo >>>米>> n>> p;"。

不过我不明白 aux 变量,你想用它做什么?

Your ifstream arquivo is actually of a similar class as std::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?

岁月蹉跎了容颜 2025-01-14 20:18:09

克里斯的链接是值得一看的正确链接。
您可以执行以下操作:(查看 cplusplus.com 网站上的示例)

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
   string line;
   ifstream myfile ("example.txt");
   if (myfile.is_open())
   {
       while ( myfile.good() )
       {
        getline (myfile,line);
        cout << line << endl;
       //maybe a function-call with the string line to extract the values you need
       }
       myfile.close();
   }

   else cout << "Unable to open file"; 

   return 0;
}

Chris' link is the right one to look onto.
You can do something like this: (checkout the example on the cplusplus.com site)

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
   string line;
   ifstream myfile ("example.txt");
   if (myfile.is_open())
   {
       while ( myfile.good() )
       {
        getline (myfile,line);
        cout << line << endl;
       //maybe a function-call with the string line to extract the values you need
       }
       myfile.close();
   }

   else cout << "Unable to open file"; 

   return 0;
}
束缚m 2025-01-14 20:18:09

您可以像这样从文本文件中读取数字。

打开文件流

ifstream DataFile;  
DataFile.open (fileLocation.c_str(), ios::in); //assume fileLocation is a string type

读取并存储一行输入

string input;    
getline(DataFile, input);

然后您可以使用各种方法(例如操作字符串、将 cstring 转换为 int/float 的方法)从该字符串中提取数字。这取决于您对文本文件的外观了解多少。

You can read numbers from a text file like this.

Open a file stream

ifstream DataFile;  
DataFile.open (fileLocation.c_str(), ios::in); //assume fileLocation is a string type

Read and store a line of input

string input;    
getline(DataFile, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文