How to copy a .txt file to a char array in c++

后端 未结 5 878
忘了有多久
忘了有多久 2020-11-27 17:26

Im trying to copy a whole .txt file into a char array. My code works but it leaves out the white spaces. So for example if my .txt file reads \"I Like Pie\" and i copy it to

5条回答
  •  时光取名叫无心
    2020-11-27 18:13

    Here is the code snippet you need:

    #include 
    #include 
    #include 
    #include 
    
    
    int main()
    {
      std::ifstream file("name.txt");
      std::string str((std::istreambuf_iterator(file)),
                            std::istreambuf_iterator());
    
      str.c_str();
    
      for( unsigned int a = 0; a < sizeof(str)/sizeof(str[0]); a = a + 1 )
      {
        std::cout << str[a] << std::endl;
      }
    
      return 0;
    }
    

提交回复
热议问题