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

array filling question 1

Status
Not open for further replies.

rtnMichael

Programmer
Aug 5, 2002
152
US
hey guys, I have certain folders I have to have for my program to run. I build char arrays to get the full path names of the folders to check with a subroutine if they exist. I have about 9-10 to check for, and the sub has redundant code checking for each individual folder. I want to send the names to an array so I can just do a loop and the sub will be cut down to two IF statements...maybe a bit of code to help.

Here's where I build the path for some of the folders and place them in char arrays:

RootPath is set by the user

static void BuildNeededPaths()
{
/* Build needed directory names.
*/
sprintf(TargetDir, "%s%s", RootPath, "NotSent/");
sprintf(WipDir, "%s%s", RootPath, "Wip/");
sprintf(PurgeDir, "%s%s", RootPath, "OkToPurge/");
sprintf(DcutokenDir, "%s%s", RootPath, "DcuTokenArch/");
...etc
}

static int CheckDirectories()
{
struct stat stat_buf;

/* Get the status information and make sure its a directory.
*/
if(stat(RootPath, &stat_buf))
{
LogIt(1,FLAGIT,"Can not stat %s - %s", RootPath, strerror(errno));
return(0); /* not found */
}

if(!(S_IFDIR & stat_buf.st_mode))
{
LogIt(1,FLAGIT,"%s is not a directory", RootPath);
return(0);
}
BOTH THESE IF'S GO ON TO CHECK THE REST OF THE FOLDERS...THAT'S A LOT OF REDUNDANT CODE
}


What would be the correct syntax to place the TargetDir, RootPath, etc... in an array I could use for the loop and pass through to the sub?

Thanks
M
 
There are two ways to go about this. One is to allocate an array that can contain the largest possible path length times the number of paths you want to hold. This is easy but wasteful.

The better way is to have an array of pointer variables. Each of these variables would then point to an allocated string that contains the relevant path. Psuedo code would be as follows......

/* Define the maximum number of paths we can cater for */
#define MAX_PATHS 20
/* Array of pointers to strings */
char * ptr_path[MAX_PATHS];
/* Initialise array to NULLS */
for(x=0;x<MAX_PATHS;x++)
ptr_path[x]=0x00;
/* Build paths */
sprintf(tempbuff,&quot;%s%s&quot;, RootPath, &quot;NotSent/&quot;);
ptr_path[0]=malloc(strlen(tempbuff)+1);
strcpy(ptr_path[0],tempbuff);
sprintf(tempbuff,&quot;%s%s&quot;, RootPath, &quot;Wip/&quot;);
ptr_path[1]=malloc(strlen(tempbuff)+1);
strcpy(ptr_path[1],tempbuff);
sprintf(tempbuff,&quot;%s%s&quot;, RootPath, &quot;OkToPurge/&quot;);
ptr_path[2]=malloc(strlen(tempbuff)+1);
strcpy(ptr_path[2],tempbuff);

etc...

/* Now to access each element in the array */
for(x=0;x<MAX_PATHS;x++)
{
if(ptr_path[x]==0x00)
break;
if(stat(*ptr_path[x], &stat_buf))
{
LogIt(1,FLAGIT,&quot;Can not stat %s - %s&quot;, ptr_path[x], strerror(errno));
return(0); /* not found */
}

if(!(S_IFDIR & stat_buf.st_mode))
{
LogIt(1,FLAGIT,&quot;%s is not a directory&quot;, ptr_path[x]);
return(0);
}
}

/* Free up allocated memory */
for(x=0;x<MAX_PATHS;x++)
{
if(ptr_path[x]!=0x00)
free(ptr_path[x]);
}

This should give you a rough idea...
 
ok, quick question...why would I get a bus error when I do the strcpy?
 
Show me the code please, usually strcpy errors out when the area being copied to is not valid (ie too short, not allocated etc)...

Check the pointer is valid prior to the strcpy - eg printf(&quot;PTR %d = %X\n&quot;,x,ptr_path[x]); Check that the point is not NULL

Cheers
 
this is what I'm doing:
char * ptr_path[MAX_PATHS];

int x;
for(x = 0; x < MAX_PATHS; x++)
{
ptr_path[x] = 0x00;
printf(&quot;PTR %d = %X\n&quot;,x,ptr_path[x]);
}

/* Build needed directory names.
*/
ptr_path[0]=malloc(strlen(RootPath)+1);
strcpy(ptr_path[0], RootPath);
sprintf(TargetDir, &quot;%s%s&quot;, RootPath, &quot;NotSent/&quot;);
ptr_path[1]=malloc(strlen(TargetDir)+1);
strcpy(ptr_path[0], TargetDir);
...
 
I get all 0's whe I print it out with the supplied printf
 
I'd expect to see 0's at that stage as you haven't allocated any memory or pointers yet.

You need to place the printfs in more strategic places along these lines...

ptr_path[0]=malloc(strlen(RootPath)+1);

printf(Length = %d\n&quot;,strlen(RootPath));
printf(&quot;PTR = %x\n&quot;,ptr_path[0]);

strcpy(ptr_path[0], RootPath);
...

What do you get from this....
 
actually, the malloc is giving me a warning:
warning: incorrect combination of pointer and integer for
operator '='

I know it's just a warning, but can that have something to do with it?
 
here' what I get:

Length = 20
PTR = 88ec

Is this valid?
 
I don't get that warning in my test code, what compiler and operating system are you using?

You sometimes have to cast the malloc call but it shouldn't affect the result.

What do you get from the printf as I suggested though?
 
I know the RootPath length is right, but the ptr_path I have no clue
 
Very odd, I tried similar code on both Linux using gcc and Solaris using cc. In both cases I got no errors, the compiler warning is probably due to you not including the stdlib.h header file but again it doesn't affect the outcome.

The printf for the pointer should be longer I would have thought as the address you have quoted is very early in the memory range.

Is there any chance of posting the whole code? If not you could email it to me at newmangj@banksa.com.au and I'll look at it offline.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top