![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jun 2005
Posts: 68
Rep Power: 4
![]() |
Help debug please
This is driving me nuts.... I dont know what the problem is. Keep in mind Im very new to programming.
The following code give the undesired results for(i = 0; i < infoptr->usedInFilename; i++)
{
filename[i] = infoptr->filenameptr[i];
}Here, I am copying the char array at infoptr->filenameptr to a local char array (filename) to be modified. usedInFilename is the number of used elements in the array and filenameptr is an array containing a filename. Both are reading correctly. The problem is that it copies two elements to the destination array, but stops after that. 'i' continues to increment until meeting the loop condition. The destination array is left with only the first two char's of the source. I tried this using strcpy and also a while loop, but resulting the same. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
The third character is no doubt a '\0', terminating the string (even if there IS material behind it). I would imagine that if you output the array contents as integers, rather than as characters, you will discover that to be the case.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jun 2005
Posts: 68
Rep Power: 4
![]() |
That is what I assumed as well, but I cant for the life of me figure out why. I use DDD for debuging, and before the copy takes place, filename is empty, and info->filenameptr is the correct filename. I dont know where the '\0' is comeing from. The destination array is large enough as well.
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jun 2005
Posts: 68
Rep Power: 4
![]() |
I found a fix, but I still dont understand it. By using a for loop and setting all values of the destination array to '\0' before the copy, the copy for loop worked just fine. Any explanation to this would be greatly appreciated.
|
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You don't actually show enough code for anyone to be able to derive an explanation.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jun 2005
Posts: 68
Rep Power: 4
![]() |
ok here's some more information.
struct FileInfo
{
char *filenameptr;
int filearraysize;
int usedInFilename;
char *rangeptr;
int rangearraysize;
int usedInRange;
};The function: /****************************************************************
*
* void checkForAccess(struct FileInfo *)
*
* Arguments: pointer to an struct that containes the desired filename.
*
* Return value: 1 if Authorized, 0 if denied.
*
* This function checks the filename provided in its current directory
* for an access file. If present, it will check the access file for
* permission to open.
*****************************************************************/
int checkForAccess(struct FileInfo *infoptr)
{
char *access = "access";
char filename[100];
int i;
char current;
int j = 0;
int match;
char *temp;
FILE* accessfp;
char *deny = "deny";
char *DENY = "DENY";
/* The following for loop is how I
got around the initial problem */
for(i = 0; i < 100; i++)
{
filename[i] = '\0';
}
strcpy(filename, infoptr->filenameptr);
i = infoptr->usedInFilename - 1;
current = filename[i];
while(i >= 0 && current != '/')
{
filename[i] = '\0';
i--;
current = filename[i];
}
if(i == 0)
{
for(i = 0; i < 6; i++)
{
filename[i] = access[i];
}
}
else
{
for(i=i+1; i < 6; i++)
{
filename[i] = access[j];
j++;
}
}
accessfp = fopen(filename, "r");
if(accessfp == NULL)
{
return(1);
}
else
{
fscanf(accessfp, "%s", temp);
match = strncmp(temp, deny, 4);
if(match)
{
match = strncmp(temp, DENY, 4);
if(match)
{
fclose(accessfp);
return(1);
}
}
fclose(accessfp);
return(0);
}
return(1);
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help debug (short program) | sixstringartist | C | 17 | Sep 12th, 2006 12:58 AM |
| Debug Assertion Failed | myName | C++ | 6 | May 23rd, 2006 1:20 PM |
| The Debug Utility | darthsabbath | Assembly | 2 | May 20th, 2006 6:12 PM |
| Debug recursion method() | pr0gm3r | Java | 3 | Oct 11th, 2005 1:33 PM |
| Exception thrown on program exit in debug mode -- Win32 | uman | C++ | 4 | Jun 4th, 2005 5:28 AM |