unix C++: get the files from a folder


 
Thread Tools Search this Thread
Top Forums Programming unix C++: get the files from a folder
# 1  
Old 11-28-2006
unix C++: get the files from a folder

Hello everybody!
How can I get the list of files from a folder in C++ (unix)?

thanks in advance for any help!

regards
# 2  
Old 11-28-2006
You can do this in plain C, no C++ class madness required.

Code:
// listdir.c
#include <sys/types.h>
#include <dirent.h>

#include <stdio.h>

int main(int argc, char *argv[])
{
  struct dirent *de=NULL;
  DIR *d=NULL;

  if(argc != 2)
  {
    fprintf(stderr,"Usage: %s dirname\n",argv[0]);
    return(1);
  }

  d=opendir(argv[1]);
  if(d == NULL)
  {
    perror("Couldn't open directory");
    return(2);
  }

  // Loop while not NULL
  while(de = readdir(d))
    printf("%s\n",de->d_name);

  closedir(d);
  return(0);
}

For more details, see 'man 3 opendir', 'man 3 readdir', and 'man 3 closedir'.
# 3  
Old 11-29-2006
thanks a lot, Corona688!

regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

HP-UNIX How to zip/compress files in a folder?

Environment: HP-Unix Operating System B.11.31 U ia64 I have a folder with around 2000 files. There are some files which sizes are more than 8 GB. I need to compress all the files in the folder into a single file, so that I can transfer the file. I tried with tar command, but the tar... (8 Replies)
Discussion started by: Siva SQL
8 Replies

2. Shell Programming and Scripting

UNIX cmd -find empty files in folder else sleep for 8hrs

Hello, I am trying to write a unix cmd , that if files in folder /path/FTP are all zero kb or empty then good to go, if not empty then sleep for 8 hrs. Following cmd list me the files which are not empty, But when I am incorporating IF ELSE cmd fails find /path/FTP. -type f -exec wc -l {}... (6 Replies)
Discussion started by: bluestarmoon
6 Replies

3. UNIX for Dummies Questions & Answers

Applying python on all the files in one folder --UNIX

Dear all, I know this might be a simple and silly question but I am struggling with it I have a long unix script and in a section of if I want to use a python script which should select all the files and run the script on it (as a whole) the loop I have is selecting file by file and I dont know... (2 Replies)
Discussion started by: A-V
2 Replies

4. Solaris

Need to transfer files between 2 UNIX servers, with same folder permission

I need to transfer directories/files between 2 Unix servers, with same folder permission. I tried scp, but it retains the the permissions, but changes the owner of the directory/file to the user used to copy them to the destination. I don't want that to happen. If possible without any other... (6 Replies)
Discussion started by: Pandee
6 Replies

5. UNIX and Linux Applications

Move Files From One Folder To Another In UNIX

There are around 13 files in folder1.I need to move these files to another folder folder2,one file at a time, after checking whether a file exists in another folder folder3. If a file exists in folder3, we should not move files from folder1 to folder2. If there are no files in folder3, we need... (1 Reply)
Discussion started by: Jassz
1 Replies

6. Shell Programming and Scripting

Compiling all modified Java files in a folder on Unix

Hi all, I have a Unix script that will compile all Java files in a sub folder as follows: find . -name "*.java" -print -exec $JAVA_HOME/bin/javac -cp .:$CLASSPATH '{}' \; I would like to enhance it to only compile those Java files who: 1.) Have no class file 2.) Have a class file... (1 Reply)
Discussion started by: Annorax
1 Replies

7. UNIX for Advanced & Expert Users

UNIX: Command to compress folder and all files into a tar

I am trying to grab a folder and all the folders and files underneath it and send it from one computer to another. I basically want to compress the whole folder into a tar, tgz, or zip file so that it can be sent as one file. is there a command to compress a folder and all its contents into a tar... (7 Replies)
Discussion started by: kane4355
7 Replies

8. UNIX for Dummies Questions & Answers

unix command to cound the number of files in a folder

Hi All Can some one help me out. Please tell the unix command to cound the number of files in a folder. Ungent please# Thanks manas (6 Replies)
Discussion started by: manas6
6 Replies

9. UNIX for Dummies Questions & Answers

Viewing files of another unix server (in a folder)

I think that's what I'm trying to do. This is the problem: I log onto my comp, Comp1. Then, from the terminal, since my web server is on another comp, I type: xrlogin Comp2, so I log on to that computer. I then navigate to my directory by typing: cd /domain/myDir, so I am in my directory, on the... (3 Replies)
Discussion started by: Djaunl
3 Replies

10. Shell Programming and Scripting

How can i move data files from a server to unix folder

Hi: I am very new in UNIX environment. I need big help. How I can move data files from a server to UNIX folder by script. I don't want to use ws-ftp. The script should check the file on server, if any file found, move it to UNIX folder. I will be very happy, if some one helps me out.... (1 Reply)
Discussion started by: shah2
1 Replies
Login or Register to Ask a Question