Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 6th, 2006, 1:01 AM   #1
sixstringartist
Programmer
 
Join Date: Jun 2005
Posts: 68
Rep Power: 4 sixstringartist is on a distinguished road
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.
sixstringartist is offline   Reply With Quote
Old Nov 6th, 2006, 1:09 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 6th, 2006, 1:14 AM   #3
sixstringartist
Programmer
 
Join Date: Jun 2005
Posts: 68
Rep Power: 4 sixstringartist is on a distinguished road
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.
sixstringartist is offline   Reply With Quote
Old Nov 6th, 2006, 1:53 AM   #4
sixstringartist
Programmer
 
Join Date: Jun 2005
Posts: 68
Rep Power: 4 sixstringartist is on a distinguished road
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.
sixstringartist is offline   Reply With Quote
Old Nov 6th, 2006, 7:25 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 7th, 2006, 7:04 PM   #6
sixstringartist
Programmer
 
Join Date: Jun 2005
Posts: 68
Rep Power: 4 sixstringartist is on a distinguished road
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);
	  
}
sixstringartist is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:42 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC