w00t
asked on
Loading arrays from text file...again :P
I asked a very similar qustion earlier here and got the answer I needed,
but now my program has changed and I'm running into some small problems.
I can load the values from the file into the 2d array perfectly, but when I try to
run an if to test the values i get screwed. This code should printf("%s", textures[0]); but
prints nothing. the first value of map.txt is 9, i.e.
cat map.txt
9 0 0 1 1 3 3 4 5 6 7
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main () {
FILE * pFile;
long lSize;
char * buffer;
pFile = fopen ("map.txt" , "rb" );
if (pFile==NULL) exit (1);
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
.
buffer = (char*) malloc (lSize);
if (buffer == NULL) exit (2);
fread (buffer,1, lSize,pFile);
char * pch;
pch = strtok (buffer," ");
int x = 0;
int y = 0;
char* matrix[15][20];
while (pch != NULL)
{
if(y <= 19)
{
matrix[x][y] = pch;
pch = strtok (NULL, " ,.");
y++;
}
else
{
y = 0;
x++;
}
}
/* ========================== ========== ========== ========== ========== ====*/
char *textures[] = {".\\images\\stone.bmp", ".\\images\\woodtile.bmp"} ;
for(int row = 0; row<15; row++){
for(int col = 0; col<20; col++){
if(matrix[0][0] == "9"){ //test matrix[0][0] to see if to equals "9"
printf("%s", textures[0]); //if it does then printf
}
}
}
/* ========================== ========== ========== ========== ========== ====*/
fclose (pFile);
free (buffer);
return 0;
}
but now my program has changed and I'm running into some small problems.
I can load the values from the file into the 2d array perfectly, but when I try to
run an if to test the values i get screwed. This code should printf("%s", textures[0]); but
prints nothing. the first value of map.txt is 9, i.e.
cat map.txt
9 0 0 1 1 3 3 4 5 6 7
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main () {
FILE * pFile;
long lSize;
char * buffer;
pFile = fopen ("map.txt" , "rb" );
if (pFile==NULL) exit (1);
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
.
buffer = (char*) malloc (lSize);
if (buffer == NULL) exit (2);
fread (buffer,1, lSize,pFile);
char * pch;
pch = strtok (buffer," ");
int x = 0;
int y = 0;
char* matrix[15][20];
while (pch != NULL)
{
if(y <= 19)
{
matrix[x][y] = pch;
pch = strtok (NULL, " ,.");
y++;
}
else
{
y = 0;
x++;
}
}
/* ==========================
char *textures[] = {".\\images\\stone.bmp", ".\\images\\woodtile.bmp"}
for(int row = 0; row<15; row++){
for(int col = 0; col<20; col++){
if(matrix[0][0] == "9"){ //test matrix[0][0] to see if to equals "9"
printf("%s", textures[0]); //if it does then printf
}
}
}
/* ==========================
fclose (pFile);
free (buffer);
return 0;
}
>> char *textures[] = {".\\images\\stone.bmp", ".\\images\\woodtile.bmp"} ;
>>
>> ...
>>
>> printf("%s", textures[0]); //if it does then printf
textures is a char array. Its type is char*. textures[0] is a char, not a char*. You are attempting to display a string using the first char in the array as the pointer to the array. You could do this by saying,
printf("%s", &textures[0]);
but it is easiuer to simply say,
printf("%s", textures);
Exceter
>>
>> ...
>>
>> printf("%s", textures[0]); //if it does then printf
textures is a char array. Its type is char*. textures[0] is a char, not a char*. You are attempting to display a string using the first char in the array as the pointer to the array. You could do this by saying,
printf("%s", &textures[0]);
but it is easiuer to simply say,
printf("%s", textures);
Exceter
Oops, strike that, I didn't notice that textures had multiple dimensions...
Here's the actual culprit, unless the sleepy seed demon is still haunting me,
>> char* matrix[15][20];
...
>> if(matrix[0][0] == "9"){ //test matrix[0][0] to see if to equals "9"
>> printf("%s", textures[0]); //if it does then printf
matrix[0][0] is a char. "9" is a null terminated string containing the character 9 and a terminating null byte. Therefore its type is char*, not char. Replacing the double quotes with single quotes tells the compiler that this is a single character and not a string. Therefore, this should read,
if(matrix[0][0] == '9')
printf("%s", textures[0]);
Cheers!
Exceter
>> char* matrix[15][20];
...
>> if(matrix[0][0] == "9"){ //test matrix[0][0] to see if to equals "9"
>> printf("%s", textures[0]); //if it does then printf
matrix[0][0] is a char. "9" is a null terminated string containing the character 9 and a terminating null byte. Therefore its type is char*, not char. Replacing the double quotes with single quotes tells the compiler that this is a single character and not a string. Therefore, this should read,
if(matrix[0][0] == '9')
printf("%s", textures[0]);
Cheers!
Exceter
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: Exceter {http:#9647391}
Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer
I will leave the following recommendation for this question in the Cleanup topic area:
Accept: Exceter {http:#9647391}
Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Tinchos
EE Cleanup Volunteer
ASKER