【问题标题】:C++ reading from a text file into a array/stringC++ 从文本文件读取到数组/字符串
【发布时间】:2014-04-19 15:55:10
【问题描述】:

这是我目前的代码。

我需要做的是从两个不同的文本文件中读取,矩阵 A 和矩阵 B。

我可以这样做,但是对于我读取的每个文本文件矩阵,它只提供了

1 0 0 

(所以基本上是第一行)Matrix A 的整个文本文件实际上是

1 0 0
2 0 0
3 0 0

那么有人知道我该怎么做吗?

谢谢!

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

【问题讨论】:

    标签: c++ arrays string matrix


    【解决方案1】:

    嗯,getline 显然得到了一行。

    您应该逐行阅读,直到文件末尾,您可以通过以下方式实现:

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

    这意味着:当从 myfile 中获取一行时,将该行写入输出流。

    【讨论】:

    • 英雄!非常感谢
    【解决方案2】:

    您只阅读一次,所以这不是奇迹。您将需要使用 while 或 for 循环进行连续阅读。你会这样写:

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

    这就是要编写的全部代码:

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

    【讨论】:

    • 我确实是这方面的初学者,并且一直在苦苦挣扎,所以不幸的是,这对我的小知识来说意义不大。如果你能告诉我如何修改我的代码,那会更有帮助?
    • @user3536870:干杯,但请买一本 C++ 书,例如Bjarne's。另外,请阅读this
    【解决方案3】:

    使用getline 是最简单的方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 2016-01-15
      相关资源
      最近更新 更多