How to read numbers from an ASCII file (C++)

后端 未结 4 1854
梦如初夏
梦如初夏 2021-01-31 19:48

I need to read in data files which look like this:

* SZA: 10.00
 2.648  2.648  2.648  2.648  2.648  2.648  2.648  2.649  2.650  2.650
 2.652  2.653  2.652  2.653         


        
4条回答
  •  感动是毒
    2021-01-31 20:44

    The String Toolkit Library (Strtk) has the following solution to your problem:

    #include 
    #include 
    #include 
    #include 
    
    #include "strtk.hpp"
    
    int main()
    {
        std::deque flist;
        strtk::for_each_line("file.txt",
                             [&flist](const std::string& line)
                             { strtk::parse(line," ",flist); }
                             );
        std::copy(flist.begin(),flist.end(),
                  std::ostream_iterator(std::cout,"\t"));
        return 0;
    }
    

    More examples can be found in C++ String Toolkit (StrTk) Tokenizer.

提交回复
热议问题