ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 >>>
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.
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 ,,,
#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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.