I think you are ignoring what happens in the switch.
float xCount = 0;
float yCount = 0;
float zCount = 0;
switch( szBuf[i] )
{
/* ... */
}
xCount = g_pVertices[index].x;
yCount = g_pVertices[index].y;
zCount = g_pVertices[index].z;
Before theswitch, you initialize them all to zero. After the switch, you discard the calculated values and assign new values.
Is this something similar to what you are trying to do?
#include <stdio.h>
#include <string.h>
int main()
{
char szBuf[256] = "003124";
int i, index = 0, nLineLen = strlen(szBuf);
float xCount = 0, yCount = 0, zCount = 0;
for ( i = 0; i < nLineLen; i++ )
{
switch ( szBuf[i] )
{
default: break;
case '0': break;
case '1': xCount++; break;
case '2': yCount++; break;
case '3': xCount++; yCount++; break;
case '4': zCount++; break;
case '5': xCount++; zCount++; break;
case '6': yCount++; zCount++; break;
case '7': xCount++; yCount++; zCount++; break;
}
//xCount = g_pVertices[index].x;
//yCount = g_pVertices[index].y;
//zCount = g_pVertices[index].z;
index++;
printf("xCount = %g, yCount = %g, zCount = %g\n", xCount, yCount, zCount);
}
return 0;
}
/* my output
xCount = 0, yCount = 0, zCount = 0
xCount = 0, yCount = 0, zCount = 0
xCount = 1, yCount = 1, zCount = 0
xCount = 2, yCount = 1, zCount = 0
xCount = 2, yCount = 2, zCount = 0
xCount = 2, yCount = 2, zCount = 1
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>but i need to scan each line and extract 3 values from each.
If this is because I used hard-coded text, that is because I didn't feel like reading your mind to get the contents of the text file, and doing the file handling you didn't post.
However you get the line of text, there is text. The part I was concentrating on was the handling of this text, however it got there. I'll try to add color to my previous post.
Either that, or I'm just confused.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Does this help?
#include <stdio.h>
#include <string.h>
int main()
{
char *szBuf[] = {"00111","003124"};
struct vertex { int x, y, z; } v [ sizeof szBuf / sizeof *szBuf ] = {0};
size_t i, j, nLineLen;
for ( j = 0; j < sizeof szBuf / sizeof *szBuf; ++j )
{
for ( i = 0, nLineLen = strlen(szBuf[j]); i < nLineLen; i++ )
{
switch ( szBuf[j][i] )
{
default: break;
case '0': break;
case '1': v[j].x++; break;
case '2': v[j].y++; break;
case '3': v[j].x++; v[j].y++; break;
case '4': v[j].z++; break;
case '5': v[j].x++; v[j].z++; break;
case '6': v[j].y++; v[j].z++; break;
case '7': v[j].x++; v[j].y++; v[j].z++; break;
}
}
printf("%s: v[j].x = %d, v[j].y = %d, v[j].z = %d\n",
szBuf[j], v[j].x, v[j].y, v[j].z);
}
return 0;
}
/* my output
00111: v[j].x = 3, v[j].y = 0, v[j].z = 0
003124: v[j].x = 2, v[j].y = 2, v[j].z = 1
*/
[edit]C code with some file handling:
/* file.txt
00111
003124
*/
#include <stdio.h>
#include <string.h>
int main(void)
{
static const char filename[] = "file.txt";
FILE *file = fopen(filename, "r");
if ( file != NULL )
{
struct vertex { int x, y, z; } v [ 5 ] = {0};
size_t i, j, len;
char line[256];
for ( j = 0; j < sizeof v / sizeof *v ; ++j )
{
if ( !fgets(line, sizeof line, file) )
{
break;
}
for ( i = 0, len = strlen(line); i < len; i++ )
{
switch ( line[i] )
{
default: break;
case '0': break;
case '1': v[j].x++; break;
case '2': v[j].y++; break;
case '3': v[j].x++; v[j].y++; break;
case '4': v[j].z++; break;
case '5': v[j].x++; v[j].z++; break;
case '6': v[j].y++; v[j].z++; break;
case '7': v[j].x++; v[j].y++; v[j].z++; break;
}
}
printf("%s: v[j].x = %d, v[j].y = %d, v[j].z = %d\n",
line, v[j].x, v[j].y, v[j].z);
}
}
return 0;
}
/* my output
00111: v[j].x = 3, v[j].y = 0, v[j].z = 0
003124: v[j].x = 2, v[j].y = 2, v[j].z = 1
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314