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

truncated file names

Status
Not open for further replies.

TheObserver

Programmer
Mar 26, 2002
91
0
0
US
I'm using the following code in my application to get a listing of a directory's contents:

main(argc,argv)
int argc;
char *argv[];
{
DIR *dirp;
int i;
struct dirent *direntp;

//check our arguments and make sure we have enough to operate
if ( argc < 2 )
{
if(consoleout)
{
printf(&quot;\n&quot;);
printf(&quot;Usage: \n&quot;);
}
exit(1);
}


dirp = opendir( argv[1] );
if( dirp == NULL )
{
//we got a blank
if(consoleout)
{
printf(&quot;\nError opening input file directory.\n&quot;);
}
exit(1);

}
else
{
while ( (direntp = readdir( dirp )) != NULL )
{

if((direntp->d_name != &quot;.&quot;) && (direntp->d_name != &quot;..&quot;))
{
printf(&quot;filename: %s\n&quot;, direntp->d_name);
}
}

closedir(dirp);
return 0;
}

The problem is, with this code I only get partial filenames, at best. For instance, I have a directory full of files and this code only returns CC.CCC (two chars, the period, and the extension suffix). If I run this with another directory that contains only one file, I might get a different pattern altogether...there seems to be no rhyme or reason to which characters it is leaving off, except that they seem to only be missing characters from the front of the filenames.

Thanks for your time.
 
At first glance - this seems perfectly OK - Its a mirror of a procedure I run at this shop.
Anyone else any ideas ??????????? Dickie Bird (:)-)))
 
Maybe in the last else block you should use
strcmp(...) instead of != in the two if conditions inside the loop? Actually, I don't think thats gona help, but you never know.

When you do get the answer can you post it (its an interesting problem)? Which libraries did you include for this program (out of curiosity)?
 
I finally got it to work. For some reason the first &quot;/&quot; on the directory path (which is provided as a command line argument) was being removed (I have no idea why).

In the case of this program and the platform I'm using, I had to change the line in my code above from:

dirp = opendir( argv[1] );

to:

dirp = opendir(strcat(&quot;/&quot;,argv[1]));

It works fine now...but like I said, I have no idea why.

Anyway, the libraries I'm using are:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>

Hope this might help somebody in future.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top