#### Archived

This topic is now archived and is closed to further replies.

# A bit of trouble...

This topic is 5277 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone First of all, Id like to say that this has got to be the best place for any programmer to come to, everyone is really friendly, and I haven't seen a single post that had no helpful replies to it... I seriously love it here Yesterday, I was searching the web for information on how to create GUIs using c++, and I came across this website/forum. Thanks to it(Well thanks to the people here I guess ) I am know capable of programming using MFC :D(Beleive me, I did so much reading yesterday my eyes are extremely red right now ) Well anyway, Im doing a little program, and I ran into a problem. I started searching the web for help again, but the problem is, im not sure what exactly im looking for, so I decided to register and ask you guys The program Im writing basically only copies files from a certain folder to another one. I found a function called CopyFile() that does so perfectly. But the thing is, if I distribute the program, I won't be able to know where the program will be installed, so Ill have trouble using CopyFile(). Well, I solved this little problem by using GetFullPathName(). This is what I have so far: char pathholder[256]; char *filenum; GetFullPathName("Hi",256,pathholder,&filenum); int z = strlen(pathholder); z = z - 2; /*Let's say the program is stored in C:\Hello\ What is stored in pathholder is "C:\Hello\Hi" but I don't want the Hi part I only want "C:\Hello\" so I store the length of the string in z, and I subtract 2 to remove the Hi part, and I try something like this: */ char x[256]; strncpy(x,pathholder,z); //Ofcourse I realize that those last two statements are //rubbish... So basically, this is where I need your help. //Can you tell me what's the best way to copy the string to // another with out the "Hi" part? Thank you all for reading this, I hope you can help me. [edited by - ShadowOfVengeance on June 6, 2004 12:31:10 PM]

##### Share on other sites
Is searching the character array out of the question. You can easily find the location of the last ''\'' and chop off everything from thereafter. Need me to be more specific?

##### Share on other sites
Yeah, Id appreciate more details. Thanks.

Does anyone else have any other ideas?

##### Share on other sites
I'm going to stick with your original idea of what to do for simplicity's sake. There are much more efficient ways but well, I don't think it makes much of a difference unless you want the satisfaction of looking at 'l33t h4x0r' code:

char pathholder[256];char *filenum;GetFullPathName("Hi",256,pathholder,&filenum); int z = strlen(pathholder);//start my BS code hereint c = z-1;while(pathholder[c--]!='\');c++;//end my BS codechar x[256];strncpy(x,pathholder,c);

Now if you want me to point you toward better ways, I'll be more than happy but that function should work. EDIT: hrm actually I don't like that code at all so I will look it up.

Check it:
#include <windows.h>void main(){     char buffer[256];     GetCurrentDirectory(256, buffer);     cout << buffer;}

[edited by - uber_n00b on June 6, 2004 3:43:21 PM]

##### Share on other sites
Thanks alot That does the trick.

One more question if you don''t mind.
Let''s say buffer now contains C:\Hello
How can I make it C:\Hello\Hi and store it somewhere?
I know this doesn''t work but I mean something like this:

buffer = buffer + "\\Hi"

Im just finding it tricky working with character arrays, since Im used to Java where all this would be solved simply by using strings.

Speaking of Java, I just thought of another question, lets say I write a program(Stand Alone Application, not an Applet), and Im happy with it. Then I decide to distribute it. How do I do so? (I don''t want to distribute the .class since a user might not have the necessary program to run it)

Thanks again

##### Share on other sites
void main(void){	char str1[11] = "hereishowy";	char str2[7] = "oudoit";	strcat(str1, str2);	cout << str1 << endl;}/*I don't really know java so I don't know the answer to that second one, but I can say that you need either Sun or Microsoft's VM to run .class files anyway since java files aren't compiled into machine code.  I'm fairly sure you can run .class files as standalone given 1.  you run the right one and 2.  you have the VM.  I could be wrong.*/

[edited by - uber_n00b on June 6, 2004 4:44:37 PM]

##### Share on other sites
Man you're great My problems are completely solved... for now hehe

Oh and thanks for your opinion on the JAVA issue.

To anyone who knows JAVA, if you want to distribute a program you made, how would you do that?

Edit:
Uuh, it turns out I still have a problem. Sorry uber

Let's say I do this:

char buffer[256];
GetCurrentDirectory(256, buffer);
//Example: buffer has "E:\Hello\How"
//What I want to do is to store "E:\Hello\Hey" in a char array.
//If I use the method you gave me:

char str2[5] = "\\Hey";
strcat(buffer, z);

//Buffer would now have: "E:\Hello\How\Hey"
//So whats the best way to remove the "\How" part?

//I.E: The finished program would be:
char buffer[256];
GetCurrentDirectory(256, buffer);
//The function that remove the "\How" part goes here"
char str2[5] = "\\Hey";
strcat(buffer, z);
//So buffer would now have: "E:\Hello\Hey"

Im really sorry Im wasting everyone's time here... Especially you Uber, and again, thanks for your help

[edited by - ShadowOfVengeance on June 6, 2004 6:02:22 PM]

##### Share on other sites
quote:
Original post by uber_n00b
void main(void){	char str1[11] = "hereishowy";	char str2[7] = "oudoit";	strcat(str1, str2);	cout &lt;&lt; str1 &lt;&lt; endl;}/*I don''t really know java so I don''t know the answer to that second one, but I can say that you need either Sun or Microsoft''s VM to run .class files anyway since java files aren''t compiled into machine code.  I''m fairly sure you can run .class files as standalone given 1.  you run the right one and 2.  you have the VM.  I could be wrong.*/

<SPAN CLASS=editedby>[edited by - uber_n00b on June 6, 2004 4:44:37 PM]</SPAN>

One thing to watch out for here is that str1 must be long enough for the whole string, otherwise who knows what you''ll overwrite -- in this case (fortunatelly) the bits of the string that didn''t fit into str1 overwrote str2, so no harm was done, but in reallity you could end-up doing something really nasty (its called a buffer overrun & has been a cause of several MS security exploits). As an example try putting an variable (say an int initialised to 0) in between the definitions of str1 and str2, then after the strcat and the cout lines, put another cout line and print the value of the integer - depending on how the compiler has organised the variables on the stack you might find that the integer no-long has the value it started with (NB: if it does come out as zero, this is either b/c the compiler re-organsised the positions of the variables or that it decided that the varible was static & that there was no point in having it on the stack, if you stick a silly loop in at the end you might be able to force it to consider the variable alive, or actually try using the volatile keyword in the int declaration)

##### Share on other sites
Thanks for the important tip. Luckily, ive been using a large enough array.

Btw, I solved the problem now uber

Thanks for the help uber, its much appreciated, and thanks for the tip *anonymous poster*

Maybe someday Ill get good enough to start helpin people here, who knows eh?

Have a nice day everyone.

##### Share on other sites
Yeah sorry I should have mentioned that. You shouldn''t concatenate strings to longer than the first string''s allocated memory spaces and remember to leave a space for the end string marker. Ah I see the problem is solved. Well my job is done

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 14
• 14
• 9
• ### Forum Statistics

• Total Topics
633369
• Total Posts
3011534
• ### Who's Online (See full list)

There are no registered users currently online

×