Monday, May 24, 2010

How do i iterate through files in c++?

i'm writing a program that handles a list of files. i don't know what the files are named. what i need to know is something like all the files ending in *.txt in a specific folder.





also, i would appreciate if someone could give me a link to a tutorial or something for things like this (file handling).

How do i iterate through files in c++?
you can use FindFirst(), and FindNext() functions. the link below isnt the greatest example of how to use them, but if you have some time you could probably find better examples.
Reply:The exact way to do this will depend on your operating system. For example, if you are in Windows, then you can fork a process and run the command "dir *.txt %26gt; blah.dat". Then, your C++ program can open blah.dat and parse it to find the names of the files.





This is one way to do it. There might be some Win32 system system calls that do the same thing. I suggest you comb through MSDN if you're running on Windows, and using the UNIX man pages if you're running on UNIX. If you can't find anything then I think the above way will do the job.
Reply:You can use the opendir(3) and readdir(3) C library calls to open a directory and iterate through files directly, or use glob(3) to do the pattern matching automatically.


Those are part of the POSIX interface, so they should be available on most systems.

sympathy flowers

No comments:

Post a Comment