Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Loading arrays from text file...again :P

Posted on 2003-10-29
7
Medium Priority
?
283 Views
Last Modified: 2010-04-02
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;
}
0
Comment
Question by:w00t
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
7 Comments
 

Author Comment

by:w00t
ID: 9647190
that period around line 19 shouldn't be there :P
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9647260
>> 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
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9647348
Oops, strike that, I didn't notice that textures had multiple dimensions...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Exceter
ID: 9647363
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
0
 
LVL 8

Accepted Solution

by:
Exceter earned 300 total points
ID: 9647391
Grrr....

>> char* matrix[15][20];

Okay, I'm getting annoyed with myself now... This is an array of char* pointers and not chars...

>>      if(matrix[0][0] == "9"){        //test matrix[0][0] to see if to equals "9"
>>        printf("%s", textures[0]);    //if it does then printf

Understand that this comparison is still wrong, I simply stated the wrong reason for this. Both matrix[0][0] and "9" are char* pointers to a string. However, they can point to different arrays that contain the same data. Therefore, the pointer value of "9" is not equal to the pointer value for matrix[0][0] and the comparison fails. The actual data in the arrays is never compared. You need to use the strcmp function for this task, for example,

if( strcmp( matrix[0][0], "9" ) == 0 ) // If the data in the strings are equal, print textures[0]
   printf("%s", textures[0]);

I should have read your question more carefully from the beginning. My apologies.

Cheers!
Exceter
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10546306
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
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question