The contents of the array are safe within the function, however, upon returning to main the data becomes garbled.

section of main:

unsigned short tcp_port = atoi("8080");
const char* workDir = "WhatInthe?";

// Parse command line arguments

if ( parseCmdArg( tcp_port, workDir, argc, argv) == 1)
return 1;

cout << argc << endl;
cout << tcp_port << endl;
cout << workDir << endl << endl << endl;
cout << workDir << endl << endl << endl;

section of function code:

int parseCmdArg( unsigned short &tcp_port, const char* &workDir, int argc, char* argv[])
{

char tempPath[256];
bool pathDef = 1;

cout << "pCA tcp_port start: " << tcp_port << endl;
cout << "pCA wordDir start: " << workDir << endl;
cout << "pCA argc start: " << argc << endl;

*/ Command line process takes place here /*

if ( pathDef == 1 )
{
cout << "workDir = NULL" << endl;
getcwd(tempPath, 256);
workDir = tempPath;
}

cout << "pCA tcp_port exit: " << tcp_port << endl;
cout << "pCA wordDir exit: " << workDir << endl;
cout << "pCA tempPath exit: "<< tempPath << endl;
cout << "pCA argc exit: " << argc << endl;

The output to console is:

pCA tcp_port start: 8080
pCA wordDir start: WhatInthe?
pCA argc start: 1
workDir = NULL
pCA tcp_port exit: 8080
pCA wordDir exit: /user/ghesquie/cse422/lab3
pCA tempPath exit: /user/ghesquie/cse422/lab3
pCA argc exit: 1
1
8080
/user/ghesqui?
????????


/user/ghesqui? ??

So exiting the function the char array at pCA wordDir exit: is correct, but main show the first 13 characters, but the rest is garbled?

Any suggestions,

Thank you in advance,

Tex

Recommended Answers

All 3 Replies

Here is documentation for getcwd():

Is the buffer NULL terminated..???

Getting garbage at the end of a cstring is a common sign of mising NULL termination. After briefly looking at the getcwd() documentation, it does not say anything about providing a null terminator at the end of the array.

Thank you again for all the help.

I was unable to get getcwd() to work correctly, but after digging some more, get_current_dir_name() did the job just fine.

On to more coding...

Thanks,

Tex

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.