| View previous topic :: View next topic |
| Author |
Message |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Sat Mar 08, 2008 6:57 pm Post subject: [C++] returning CHAR |
|
|
Main.cpp
| Code: |
#include <windows.h>
#include <iostream>
using namespace std;
#include "Header.h"
int main()
{
//Gets Activation Code From Activation.ini
CHAR* Activation_Code = Read_Strings("Activation", "Key", "\\Activation.ini");
cout << Activation_Code;
}
|
Header.h
| Code: |
#include <windows.h>
CHAR* Read_Strings(CHAR*, CHAR*, CHAR*);
|
Read Ini.cpp
| Code: |
#include <windows.h>
#include <direct.h>
#include <string>
#include <iostream>
using namespace std;
#include "Header.h"
CHAR* Read_Strings(CHAR* Section, CHAR* Key, CHAR* File)
{
//Declare the current path and new path
char Current_Path[_MAX_PATH];
char* New_Path;
//use this function to get the path
_getcwd(Current_Path, _MAX_PATH);
//Get New path
New_Path = strncat(Current_Path, File, 50);
//Declare a string to get the string
char Return_String[50];
//Get String from settings.ini
GetPrivateProfileString(Section, Key, "No Key", Return_String, sizeof(Return_String), New_Path);
//return thing here
cout << Return_String;
return Return_String;
}
|
Output
| Code: |
Key
╠╠╠╠╠╠╠╠╠╠x╩T►╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╚╩T►☺╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠8☺ ╠╠╠╠╠╠╠╠<-
|
I know that the string is correct before I return it, what am I doing wrong? How can I get the string to return and not have the messed up stuff.
Off Topic - Super Smash Bros. Brawl is comming out tomorrow! Who's getting it? I know I am.
_________________
What dosen't kill you, usually does the second time. |
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Sat Mar 08, 2008 8:28 pm Post subject: |
|
|
I usually make it static. You could also go the dynamic memory route, but I don't recommend that.
| Code: |
static char Return_String[50] = { 0 };
|
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Sat Mar 08, 2008 10:13 pm Post subject: |
|
|
| Maybe i'm just having a im tired moment, but you declare the function in the Readini.cpp and the prototype in the header. You don't include readini.cpp in the header or main.cpp. It seems like readini isn't connected to the project in any way?
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Sat Mar 08, 2008 11:46 pm Post subject: |
|
|
You are trying to return a local pointer that gets destroyed after the function is done being called. Pass a pointer to a char array as a param in the function and copy the string to the pointer.
For example:
| Code: | #include <windows.h>
void Function1( char* szString )
{
char szName[1024] = {0};
GetEnvironmentVariable( "USERNAME", szName, 1024 );
strcpy_s( szString, 1024, szName );
}
int main()
{
char szName[1024] = {0};
Function1( szName );
return 0;
} |
_________________
- Retired. |
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Sun Mar 09, 2008 9:01 am Post subject: |
|
|
yea like wiccan said. But i thought i would point out that
char buf[1024] - big ass buffer is still a pointer to a char. You can pass that in a pointer situation as long as you do buf[sizeofbuffer]
And include the file in your project damit
|
|
| Back to top |
|
 |
|