注册 登录
编程论坛 C++教室

如何将txt文件里的东西存放到字符串组里?

复旦 发布于 2018-11-12 01:47, 1086 次点击
我用的是Linux C++。 用vim编辑器。
如何将txt文件里的东西存放到字符串组里面?
一个字符串应包含一个行的内容。

谢谢!
2 回复
#2
rjsp2018-11-12 09:05
没明白你想要什么,难道不就是一行一行地读?

程序代码:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main( void )
{
    vector<string> ss;
    {
        ifstream file( "a.txt" );
        for( string s; getline(file,s); )
            ss.push_back( s ); // 若嫌效率不高,那 move(s)
    }

    return 0;
}

#3
复旦2018-11-12 16:20
谢谢!成功运行。
1