5

Here is the code I have so far.

What I need to do is read from two different text files, Matrix A and Matrix B.

I can do this however for each text file matrix I read it only comes up with

1 0 0 

(so basically the first line) where the whole text file for Matrix A is in fact

1 0 0
2 0 0
3 0 0

so does anybody know how I can do this?

Thanks!

#include <iostream>  //declaring variables
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;
string code(string& line);
int main()
{
    ofstream outf;
    ifstream myfile;
    string infile;
    string line;
    string outfile;

    cout << "Please enter an input file (A.txt) for Matrix A or (B.txt) for Matrix B" << endl;
    cin >> infile;   //prompts user for input file

    if (infile == "A.txt")
    {      //read whats in it and write to screen
        myfile.open("A.txt");
        cout << endl;
        getline (myfile, line);
        cout << line << endl;

    }
    else
        if (infile == "B.txt")
        {
            myfile.open("B.txt");
            cout << endl;
            getline (myfile, line);
            cout << line << endl;
        }
        else
    { 
        cout << "Unable to open file." << endl;
    }
        //{
            //while("Choose next operation");
        //}
    return 0;
}
0

3 Answers 3

9

Well, getline obviously gets one line.

You should read line by line until the end of file, and you can achieve that with, for example:

while (getline(myfile, line))
    out << line << endl;

This means: while there is a line to get from myfile, write that line to the output stream.

Sign up to request clarification or add additional context in comments.

Comments

2

You are reading only once, so this is not a miracle. You will need to use a while or for loop for continous reading. You would be writing something like this:

while (getline (myfile, line))
    cout << line << endl;

This would be the whole code to write:

#include <iostream>  //declaring variables
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;
string code(string& line);
int main()
{
    ofstream outf;
    ifstream myfile;
    string infile;
    string line;
    string outfile;

    cout << "Please enter an input file (A.txt) for Matrix A or (B.txt) for Matrix B" << endl;
    cin >> infile;   //prompts user for input file

    if (infile == "A.txt")
    {      //read whats in it and write to screen
        myfile.open("A.txt");
        cout << endl;
        while (getline (myfile, line))
            cout << line << endl;


    }
    else
        if (infile == "B.txt")
        {
            myfile.open("B.txt");
            cout << endl;
            while (getline (myfile, line))
                cout << line << endl;
        }
        else
    { 
        cout << "Unable to open file." << endl;
    }
        //{
            //while("Choose next operation");
        //}
    return 0;
}

2 Comments

I'm literally a beginner to this and struggling a lot so that doesn't mean a great deal to my little knowledge unfortunately. It would help a lot more if you could possibly show me how I could modify my code?
@user3536870: cheers, but please get a C++ book, e.g. Bjarne's. Also, please read this.
0

Using getline is the easiest way:

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

void read_file_line_by_line(){
    ifstream file;
    string line;
    file.open("path_to_file");
    while (getline (file, line))
        cout << line << endl;
}

int main(){
    read_file_line_by_line();
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.