C++ 从文本文件读取到数组/字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23172166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:21:16  来源:igfitidea点击:

C++ reading from a text file into a array/string

c++arraysstringmatrix

提问by user3536870

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.

我需要做的是从两个不同的文本文件中读取,矩阵 A 和矩阵 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

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

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

回答by Eutherpy

Well, getlineobviously gets one line.

好吧,getline显然得到了一条线。

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.

这意味着:虽然有一行要从 myfile 中获取,但将该行写入输出流。

回答by lpapp

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 或 for 循环来连续阅读。你会写这样的东西:

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

回答by madx

Using getlineis the easiest way:

使用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;
}