|
-
May 21st, 2008, 01:41 PM
#1
c++ - find first file in directory on linux system?
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
-
May 21st, 2008, 02:01 PM
#2
Re: c++ - find first file in directory on linux system?
[open/close]dir and readdir from glibc should do the trick.
-
May 22nd, 2008, 01:57 AM
#3
Re: c++ - find first file in directory on linux system?
thanks plasmator. found this in searches based off your link:
Code:
/* 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
-
May 22nd, 2008, 03:23 AM
#4
Re: c++ - find first file in directory on linux system?
 Originally Posted by BabaG
thanks plasmator. found this in searches based off your link:
Code:
/* 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
The 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.
All you have is main().
Regards,
Paul McKenzie
-
May 22nd, 2008, 11:58 AM
#5
Re: c++ - find first file in directory on linux system?
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
-
May 22nd, 2008, 11:59 AM
#6
Re: c++ - find first file in directory on linux system?
#include <cstdlib> for free().
-
May 22nd, 2008, 01:28 PM
#7
Re: c++ - find first file in directory on linux system?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|