-
Can't Figure this out
ok I have a tidbit of code and it compiles and works in borland but in microsoft it doesn't so I need help here you go. Also it is not optimized and is kinda weird looking cause I am debugging it and this one is confusing the hell out of me cause you can acces parts of the array with subscript notation. It might help that when I am running the program I get
Unhandled Exception in idx.exe: 0xc0000005: Access Violation
Code:
char *lpCommandLine = argv[1]; //holds the command line parameter such as c:\program files\index\*.idx
char FileNameBuffer[100] [100] = {0}; // this holds files containing *.idx
GetPathName(lpCommandLine, FileNameBuffer); // here is where I call the function
int GetPathName(char *FilePathName2, char FileNameBuffer[100][100])
{
int nCount = 0;
int nChars = strlen(FilePathName2);
char Buffer[10] = {0};
bool bCantenate = false;
char *FilePathName = FilePathName2;
while(nCount < nChars)
{
if(FilePathName[nCount] == 42)
{
FilePathName[nCount] = '\0';
bCantenate = true;
}
else
nCount++;
}
nCount = 0;
if(bCantenate)
{
while(FileNameBuffer[nCount][0] != '\0')
{
strncat(Buffer, FileNameBuffer[nCount], 10);
FileNameBuffer[nCount][0] = '\0';
strncat(FileNameBuffer[nCount], FilePathName, nChars);
strncat(FileNameBuffer[nCount], Buffer, 10);
Buffer[0] = '\0';
nCount++;
}
}
-
It happens at
Code:
while(nCount < nChars)
{
if(FilePathName[nCount] == 42)
{
FilePathName[nCount] = '\0'; //error occurs here when debugging
bCantenate = true;
break;
}
else
nCount++;
}
-
Are you sure you checked the argc variable if there is an argument?
Try to copy the argv[1] in another buffer and work with this buffer (don't change the argv variable).
-
if argv is null then it passes a default one that is "C:\\*.idx" for debugging purposes
-
> char Buffer[10] = {0};
This should be:
char Buffer[11] = {0};
Otherwise the strncat() could go past the end of the buffer.
> if(FilePathName[nCount] == 42)
Why not use:
if(FilePathName[nCount] == '*')
One area of concern is:
GetPathName(lpCommandLine, FileNameBuffer);
After this call lpCommandLine gets changed. This may be ok, but if you don't want it changing, make a copy down in the function.
-
I want Buffer to be 0-9 cause the file names will only be xxxxx.idx so that leaves one space for null terminator. I just thought I would use 42 cause I like it :) I don't know I just did use it. I actually want lpCommandLine to be changed because I use it later on in its morphed form well can anybody help I fixed the problem I talked to a co-worker and he didn't have any answer so thanks everybody.
Ryan
-
This statement could go past the end of array:
> strncat(Buffer, FileNameBuffer[nCount], 10);
You are telling it to concatenate 10 characters, but you buffer only holds 9 characters.
Also you might try declaring this array above main(), due to its size:
>char FileNameBuffer[100] [100] = {0}; // this holds files containing *.idx
-
char FileNameBuffer[100] [100] = {0};
int main(void)
.
.