Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7557207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:04:19+00:00 2026-05-30T12:04:19+00:00

I want to read two matrices from two different txt files, and say output

  • 0

I want to read two matrices from two different txt files, and say output them in another one.
I doesn’t write all of them, it only writes the fist one and “Hi” and stops there.
So I believe it cannot read the second file.
Here is the code:

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

#define I 5
#define J 5
#define P 2
int i,j,k;  //for loops

int main ()
{
    ifstream inFile;
    ofstream outFile;

    double C[I][J];
    double u[I][J];
    double UB = 0;

    outFile.open("results.txt");
    // READ U0
    inFile.open("u.txt", ios::in);
    if (! inFile) {
        cerr << "unable to open file u.txt for reading" << endl;
        return 1;
    }
    for(i = 0; i < I; i++)
        for(j = 0; j < J; j++)
            inFile >> u[i][j];

    outFile << "u" << endl;
    for(i = 0; i < I; i++)
    {
        for(j = 0; j < J; j++)
            outFile << u[i][j];
            outFile << endl;
    }
    outFile << "Hi";

    //READ C
    inFile.open("C.txt", ios::in);
    if (! inFile) {
        cerr << "unable to open file C.txt for reading" << endl;
        return 1;
    }
    for(i = 0; i < I; i++)
        for(j = 0; j < J; j++)
            inFile >> C[i][j];
    outFile << "C" << endl;

    outFile << "UB=" << UB;


    inFile.close();
    outFile.close();

    return 0;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T12:04:21+00:00Added an answer on May 30, 2026 at 12:04 pm

    You have two general choices:

    • Use two different variables, such as inFile1 and inFile2, for u.txt and C.txt.
    • Close inFile with inFile.close() before trying to use the same variable to open a new file.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read from two files, and generate output in a third. I
I want to read a folder for all the files and programmatically print them
I want to read .ppt files from the iPhone mail app. I have already
which one of the two is more spread? I want to read out the
i want to read two records each time from select query results in PHP
I want to read two properties from a worker thread. Here it is an
I want to read each line from a text file and store them in
In my C++ code, I want to read from a text file (*.txt) and
I have a bash script as below, and I want it to read two
I have a specific object with image data. And I want read/write images (in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.