Read from a file C++

前端 未结 3 1344
猫巷女王i
猫巷女王i 2021-01-07 10:40

Hi I want to read data from a VTK file into my C++ program. Here is how my files will typically look.

POINTS 2 double

1 2

3条回答
  •  盖世英雄少女心
    2021-01-07 11:19

    You could try some code that looks like this. It's kind of a combo of C and C++:

    int main ()
    {
      //assuming that you've completely read in the file to a char* called myFileContents
      char * token;
      token = strtok (myFileContents," ");
      while (token != NULL)
      {
        istringstream iss( token );
        int readInData;
        readInData << iss;
        if(!iss){
          //this means that the data wasn't numeric so don't do anything with it
        }
        else{
          //data was numeric, store it how ever you want
        }
        token = strtok (NULL, " ");
      }
      return 0;
    }
    

提交回复
热议问题