Jump to content


substr works sometimes, other times it doesn't????


5 replies to this topic

#1 gardon

    Valued Member

  • Members
  • PipPipPip
  • 282 posts

Posted 23 December 2007 - 05:54 PM

I'm extracting data from a .txt file. First I extract the name of the image I will be using, secondly it's filename, and thirdly all the other width, height, and transparency attributes.

Each entry is delimited by a comma.

Here's the actual entry in the file: guardian,guardian.png,0,0,85,75,... etc.

Here's my code to extract it:

int lastPlace = 0;
int delimiter = 0;

delimiter = line.find(",", lastPlace);

// entry is the label/string entry to update
entry = line.substr(lastPlace, delimiter);
lastPlace = delimiter + 1;

delimiter = line.find(",", lastPlace);

// now the fileName
// --- !!! Here is where it screws up !!! ---
imageFileName = line.substr(lastPlace, delimiter);
lastPlace = delimiter + 1;

delimiter = line.find(",", lastPlace);
		
// the starting X and Y positions
startX = atoi(line.substr(lastPlace, delimiter).c_str());
lastPlace = delimiter + 1;


... etc .. 


As you can see, it finds the next comma, and returns a substring from the last place it was. In this case, I set the int variable 'lastPlace' to 0 initially, and then to the beginning of the next entry each time there-after.

What happens is, 'guardian' is read in for the string 'entry'.
'guardian.png,0,0,85,7' is read in for the string 'imageFileName'
'0' is read in for the int startX,
'0' is read in for the int startY,
'85' is read in for imageWidth
'75' is read in for imageHeight

So the problem is that it's reading everything else correctly, but not the correct filename. What's even more strange, is that the comma delimiter for the second entry (imageFileName) is at position 21, and there are 21 characters in the 'imageFileName' variable that is input. See how's it's truncated? The numbers at the end of it are the following variables for startX, startY, imageWidth, and the beginning of imageHeight. Why is it doing that?

So it's almost as if the second read is finding the correct comma position for the delimiter, it's finding the right position for the lastPlace it was, but for some reason the substr method is returning the next 21 characters in sequence, rather than returning the string inbetween index 'lastPlace' and index 'delimiter' (which is 21).

Does this make any sense at all?

-Gardon

#2 gardon

    Valued Member

  • Members
  • PipPipPip
  • 282 posts

Posted 23 December 2007 - 06:06 PM

Update:

I manually went back and set the substr() method to the following:

imageFileName = substr(9, 21)

which is the direct positioning of the 'guardian.png' entry. It still gave me the same answer. It's like the substr() method is doing something completely opposite!

So I went back again and did this:

imageFileName = substr(9, 12)


Which doesn't make any sense at all, but it gave me 'guardian.png'. Also note that 'guardian.png' is 12 letters long.

So my conclusion is that substr will sometimes flip it's function to count the letters ahead of it, rather than doing what it's supposed to do.

What the fking hell is going on?

#3 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 23 December 2007 - 07:01 PM

Err, substr has ALWAYS been defined as taking the starting position and the COUNT, not the ending position.

http://msdn2.microso...9c6(VS.80).aspx
reedbeta.com - developer blog, OpenGL demos, and other projects

#4 gardon

    Valued Member

  • Members
  • PipPipPip
  • 282 posts

Posted 23 December 2007 - 07:50 PM

!!!!!!!!!

Perhaps when I do the 'atoi()' function it parses the readable data found first, then drops off the remaining, un-relevant data?

I've been using it my way for years and have never had a problem lol. This is the first time it's given me a problem. weird....

#5 STLDude

    Member

  • Members
  • PipPip
  • 59 posts

Posted 23 December 2007 - 09:33 PM

gardon said:

!!!!!!!!!
I've been using it my way for years and have never had a problem lol. This is the first time it's given me a problem. weird....

You just got lucky. First stop documentation.

#6 gardon

    Valued Member

  • Members
  • PipPipPip
  • 282 posts

Posted 24 December 2007 - 12:37 AM

lol. Normally I always check the docs, but when it's worked for so long I didn't think it was my fault.

Proven wrong, yet again :0 God damn...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users