C++: Read from text file and separate into variable

前端 未结 3 1123
遇见更好的自我
遇见更好的自我 2020-12-03 15:10

I have this in a text file:

John 20 30 40
mike 30 20 10

How do i read from the text file and separate them into variable name, var1, var2,

3条回答
  •  死守一世寂寞
    2020-12-03 15:45

    It looks like you need to declare var1, var2, and var3.

    Also instead of this:

          getline (myfile,name,'\t');
          getline (myfile,var1,'\t');
          getline (myfile,var2,'\t');
          getline (myfile,var3,'\t');
    

    Try this:

      myfile >> name;
      myfile >> var1;
      myfile >> var2;
      myfile >> var3;
    

    Not because what you had is wrong, but the second is cleaner and will handle all white space.

提交回复
热议问题