LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-24-2004, 06:42 PM   #1
heshamm
LQ Newbie
 
Registered: May 2004
Posts: 3

Rep: Reputation: 0
Unhappy 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 >>>
 
Old 05-24-2004, 07:52 PM   #2
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
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.
 
Old 05-24-2004, 10:04 PM   #3
mojo_head
LQ Newbie
 
Registered: May 2004
Distribution: slackware
Posts: 3

Rep: Reputation: 0
Cool

Sounds like some programming homework to me.
A
 
Old 05-25-2004, 06:02 PM   #4
heshamm
LQ Newbie
 
Registered: May 2004
Posts: 3

Original Poster
Rep: Reputation: 0
/* 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 ,,,
 
Old 05-25-2004, 06:34 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 05-26-2004, 06:06 AM   #6
heshamm
LQ Newbie
 
Registered: May 2004
Posts: 3

Original Poster
Rep: Reputation: 0
itsme86 , Thanks >>
 
  


Reply



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 Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration