LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   pleaaaaaaaaaaase help me !! (https://www.linuxquestions.org/questions/programming-9/pleaaaaaaaaaaase-help-me-185398/)

heshamm 05-24-2004 06:42 PM

pleaaaaaaaaaaase help me !!
 
Any body help me pleaaaaase , I want
C / C++ code that can be listing ( list files)
file by another command , I mean not by command ( LS ) , and the program take
the path from user , I hope that any body help me as possible ... Thanks Alot >>>

oulevon 05-24-2004 07:52 PM

Your apt to get more help if you try to do it on your own and post your code. I don't think you're going to find anyone to do the whole thing for you, but I'm sure plenty of people would be glad to help.

mojo_head 05-24-2004 10:04 PM

Sounds like some programming homework to me.
A

heshamm 05-25-2004 06:02 PM

/* sample program to illustrate the opendir(), readdir() and stat()
system calls

it lists all files in the given directory, and adds an asterisk
to each one which is a subdirectory */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/dir.h>

#define MAX_CHARS 200

int CharsRead,Tmp; /* number of characters read from a directory entry */
struct direct *DirEntryPtr; /* pointer to a directory entry */
DIR *DirPtr; /* pointer to the directory */
struct stat Stat;
char Path[MAX_CHARS];

main(argc,argv)
int argc; char **argv;

{ DirPtr = opendir(argv[1]);
while (1) {
DirEntryPtr = readdir(DirPtr);
if (DirEntryPtr == 0) break; /* reached end of directory entries */
/* process file (other than . and ..) */
if (strcmp(DirEntryPtr->d_name,".") != 0 &&
strcmp(DirEntryPtr->d_name,"..") != 0) {
/* print file name */
printf("%s",DirEntryPtr->d_name);
/* build full path name of the file, for stat() */
Path[0] = 0;
strcat(Path,argv[1]);
strcat(Path,"/");
strcat(Path,DirEntryPtr->d_name);
Tmp = stat(Path,&Stat);
/* Stat now contains lots of info about the file,
e.g. its size, though we are not interested in
most of that info here */
if (S_ISDIR(Stat.st_mode)) printf("*");
printf("\n");
}
}
}

This is my trying ,,, but its so bad because there are some errors ,, If any body help me I willl thank ful for him ,,,

itsme86 05-25-2004 06:34 PM

Here's a fixed-up version of your code:

Code:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/dir.h>

#define MAX_CHARS 200 // Should probably not have this and just use MAX_PATH

int CharsRead,Tmp; /* number of characters read from a directory entry */
struct direct *DirEntryPtr; /* pointer to a directory entry */
DIR *DirPtr; /* pointer to the directory */
struct stat Stat;
char Path[MAX_CHARS];
char WorkingDir[MAX_CHARS];

int main(int argc, char **argv)
{
  if(argc < 2)
    strcpy(WorkingDir, ".");
  else
    strcpy(WorkingDir, argv[1]);

  DirPtr = opendir(WorkingDir);

  while((DirEntryPtr = readdir(DirPtr)))
  {
    /* process file (other than . and ..) */
    if(strcmp(DirEntryPtr->d_name,".") && strcmp(DirEntryPtr->d_name,".."))
    {
      /* print file name */
      printf("%s", DirEntryPtr->d_name);

      /* build full path name of the file, for stat() */
      sprintf(Path, "%s/%s", WorkingDir, DirEntryPtr->d_name);

      Tmp = stat(Path, &Stat);
      /* Stat now contains lots of info about the file,
      e.g. its size, though we are not interested in
      most of that info here */
      if(S_ISDIR(Stat.st_mode))
        printf("*");
      printf("\n");
    }
  }

  return 0;
}

You had it mostly right. Only a few things.

heshamm 05-26-2004 06:06 AM

itsme86 , Thanks >>


All times are GMT -5. The time now is 09:52 AM.