Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Adding strings together using strcat

Author
Message
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 17th Oct 2008 23:42
Hey guys, I'm trying to add several strings together before loading a file with the result as the filename. I searched the forums and learned that a way to do it is by using strcat.

I used this code:



Note that for now LevelNumber is constant because I'm just testing the function, in the future I'll replace it with dbStr(CurrentLevel) or something.

Anyway, when I run this code, the program crashes. It just freezes completely, I have to force quit visual c++. Any ideas what I'm doing wrong?

Thanks

prasoc
15
Years of Service
User Offline
Joined: 8th Oct 2008
Location:
Posted: 18th Oct 2008 00:07
Are you sure Level1.txt exists? maybe it is Level01.txt?
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 18th Oct 2008 00:21 Edited at: 18th Oct 2008 00:36
Update, It no longer crashes, but the text output is all distorted.

It's supposed to output Level1.txt, but instead I get Level1.t0Y, where 0 is a white square, and Y is in a weird font.

My new code:


Edited:
Nevermind! It works now, I realized that I wasn't specifying the size of the chars, I changed the size to 256 and it works now ^^.

Second edit:
So now that I have that, I want to replace the "1.txt" with a variable value.

I have an integer value for CurrentLevel that I want to replace the 1 with, but I want to keep the ".txt" constant.
How would I accomplish the DBP equivalent of "Level" + Str$(1) + ".txt"?

Thanks

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 18th Oct 2008 02:23
When you declare

char destination [] = "Level";

you're reserving only enough space to contain the characters in the string and the terminating zero. When you concatenate more text to it, it's being stored in unknown territory and probably is banging into other stored data.

Try

char destination [128] = "Level";

That will reserve enough space for most added text.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 18th Oct 2008 23:48
Aha ^^, that worked. Thanks much!

AlexI
19
Years of Service
User Offline
Joined: 31st Dec 2004
Location: UK
Posted: 19th Oct 2008 02:28
Quote: "but the text output is all distorted."


try strcpy(varible,""); before you use it

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France

Login to post a reply

Server time is: 2024-09-30 09:37:01
Your offset time is: 2024-09-30 09:37:01