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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with scope.

Status
Not open for further replies.

TheMillionDollarMan

Programmer
Jun 10, 2002
132
0
0
US

Hi.

I have a very simple file reader that I'm trying to build up (I'm from MSVC++ 6.0).

Here is the code:
char cDir[500] ;
char c;
char ctest[10];
int in = open("file.in", O_RDONLY);
while(read(in,&c,1) == 1) {
sprintf(cDir,"%c",c);
printf(cDir); //Prints out characters ok
sprintf(ctest,"%c",c);
if ( strcmp(",",ctest) == 0)
{
sprintf(cDir,"%s -man", cDir);
printf(cDir); //only prints -man. Why?????
}
}

My problem is that after the if statement cDir no longer has any characters in it. its blank. It should have whatever was read in from the file.

Why is this so?

Thanks
D
 
> printf(cDir); //Prints out characters ok
Don't do this - if cDir contains a %, then the call to printf is broken, as it attempts to do some unknown conversion
Use
printf("%c",cDir); //Prints out characters ok
or
putchar(cDir);

> sprintf(cDir,"%s -man", cDir);
Overlapping source and destination buffers lead to undefined behaviour in nearly all standard C library functions (about the only exception I can think of at the moment is memmove())

Use another buffer to store the result
sprintf(result,"%s -man", cDir);

--
 

thanks Salem!
I am using
cout << "" << endl;
and it works better.

ps I am trying to convince my boss that C++ is faster than java. All we need to do is back end processing. No web, just data movement and manipulation.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top