Search code examples
c++fileparsingcrop

How to get variables to my program vrom text file


I have a file like:


  • a [able%5:00:00:capable:00] [able] capable
  • a [abnormal%3:00:00::] [abnormal]
  • a [absent%3:00:00::] [absent]
  • a [absolute%3:00:00::] [absolute] perfect or complete
  • a [abstract%3:00:00::] [abstract] existing only in the mind
  • a [abundant%3:00:00::] [abundant] plentiful

I want to get first column "avle", "abnormal" etc to my object. How to crop them and how storage them?


Solution

  • Use the string tokenizer, the best/easiest way to split a line into different variables.

    char* word = strtok(line," [%:]");
    char* word2 = strtok(0," [%:]");
    int value = strtoi(strtok(0," [%:]"));
    

    to store there is a vector container, but there can be used any type of arrays, what is more convenient