Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

appending char*'s 1

Status
Not open for further replies.

skiflyer

Programmer
Sep 24, 2002
2,213
US
I know I've done this a million times before, but it's been awhile and I'm drawing a huge blank... anyone help me?

I have two char* 's and I want to append them.

I'm using VC++.NET.

I don't want to make them string types or any of that nonsense.

Thanks,
Rob
 
Make sure that the buffer of the first one is large enough and use strcat, or create a new buffer and user sprintf.
Greetings,
Rick
 
Oh boy, I'm showing my rustiness... how do I set that?

here's the relevant snippit of code, minus error checking, I tried just changing char* tempdir; to char* tempdir[MAX_PATH];, but to no avail.

Code:
	FILE* stream;
	char* tempdir;
	int stat;

	tempdir = getenv("temp");
	strcat(tempdir, "\\data1.txt");

	stream  = fopen(tempdir, "w");
	fprintf(stream, "Hello you\r\n");
	fclose(stream);

Thanks much.

-Rob
 
Just as a note it works without doing anything, but I'm afraid I'm asking for trouble doing it that way.

-Rob
 
I don't think it's wise what you do there:

1. I believe that what getenv() returns is a pointer to the actual entry in its table. I would never append anything to this if I were you, as what you're doing is changing the table entry (which probably leads to access violations, since your' probably writing beyond the buffer). Better copy the string into a new buffer and append from there.

2. You cannot just strcat one char* to the other. Your destination buffer needs to be large enough, or you'll cause access violations.


You'd better do it like this:

_TCHAR szPath[MAX_PATH];

if(GetTempPath(MAX_PATH, szPath) && PathAppend(szPath, _T("data1.txt"))) {
do your stuff;
}

The beauty of PathAppend is, that it's wise enough to see whether or not backslashes need to be added. You'll have to link to shlwapi.lib to use it though.



But if you want to stick to getenv(), I'd od it like this:
_TCHAR* szEnv = _tgetenv(_T("temp"));
_TCHAR* szPath = new _TCHAR[_tcsclen(szEnv) + 11];
_tcscpy(szPath, szEnv);
_tcscat(szPath, _T("\\data1.txt")));

...
...

delete[] szPath;

Greetings,
Rick
 
I don't think it's wise what you do there:

1. I believe that what getenv() returns is a pointer to the actual entry in its table. I would never append anything to this if I were you, as what you're doing is changing the table entry (which probably leads to access violations, since your' probably writing beyond the buffer). Better copy the string into a new buffer and append from there.

2. You cannot just strcat one char* to the other. Your destination buffer needs to be large enough, or you'll cause access violations.


You'd better do it like this:

_TCHAR szPath[MAX_PATH];

if(GetTempPath(MAX_PATH, szPath) && PathAppend(szPath, _T("data1.txt"))) {
do your stuff;
}

The beauty of PathAppend is, that it's wise enough to see whether or not backslashes need to be added. You'll have to link to shlwapi.lib to use it though.



But if you want to stick to getenv(), I'd do it like this:
_TCHAR* szEnv = _tgetenv(_T("temp"));
_TCHAR* szPath = new _TCHAR[_tcsclen(szEnv) + 11];
_tcscpy(szPath, szEnv);
_tcscat(szPath, _T("\\data1.txt")));

...
...

delete[] szPath;

Greetings,
Rick
 
Great... I'm not sure whether or not that's how getenv() functions, but I certainlly prefer this method for all the other reasons.

Thanks again,

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top