Can't Figure this out

This is a discussion on Can't Figure this out within the C++ Programming forums, part of the General Programming Boards category; ok I have a tidbit of code and it compiles and works in borland but in microsoft it doesn't so ...

  1. #1
    Unregistered
    Guest

    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++;
       	}
       }

  2. #2
    Unregistered
    Guest
    It happens at

    Code:
    while(nCount < nChars)
       {
       	if(FilePathName[nCount] == 42)
          {
    		FilePathName[nCount] = '\0';   //error occurs here when debugging
    		bCantenate = true;
    		break;
          }
          else
          	nCount++;
       }

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    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).

  4. #4
    Unregistered
    Guest
    if argv is null then it passes a default one that is "C:\\*.idx" for debugging purposes

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > 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.

  6. #6
    Unregistered
    Guest
    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

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    char FileNameBuffer[100] [100] = {0};

    int main(void)
    .
    .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 04:22 PM
  2. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  3. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 12:47 AM
  4. My library is failing and I cannot figure out why...
    By DerelictDream in forum C++ Programming
    Replies: 7
    Last Post: 08-15-2005, 03:47 PM
  5. Can't figure out strcmp error
    By Fyodorox in forum C++ Programming
    Replies: 10
    Last Post: 05-08-2002, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21