strcpy(x, x) works in NT/2000 but not 95/98 
Author Message
 strcpy(x, x) works in NT/2000 but not 95/98

The problem is with strcpy.  If I make the second varible "user"
instead of CurrentUser which is a pointer varible it'll work on Windows
95.  Why is this happening and how do I fix it to work on both 95 and
2000?

Thanks, the code is as follows:

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <mbstring.h>

map_g();
map_g_term();
void map_p();
void map_s();
void map_s_term();
void map_u();

char *CurrentUser;
char strNameB[21] = "";
char Cmd[12] = "NET USE G: ";

int intCounter = 0;
char strName[21] = "";
char strNameA[21] = "";

void main()
{
CurrentUser = getenv( "USERNAME" );

map_g();

Quote:
}

map_g()
{
char Path[19] = "";

ifstream in("\\\\administration\\all\\user\\G.txt", ios::in |
ios::binary);

if(!in){
  cout << "Cannot open file.";
  return 1;

Quote:
}

//strcpy(strNameA, "user");
strcpy(strNameA, CurrentUser);
//strcpy(strNameB, "user");
strcpy(strNameB, CurrentUser);

strcat(strNameB, "\r");

strcat(Path, "\\\\administration\\");

strcat(Path, strNameA);
strcat(Cmd, Path);

strcat(Cmd, " /PERSISTENT:NO");

while(in) {
  in.getline(strName,20);

  if(strcmp(strNameB, strName)==0){
  system( Cmd );
  }

Quote:
}

in.close();

return 0;

Quote:
}

map_g_term()
{

Quote:
}

void map_p()
{

Quote:
}

void map_s()
{

Quote:
}

void map_s_term()
{

Quote:
}

void map_u()
{

Quote:
}

Sent via Deja.com http://www.*-*-*.com/
Before you buy.


Sun, 15 Dec 2002 03:00:00 GMT  
 strcpy(x, x) works in NT/2000 but not 95/98
first of all, you have to check the value returned from getenv() the
result might be a null pointer. You can't use a null pointer in strcpy.
Secondly, check the string's length before copying it; finally, I
suggest using std::string rather than char arrays and strcpy().

Danny Kalev

"The ANSI/ISO C++ Professional Programmer's Handbook"
http://www.amazon.com/exec/obidos/ASIN/0789720221

Quote:

> The problem is with strcpy.  If I make the second varible "user"
> instead of CurrentUser which is a pointer varible it'll work on Windows
> 95.  Why is this happening and how do I fix it to work on both 95 and
> 2000?



Mon, 16 Dec 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Reading registry values in 95/98 compared to NT/2000

2. stretchblt not working under NT but does under 95/98

3. CPageSetupDialog: Works in NT. Not in 95/98

4. IPC windows 95/98/NT/2000

5. Application runs on 2000 but not on 98 or 95

6. GetPrivateProfileSectionNames not working in 98 or ME but does work in 2000

7. Program runs ONCE in 98/ME, works fine in Xp/NT/2000

8. Code does not run under windows 95/98 but on NT

9. Different mnemonics behaviour on 95/98 and 2000/XP

10. Getting MAC Address in WIndows 98 and NT/2000/NT

11. GetPrivateProfileSectionNames works in 2000, doesn't work in 98 or ME

12. DBWin32 V2.2 Captures OutputDebugString on '95/'98/NT

 

 
Powered by phpBB® Forum Software