-1

I am using the function fscanf_s to make multiple strings from a string that I read from an .obj file. Perhaps I am misusing it i don´t know but I´ve tried with fscanf_s(file, "%s %s %s\n", &temp, &temp1, &temp2).

Here is an example: f 1//1 14//1 13//1

What I want to do is to store 1//1, 14//1 and 13//1 into 3 separate strings.

I don´t really care for the fastest way to do it but it would be preferd obviously.

EDIT For some reason ifstream >> operator didn´t work correctly in my project. I found a solution to my own problem though.

char s1[10];
char s2[10];
char s3[10];

int matches = fscanf_s(file, "%s %s %s", s1, sizeof(s1), s2, sizeof(s2), s3, sizeof(s3));

And then using the chars in string constructors.

1
  • 7
    Since these are space-separated items, just use a std::ifstream and the >> operator into a std::string variable. Feb 25, 2015 at 17:20

1 Answer 1

0

When using c++, I strongly recommend using the c++ functions as well :) Try something like this for parsing your file:

std::ifstream file ("filename");
while (file)
{
  std::string token;
  file >> token;
  // process your token...
}

the >> operator extracts the next sequence of non-whitespace characters from an input stream (have a look: http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.