How To Parse String File Txt Into Array With C++

前端 未结 3 1467
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-03 09:29

I am trying to write a C++ program, but I am not familiar with C++. I have a .txt file, which contains values as follows:

0
0.0146484
0.0292969
         


        
3条回答
  •  旧巷少年郎
    2021-01-03 09:38

    First you'll need a vector:

    std::vector lines; // requires #include 
    

    Then you'll need to take a string taken from the getline operation, and push it back into the vector. It's very simple:

    for (std::string line; std::getline(myReadFile, line);) 
    {
        lines.push_back(line);
    }
    

    For an output operation, all you need is:

    {
        int i = 0;
    
        for (auto a : lines)
        {
            std::cout << "lines[" << i++ << "] = " << a << std::endl;
        }
    }
    

提交回复
热议问题