2014-04-19 12 views
5

Hier ist der Code, den ich bisher habe.C++ Lesen von einer Textdatei in ein Array/String

Was muss ich aus zwei verschiedenen Textdateien, Matrix A und Matrix B tun lesen

ich dies jedoch für jede Textdatei Matrix tun kann ich es nur mit

1 0 0 
aufkommt lesen

(also im Grunde die erste Zeile), wo die ganze Textdatei für Matrix A in der Tat ist

1 0 0 
2 0 0 
3 0 0 

so weiß es jemand, wie ich das tun kann?

Danke!

#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; 
} 
+2

eine Schleife verwenden, vielleicht? –

Antwort

9

Nun, getline bekommt offensichtlich eine Zeile.

Sie sollten Zeile für Zeile bis zum Ende der Datei lesen, und Sie können das erreichen mit zum Beispiel:

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

Das bedeutet: Während es eine Linie von Meinedat zu bekommen, schreiben Sie diese Zeile zu der Ausgabestrom.

+0

Held! Vielen Dank – user3536870

+0

Gern geschehen, viel Glück. :) – Eutherpy

2

Sie lesen nur einmal, also ist dies kein Wunder. Sie müssen eine while- oder for-Schleife für kontinuierliches Lesen verwenden. Sie würden so etwas wie dieses schreiben:

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

Dies wäre der gesamte Code zu schreiben:

#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; 
} 
+0

Ich bin im wörtlichen Sinne ein Anfänger und kämpfe sehr, das bedeutet leider nicht viel zu meinem kleinen Wissen. Es würde viel mehr helfen, wenn Sie mir möglicherweise zeigen könnten, wie ich meinen Code ändern könnte? – user3536870

+0

Was für ein Held! Vielen Dank Alter ! – user3536870

+0

@ user3536870: Prost, aber bitte ein C++ Buch, z. [Bjarne] (http://www.stroustrup.com/4th.html). Lesen Sie auch [this] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – lpapp

0

Mit getline ist die einfachste Art und Weise:

#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; 
}