0

All of this happens in my single loop. I create temporary variables for X, Y, and 3 strings.

The text file has 320 lines, which look like this:

1   2   "string"   "stringy"   "stringed"

int1 int2 string1 string2 string3

int1 int2 string1 string2 string3

int1 int2 string1 string2 string3

int1 int2 string1 string2 string3

And the loop code is here:

for(int Y = 0;Y < 320 ;Y++)
{

    int tempX;
    int tempY;
    char tempRegionName;
    char tempTXTfile;
    char tempIMGfile;

    fscanf(FileHandle, "%d %d %s %s %s ", &tempX, &tempY, &tempRegionName, &tempTXTfile, &tempIMGfile);

    cout<<"X: "<<tempX<<" Y: "<<tempY<<" Name: "<<tempRegionName<<" TXT: "<< tempTXTfile << " IMG: " << tempIMGfile << endl;

}

When I debug, let's say the line it reads is this :

1   2   "string"   "stringy"   "stringed"

It then does this.

tempX = 1

tempY = 2 (tempX is now 0)

tempRegionName = "string" (tempY is now 0)

tempTXTfile = "stringy" (tempReginoName is now null)

tempIMGfile = "stringed" (tempTXTfile is now null).

Then it outputs this:

 X: 1   Y: 0    NAME:    TXT:    IMG: stringed

I am not understanding this. I tried to follow the examples I found on using fscanf, and another example of code works using %d:%d. I tried replacing the spaces with :, but it obviously isn't the white space.

Looking it up on cplusplus, I am having a bit difficulty understanding. Maybe I'm just tired, but what am I doing wrong?

1 Answers1

1

The buffers aren't large enough to hold the strings, i.e. char is only one byte. You should declare the variables as arrays of characters. For example, try this instead:

for(int Y = 0;Y < 320 ;Y++)
{
    int tempX;
    int tempY;
    char tempRegionName[64];
    char tempTXTfile[64];
    char tempIMGfile[64];

But be careful with %s and the size of your target buffers. It is easy to write 'out of bounds'.

fredvanl
  • 193
  • 1
  • 9
  • Thank you. I never would have guessed it was the buffer size. – user2903351 Oct 21 '13 at 23:41
  • I am grateful, this both fixed it and gave me an answer that it would have took me days to figure out without your help. This saved me such a big headache. I wasn't even close to figuring out the problem, lol. – user2903351 Oct 21 '13 at 23:47