0

This is my first time writing a proper question for StackOverflow so if I am making mistakes please correct me!

I am trying to load in the v, vn and vt lines from an obj file. This code is in a while loop after getting the next line from the solution:

string type ("  ");
glm::vec3 vertex;

sscanf_s(line.c_str(), "%s %f %f %f\n",&type, &vertex.x, &vertex.y, &vertex.z);

The sscanf_s line throws these warnings:

Warning C6067   _Param_(3) in call to 'sscanf_s' must be the address of a string. Actual type: 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *'.

Warning C6270   Missing float argument to 'sscanf_s': add a float argument corresponding to conversion specifier '5'.

Warning C6273   Non-integer passed as _Param_(4) when an integer is required in call to 'sscanf_s' Actual type: 'float *':  if a pointer value is being passed, %p should be used.

I am confused as the three float values from the vector should surely throw the same error. When I run the code I also get an access violation writing location error on this line.

Please help me! I am new to c++!

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • You can't read into `std::string` objects like that. Why don't you use `std::istringstream` and plain `>>` stream input? – Some programmer dude Nov 18 '19 at 15:24
  • And you're not even using [`sscanf_s`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/sscanf-s-sscanf-s-l-swscanf-s-swscanf-s-l?view=vs-2019) correctly in the first place even without the `std::string` problem. – Some programmer dude Nov 18 '19 at 15:26
  • @Someprogrammerdude As said I am new to C++, I use C# and Java a lot more. I don't really understand the string streams and used sscanf following an example online – Luke Jansen Nov 18 '19 at 15:36
  • 1
    That example is probably for C not C++, or it was incomplete. Especially if it missed to tell you that `sscanf_s` needs an extra argument for `%s` specifier. I really recommend you take a few steps back, invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and learn how to use C++ and C++ streams properly. Take it step by step, learning as you go, and don't try to do to much to soon. – Some programmer dude Nov 18 '19 at 15:39
  • @Someprogrammerdude Thank you! I am on a time limit as I am learning this through a university module and it has not been taught well at all! But when I get the time I will make sure to start back at square 1 – Luke Jansen Nov 18 '19 at 15:47

1 Answers1

1

You can't read to a string object using sscanf_s, you must read to an array of chars.

char buffer[666];
glm::vec3 vertex;

sscanf_s(line.c_str(), "%s %f %f %f\n",buffer, sizeof(buffer), &vertex.x, &vertex.y, &vertex.z);
Jose Maria
  • 455
  • 2
  • 10