C++ 将文本文件读入c++;

C++ 将文本文件读入c++;,c++,file-io,stdvector,C++,File Io,Stdvector,我有一个文本文件,其中有几行文本 我想把文本放到2D向量中,因为我 需要能够分别调用每个字符[x][y] 这就是我得到的: 我在迷宫[i][j]上出错“\0” intmain(){ //变量声明 流文件; int i=0; 向量迷宫(1,向量(1)); ifstreammyreadfile; myReadFile.open(“input.txt”); 而(!myReadFile.eof()){ 对于(int j=0;迷宫[i][j]!=“\0”;j++){ myReadFile>>迷宫[i][j

我有一个文本文件,其中有几行文本

我想把文本放到2D向量中,因为我 需要能够分别调用每个字符[x][y]

这就是我得到的: 我在迷宫[i][j]上出错“\0”

intmain(){
//变量声明
流文件;
int i=0;
向量迷宫(1,向量(1));
ifstreammyreadfile;
myReadFile.open(“input.txt”);
而(!myReadFile.eof()){
对于(int j=0;迷宫[i][j]!=“\0”;j++){
myReadFile>>迷宫[i][j];
}
i++;
}
file.close();
对于(int i=0;i我可能会看到一些错误

  • 您将向量定义为大小为1,但尝试使用直接索引添加更多元素。这会导致异常。您应使用push_back添加更多元素

  • 如果要检查行尾,字符为“\r”和“\n”

  • 如果要比较字符,请使用“\0”而不是“\0”
  • 下面是建议的代码:

    int main() {
    // Variable declarations
    fstream file;
    vector<vector<char> > maze;
    
    ifstream myReadFile;
    myReadFile.open("input.txt");
    vector <char> line;
    char c;
    while (!myReadFile.eof()) {
        myReadFile.get(c);
        if (c == '\r') 
        {
            myReadFile.get(c);
            if (c == '\n')
            {
                if (line.size() > 0)
                    maze.push_back(line);
                line.clear();
                continue;
            }
        }
        line.push_back(c);
    
    }
    
    file.close();
    for (int i = 0; i < maze.size(); i++)
    {
        for (int j = 0; j < maze[i].size(); j++)
        {
            cout << maze[i][j];
        }
    }
        return 0;
    }
    
    intmain(){
    //变量声明
    流文件;
    矢量迷宫;
    ifstreammyreadfile;
    myReadFile.open(“input.txt”);
    矢量线;
    字符c;
    而(!myReadFile.eof()){
    get(c);
    如果(c=='\r')
    {
    get(c);
    如果(c=='\n')
    {
    如果(line.size()>0)
    迷宫。推回(直线);
    line.clear();
    继续;
    }
    }
    线。推回(c);
    }
    file.close();
    对于(int i=0;iC99和C99有什么关系?你是说C++ 98吗?“在C99”中,但是标记了??你的具体问题是什么?SRY已经很累了。我想我是C++和错拼了:/你也可以使用<代码> GETLILE()/Cuth>,但是你需要预先定义一个固定的空间缓冲区。如果大小不够,可能会有潜在的问题。
    
    #include <fstream>
    #include <stdio.h>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    template <typename T> //T could be of any data type
    void printVector(T dvalue){//print the input data
     cout<<"List of the stored values "<<endl;
     for(int i=0; i<dvalue.size(); i++){
     for(int j=0; j<dvalue[i].size(); j++){
     cout<<dvalue[i][j]<<" ";
     }
     cout<<endl;
     }
    }
    
    int main(int argc, char* argv[]){
            cout<<"This example utilizes multi-dimensional vectors to read an input data file!"<<endl;
           vector< vector<string> > dvalue; //multidimensional vector declaration
            string line;
            fstream myfile; //define a myfile object of fstream class type
            myfile.open("input.txt"); //open the file passed through the main function
    
    //read 1 line at a time.If the data points are seperated by comma it treats as a new line
            while(getline(myfile,line,'\n')){
                    vector <string> v1;
                    istringstream iss(line);//read the first line and store it in iss
                    string value;
    //this line breaks the row of data stored in iss into several columns and stores in the v1 vector
                    while(iss>>value){
                            v1.push_back(value);
                    }
                    dvalue.push_back(v1);//store the input data row by row
            }
            printVector(dvalue);
    return 0;
    } 
    
    int main() {
    // Variable declarations
    fstream file;
    vector<vector<char> > maze;
    
    ifstream myReadFile;
    myReadFile.open("input.txt");
    vector <char> line;
    char c;
    while (!myReadFile.eof()) {
        myReadFile.get(c);
        if (c == '\r') 
        {
            myReadFile.get(c);
            if (c == '\n')
            {
                if (line.size() > 0)
                    maze.push_back(line);
                line.clear();
                continue;
            }
        }
        line.push_back(c);
    
    }
    
    file.close();
    for (int i = 0; i < maze.size(); i++)
    {
        for (int j = 0; j < maze[i].size(); j++)
        {
            cout << maze[i][j];
        }
    }
        return 0;
    }