繁体   English   中英

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

[英]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 文件。 我希望它将每个输入文件的结果打印到同一个 output 文件。 但我不能那样做。 我怎样才能做到这一点?

`#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 denotes row number of resultant 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"; // Klasör yolunu buraya girin
    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);

    // Write the result matrix to 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文件:

在此处输入图像描述

2.txt文件:

在此处输入图像描述

3.txt文件:

在此处输入图像描述

4.txt文件:

在此处输入图像描述

在此处输入图像描述

OUTPUT 文件结果,但仅适用于 4.txt 文件:(

在此处输入图像描述

所有矩阵的文件都命名为“output.txt”,默认情况下打开文件时,它会清除并删除其内容。

你有两个选择:

  1. 指定您的代码不删除 output.txt 文件的内部: 可能是这样的

  2. 每个矩阵有一个 output 文件:output1.txt、output2.txt 等...

希望有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM