英文:
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文件:
2.txt文件:
3.txt文件:
4.txt文件:
输出文件结果,但仅针对4.txt文件
英文:
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 <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 file:
2.txt file:
3.txt file:
4.txt file:
OUTPUT FILE RESULTS BUT FOR ONLY 4.txt FILE
答案1
得分: 1
你的文件名为"output.txt",对于所有矩阵都是如此,当默认打开文件时,它会清空并删除其内容。
你有两个选项:
-
指定你的代码不要删除output.txt文件的内部内容:可能类似于这样
-
每个矩阵都有一个输出文件: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 :
-
Specify your code to not delete the inside of the output.txt file : something like this probably
-
have an output file per matrix : output1.txt, output2.txt etc...
Hope that helps
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论