C++ Question.

Dimmu

Senior member
Jun 24, 2005
890
0
0
I want to be able to have this program open any/all text file/files that are in the same folder that the program resides in. Here is what I have thus far,

int main () {
cout << "This is what the file contained:\n";

char buffer[256];
ifstream fileName ("example.txt");
if (! fileName.is_open())
{ cout << "Error opening file"; exit (1); }

while (! fileName.eof() )
{
fileName.getline (buffer,100);
cout << "\n";
cout << buffer << endl;
}

int a;
cin >> a;

return 0;

}

it only opens one specific file ATM though. Could someone help edit this so that it opens any and all text files in its folder? TY in advance.
 

mattsaccount

Member
Nov 30, 2003
87
0
0
Enumerating the files in a directory is platform specific iirc. Try looking up the functions "findfirst" and "findnext". There are some cross-platform libraries available for this task, as well, but I don't recall their names.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Your class instructor probably told you what file functions to use, if you missed that part you might want to get notes from another student.

If someone posts code using different functions it might run, but your instructor will know you didn't do the work.
 

Dimmu

Senior member
Jun 24, 2005
890
0
0
This isn't for a class, I'm trying to teach myself C++. So it doesn't matter. Oh, and mattsaccount, I'll try looking for what you suggested.
 

itachi

Senior member
Aug 17, 2004
390
0
0
with windows.. you need access to the win32 api.. which you can only get with visual c++ or through .net (but i doubt you'll know how to deal with managed code). with linux, it's implemented through directory.h.

it's much simpler to use functions implemented by the kernel.. dir and ls both list the contents of the directory and can be used to filter the types and stream redirection can redirect the output of the programs to your own program.