How do i view files of a directory?

P

Peter Pippinger

Hello NG,

I have written some code which worked fine under c#. But i don´t know,
how this shoud work in c++. I have tryed much things, but i can´t find
out how to use DirectoryInfo and FileInfo under c++.

Thanks for any hints!
Peter

Here is the c# code:

//
---------------------------------------------------------------------
// -- View *.prn Files in Directory
//
---------------------------------------------------------------------
void Btn_aktualisierenClick(object sender, System.EventArgs e)
{
// clear listview
lv_fileliste.Clear();

DirectoryInfo dir = new DirectoryInfo("c:\\");
FileInfo[] fileInfo = dir.GetFiles("*.prn");

// Insert files in listview
foreach ( FileInfo fi in fileInfo )
{
lv_fileliste.Items.Add(fi.Name.ToString(),0);
}
}
 
S

shailesh

Standard C++ doesn't have support for reading directory listings.

If you are on Windows, see functions in platform SDK for Files and
Directories. Or you may use MFC class CFileFind.

-shailesh
 
B

benben

shailesh said:
Standard C++ doesn't have support for reading directory listings.

If you are on Windows, see functions in platform SDK for Files and
Directories. Or you may use MFC class CFileFind.

Or look into the Boost file system library (not just for windows.)
-shailesh

Ben
 
M

Moonlit

Hi,

A few threads ago I posted some notes and in another some code. This is
basically what you need


WIN32_FIND_DATA FindFileData;
HANDLE hFind;
string Wild = Dirname;

if( !Wild.empty() )
{
if( *Wild.rbegin() != '\\' ) Wild += '\\';

}
string Dir = Wild;
Wild += "*.*";

hFind = FindFirstFileEx( Wild.c_str(), FindExInfoStandard, &FindFileData,
FindExSearchNameMatch, NULL, 0 );

if (hFind == INVALID_HANDLE_VALUE)
{

stringstream Error;
Error << "Invalid File Handle. GetLastError reports " << GetLastError ()
<< endl;

// NOTE if errorcode is 2 it isn't really an error ('no such file or
directory')

throw CInfoException( Error.str() );
}
else
{
do
{
//NOTE: Filename = FindFileData.cFileName,
}
while( FindNextFile( hFind, &FindFileData ) );

FindClose(hFind);




--


Regards, Ron AF Greve

http://moonlit.xs4all.nl

Hello NG,

I have written some code which worked fine under c#. But i don´t know,
how this shoud work in c++. I have tryed much things, but i can´t find
out how to use DirectoryInfo and FileInfo under c++.

Thanks for any hints!
Peter

Here is the c# code:

//
---------------------------------------------------------------------
// -- View *.prn Files in Directory
//
---------------------------------------------------------------------
void Btn_aktualisierenClick(object sender, System.EventArgs e)
{
// clear listview
lv_fileliste.Clear();

DirectoryInfo dir = new DirectoryInfo("c:\\");
FileInfo[] fileInfo = dir.GetFiles("*.prn");

// Insert files in listview
foreach ( FileInfo fi in fileInfo )
{
lv_fileliste.Items.Add(fi.Name.ToString(),0);
}
}
 
H

homsan toft

Peter said:
I have written some code which worked fine under c#. But i don´t know,
how this shoud work in c++. I have tryed much things, but i can´t find
DirectoryInfo dir = new DirectoryInfo("c:\\");
FileInfo[] fileInfo = dir.GetFiles("*.prn");

This used to be off-topic for this group. But Boost.Filesystem is proposed
to the standard. It gives you fairly simple syntax, and portability.

Get it from http://boost.org/, read about it at http://boost.org/libs/filesystem/
The class directory_iterator is hiding in section Operations

Reference:
http://boost.org/libs/filesystem/doc/operations.htm#directory_iterator
Example:
http://boost.org/libs/filesystem/doc/index.htm#tutorial

If you're not used to such things, the most daunting part may be
that you need to compile the library (also off-topic here, and documented).

hth,
homsan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top