I'm hoping to get an answer that doesn't require changing my methods. I'm an electrical student, not software, so I just need to get this to work.
I'm reading a text file that has numerical information for x amount of elements, where each element has numerical data for 64 pixels as well as a number for an identifier.
I'm working towards loading all the data from this text document into 2 arrays. a float array for all the pixel data (each piece of data is 9 characters[example -0.608261], and an integer array for all the integer data (all data is 1 or -1).
my code is completely functioning apart from an issue with the elements of the float array containing extra digits when I load them from the character array. This is my method:
Declare a 2d character array, where the first dimension is the word and the second dimension is the character for each word:
1 2 3 4 5 6
|
// declare a 2d array of characters, where the first dimension holds each word and the second dimension holds each letter of each word. In this case
char** verification_string_array = new char* [char_count];
for (int i = 0; i < char_count; ++i)
{
verification_string_array[i] = new char[9];
}
|
Move the data from the initial 1d character array that holds the entire text file into the 2d array, so that each element of the first dimension holds one of the "words" (whether thats pixel information or an identifier isn't important).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
// store character array into string array, use spaces as indicater to index string array (each string array element holds 1 word)
for (int i = 0; i < file_size - 1; i++)
{
//if (the character isn't a space) AND (the character isn't a new line), add character to current word
if ((char_array[i] != ' ') && (char_array[i] != '\n'))
{
verification_string_array[k][m] = char_array[i];
m++;
}
//if (the character is a space) OR (the character is a newline and isn't preceeded by a space), start a new word
else if (char_array[i] == ' ' || (char_array[i] == '\n' && char_array[i - 1] != ' '))
{
k++;
m = 0;
}
}
|
now load the character array into the float array and integer array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
// set variables for upcoming iterations
j = 0;
k = 0;
m = 0;
for (int i = 3; i < char_count; i++)
{
if (k < 64)
{
sscanf(verification_string_array[i], "%f", &pixel_array[j]);
k++;
j++;
}
else
{
sscanf(verification_string_array[i], "%d", &identifier_array[m]);
k = 0;
m++;
}
}
|
my problem is, if we consider the first element of actual information (first 3 elements are header information) from the watch window:
verification_string_array[3] = 0x017ce268 "-0.693180yyyYYYc$"
all I wanted was the numbers. When I load this into the float array, I get this result:
pixel_array[0] = -0.693180025
which has extra digits
Can anyone offer some clarification as to what my issue is?