-
sscanf causing problems
Hello,
I am trying to use sscanf in a program. It compiles fine, but it is causing the run to hang up everytime.
This is the code that does it. If I comment it out, the program runs fine without it, just doesn't function, of course.
Code:
sscanf((chrString),"%c%c%c",
&gObj->vertices[count].xCoord,
&gObj->vertices[count].yCoord,
&gObj->vertices[count].zCoord);
Any Idea why this would be?
Stephen
-
Code:
scanf((chrString),"%c%c%c",
gObj->vertices[count].xCoord,
&gObj->vertices[count].yCoord,
&gObj->vertices[count].zCoord);
Missing an ampersand?
-
Caught that just after posting it. Edited in original post. It didn't fix the problem.
-
Okay, then you'll have to be more specific.
>but it is causing the run to hang up everytime
Describe this more fully. Does the input request never seem to return? Is there a runtime error? Can you post a complete example program that exhibits the problem?
-
What happens is the program runs until it hits this line, then stops running. Windows throws an error, and then the console window stops responding. I've put ouput lines directly after this one, and they never execute.
-
Are x, y, and z coords characters, integers, floats, doubles or what? What doyou enter to stop it working?
-
>Windows throws an error
Okay, now we're getting somewhere. That's probably an access violation. First, print out the chrString string and post the contents here. Then show us how Obj and any of it's dependent types are defined.
-
cout << chrString; yeilds -10 30 0
Code:
struct vertex //Defines a linked list node to hold a vertex
{
int xCoord,
yCoord,
zCoord;
vertex *next;
};
vertex *start_ptr = NULL;
struct Group
{
vertex *vertices;
Group *next;
};
This appears in the method where the sscanf call is: Group *gObj=NULL;
-
>cout << chrString; yeilds -10 30 0
Okay, "%c%c%c" is the incorrect format string. You want "%d%d%d".
>Group *gObj=NULL;
That's probably your problem. gObj probably doesn't refer to anything before you try to read data into it. Well, it does refer to something, but you can't predict it and 9 times out of 10 writing to it will break something. Are you doing any memory management with these uninitialized pointers?
> int xCoord,
>yCoord,
>zCoord;
That's dangerous. When you tell sscanf you're giving it a char, you'd be wise to give it a char and only a char. Since these coords are integers, you need to use %d instead of %c.
>vertex *vertices;
This likely has the same problem as gObj.
-
OK, I fixed the %d thing. I just wasn't paying attention there, I had copied that from another program.
Since I have not used C++ in a few years, and then only a bit, I am unsure of how I need to proceed with the pointers. I have used Java extensively, but am having to use C++ for a class. So, I am trying to teach it to myself and use it at the same time.
Basically, I am trying to create a program that loads an *.obj file and renders it using openGL. For my class, I am writing the loader myself, but have used a prewritten one as a guide. It used a linked list to hold vertices, normals, etc. I am trying to do the same (after a failed attempt at using nested vectors).
Thanks so much for your patient help.
-
Why are you using sscanf in a C++ program, instead of >> ?