Click to See Complete Forum and Search --> : c++ - find first file in directory on linux system?
BabaG
May 21st, 2008, 01:41 PM
all the examples i've seen seem to be for windows and are a
bit different than what i need. also, findfirstfile is not
in the textbook i bought to use as reference.
i need to find the first file in a directory so i can perform
a loop for processing. all of the files in the directory will
be of the same type and have the same name except that each
file is numbered. if i can find the first file in the directory,
i should be able to parse the name and increment the numbers in
the loop to move through the directory.
any tips on finding the first file's name?
thanks,
BabaG
Plasmator
May 21st, 2008, 02:01 PM
[open/close]dir and readdir from glibc should do the trick.
BabaG
May 22nd, 2008, 01:57 AM
thanks plasmator. found this in searches based off your link:
/* print files in current directory in reverse order */
#include <dirent.h>
main(){
struct dirent **namelist;
int n;
n = scandir(".", &namelist, 0, alphasort);
if (n < 0)
perror("scandir");
else {
while(n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
}
}
does almost exactly what i need so i think i can adapt
it if i can get this working. it produces errors when
i compile it. i get:
perror was not declared in this scope
printf was not declared in this scope
free was not declared in this scope
free was not declared in this scope
how do i properly declare these? or do i simply have
the wrong commands there for c++?
thanks,
BabaG
Paul McKenzie
May 22nd, 2008, 03:23 AM
thanks plasmator. found this in searches based off your link:
/* print files in current directory in reverse order */
#include <dirent.h>
main(){
struct dirent **namelist;
int n;
n = scandir(".", &namelist, 0, alphasort);
if (n < 0)
perror("scandir");
else {
while(n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
}
}
does almost exactly what i need so i think i can adapt
it if i can get this working. it produces errors when
i compile it. i get:
perror was not declared in this scope
printf was not declared in this scope
free was not declared in this scope
free was not declared in this scope
how do i properly declare these? or do i simply have
the wrong commands there for c++?
thanks,
BabaGThe code you found is for 'C' compilers.
The code will not compile for C++ for various reasons. First, In C++, all functions must be declared or defined before using them. You just can't call printf() without it being declared. You are missing the <cstdio> header. You are also missing the headers that declare perror() and free().
Second, the main() function returns an int.
int main()
All you have is main().
Regards,
Paul McKenzie
BabaG
May 22nd, 2008, 11:58 AM
thanks paul.
added:
#include <cstdio>
int main()
return 0
and am now left with only:
free was not declared in this scope
free was not declared in this scope
how do i declare 'free'?
thanks again,
BabaG
laserlight
May 22nd, 2008, 11:59 AM
#include <cstdlib> for free().
BabaG
May 22nd, 2008, 01:28 PM
thanks!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.