在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

huangapple go评论69阅读模式
英文:

Reading multiple matrix files in C++17, how can I write the results of each multiplication to a single output file?

问题

在这段代码中,我创建了带有像1.txt、2.txt、3.txt等名称的矩阵条目的文件,总共包含2个矩阵。程序读取每个文件中的2个矩阵,并将其相乘,然后将结果矩阵打印到名为output.txt的文本文件中。但是,在打印时,它只乘以3.txt文件中的值,这是最后一个文本文件,并创建一个仅包含这些值的output.txt文件。我希望它将每个输入文件的结果打印到同一个输出文件中。但是我无法做到这一点。我该如何做?

#include <iostream>
#include <fstream>
#include <filesystem>
#include <pthread.h>
using namespace std;

#define MAX 5
#define MAX_THREAD 5

int matA[MAX][MAX];
int matB[MAX][MAX];
int matC[MAX][MAX];
int mc_rows = 0;

void* multi(void* arg)
{
    int i = mc_rows++; // i表示结果矩阵matC的行号

    for (int j = 0; j < MAX; j++)
        for (int k = 0; k < MAX; k++)
            matC[i][j] += matA[i][k] * matB[k][j];

    pthread_exit(0);
}

void ReadMatricesFromFile(const std::filesystem::path& directory)
{
    int fileCount = 0;
    for (const auto& entry : std::filesystem::directory_iterator(directory))
    {
        if (entry.is_regular_file() && entry.path().extension() == ".txt")
        {
            ifstream inputFile(entry.path());
            if (!inputFile)
            {
                cout << "Failed to open " << entry.path() << endl;
                continue;
            }

            cout << "Reading " << entry.path() << endl;

            for (int i = 0; i < MAX; i++) {
                for (int j = 0; j < MAX; j++) {
                    inputFile >> matA[i][j];
                    cout << matA[i][j] << " ";
                }
                cout << endl;
            }

            cout << endl;

            for (int i = 0; i < MAX; i++) {
                for (int j = 0; j < MAX; j++) {
                    inputFile >> matB[i][j];
                    cout << matB[i][j] << " ";
                }
                cout << endl;
            }

            cout << endl;

            inputFile.close();
            fileCount++;
        }
    }

    if (fileCount == 0)
    {
        cout << "No .txt files found in the directory: " << directory << endl;
    }
}

int main()
{
    std::filesystem::path directoryPath = "/mnt/c/users/monster/desktop/matris/txt_files"; // 在这里输入文件夹路径
    ReadMatricesFromFile(directoryPath);

    pthread_t threads[MAX_THREAD];

    for (int i = 0; i < MAX_THREAD; i++) {
        int* p;
        pthread_create(&threads[i], NULL, multi, (void*)(p));
    }

    for (int i = 0; i < MAX_THREAD; i++)
        pthread_join(threads[i], NULL);

    // 将结果矩阵写入output.txt
    ofstream outputFile("output.txt");
    if (!outputFile) {
        cout << "Failed to create output.txt" << endl;
        return 1;
    }

    for (int i = 0; i < MAX; i++) {
        for (int j = 0; j < MAX; j++) {
            outputFile << matC[i][j] << " ";
        }
        outputFile << endl;
    }

    outputFile.close();

    return 0;
}

1.txt文件:

在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

2.txt文件:

在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

3.txt文件:

在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

4.txt文件:

在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

输出文件结果,但仅针对4.txt文件 在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

英文:

In this code, I created files with matrix entries with names such as 1.txt, 2.txt, 3.txt and containing a total of 2 matrices. The program reads the 2 matrices in each file and multiplies the pairs of matrices it reads and prints the resulting matrix to a text file called output.txt. But when printing, it only multiplies the values in the 3.txt file, which is the last text file, and creates an output.txt file containing only those values. I want it to print the results for each input file to the same output file. But I couldn't do that. How can I do that ?

`#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;filesystem&gt;
#include &lt;pthread.h&gt;
using namespace std;
#define MAX 5
#define MAX_THREAD 5
int matA[MAX][MAX];
int matB[MAX][MAX];
int matC[MAX][MAX];
int mc_rows = 0;
void* multi(void* arg)
{
int i = mc_rows++; // i denotes row number of resultant matC
for (int j = 0; j &lt; MAX; j++)
for (int k = 0; k &lt; MAX; k++)
matC[i][j] += matA[i][k] * matB[k][j];
pthread_exit(0);
}
void ReadMatricesFromFile(const std::filesystem::path&amp; directory)
{
int fileCount = 0;
for (const auto&amp; entry : std::filesystem::directory_iterator(directory))
{
if (entry.is_regular_file() &amp;&amp; entry.path().extension() == &quot;.txt&quot;)
{
ifstream inputFile(entry.path());
if (!inputFile)
{
cout &lt;&lt; &quot;Failed to open &quot; &lt;&lt; entry.path() &lt;&lt; endl;
continue;
}
cout &lt;&lt; &quot;Reading &quot; &lt;&lt; entry.path() &lt;&lt; endl;
for (int i = 0; i &lt; MAX; i++) {
for (int j = 0; j &lt; MAX; j++) {
inputFile &gt;&gt; matA[i][j];
cout &lt;&lt; matA[i][j] &lt;&lt; &quot; &quot;;
}
cout &lt;&lt; endl;
}
cout &lt;&lt; endl;
for (int i = 0; i &lt; MAX; i++) {
for (int j = 0; j &lt; MAX; j++) {
inputFile &gt;&gt; matB[i][j];
cout &lt;&lt; matB[i][j] &lt;&lt; &quot; &quot;;
}
cout &lt;&lt; endl;
}
cout &lt;&lt; endl;
inputFile.close();
fileCount++;
}
}
if (fileCount == 0)
{
cout &lt;&lt; &quot;No .txt files found in the directory: &quot; &lt;&lt; directory &lt;&lt; endl;
}
}
int main()
{
std::filesystem::path directoryPath = &quot;/mnt/c/users/monster/desktop/matris/txt_files&quot;; // Klas&#246;r yolunu buraya girin
ReadMatricesFromFile(directoryPath);
pthread_t threads[MAX_THREAD];
for (int i = 0; i &lt; MAX_THREAD; i++) {
int* p;
pthread_create(&amp;threads[i], NULL, multi, (void*)(p));
}
for (int i = 0; i &lt; MAX_THREAD; i++)
pthread_join(threads[i], NULL);
// Write the result matrix to output.txt
ofstream outputFile(&quot;output.txt&quot;);
if (!outputFile) {
cout &lt;&lt; &quot;Failed to create output.txt&quot; &lt;&lt; endl;
return 1;
}
for (int i = 0; i &lt; MAX; i++) {
for (int j = 0; j &lt; MAX; j++) {
outputFile &lt;&lt; matC[i][j] &lt;&lt; &quot; &quot;;
}
outputFile &lt;&lt; endl;
}
outputFile.close();
return 0;
}`

1.txt file:

在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

2.txt file:

在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

3.txt file:

在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

4.txt file:

在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

OUTPUT FILE RESULTS BUT FOR ONLY 4.txt FILE 在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

在C++17中读取多个矩阵文件,如何将每次乘法的结果写入单个输出文件?

答案1

得分: 1

你的文件名为"output.txt",对于所有矩阵都是如此,当默认打开文件时,它会清空并删除其内容。

你有两个选项:

  1. 指定你的代码不要删除output.txt文件的内部内容:可能类似于这样

  2. 每个矩阵都有一个输出文件:output1.txt,output2.txt等等...

希望这有所帮助。

英文:

Your file is named "output.txt" for all matrices, and when opening a file by default it cleans and delete its content.

You have two options :

  1. Specify your code to not delete the inside of the output.txt file : something like this probably

  2. have an output file per matrix : output1.txt, output2.txt etc...

Hope that helps

huangapple
  • 本文由 发表于 2023年6月2日 04:18:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定