Reading in files without knowing the filenames beforehand?
Hi,
I have to write a program where I am given an input directory which will contain files with known extensions (e.g., *.exd, *.txt) but of which I do not know the beginnings of the names. Anyway, I have to be able to automatically detect all such files and then open them one by one. Can someone help me out how I can accomplish this? I know how to open a file when I know the entire name beforehand but have never had to do this before.
For example, lets say the directory contains the following files:
genseq001.txt
Data set 2.exd
Data set 2 jeff.exd
Data set 31 steve.exd
genseq002.txt
logref.txt
output.exf
output1.exf
So in this case, I have to be able to figure out that all these files are in the directory and then process them one by one except for the *.exf files.
Any help on what classes or commands I can use? I have to use standard C++ so it works on both Windows and Linux.
Thanks!
Re: Reading in files without knowing the filenames beforehand?
Quote:
Originally posted by C++ Newbie
Hi,
I have to write a program where I am given an input directory which will contain files with known extensions (e.g., *.exd, *.txt) but of which I do not know the beginnings of the names. Anyway, I have to be able to automatically detect all such files and then open them one by one. Can someone help me out how I can accomplish this?=
For Windows --> FindFirstFile() / FindNextFile()
For Linux --> opendir() / readdir()
For any other OS -- read your compiler's manual.
Basically reading a directory's entries is not only OS specific, it can also be compiler specific. For example, I remember the Borland compiler's version of finding files was dos_findfirst and dos_findnext, while Microsofts was findfirst and findnext. Another compiler may call it something else.
To make it portable, you have to write (or use) two sets of code, one for Windows, the other for Linux and maintain both sets of code. How you maintain it (whether you use #ifdef WINDOWS / #ifdef LINUX, or whether you create two sets of source and include the appropriate one in your projects, or some other method), that's up to you.
Regards,
Paul McKenzie