470,815 Members | 1,042 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

How do i view files of a directory?

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);
}
}

May 30 '06 #1
4 4850
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

May 30 '06 #2
shailesh wrote:
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
May 30 '06 #3
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

"Peter Pippinger" <pe*************@gmx.de> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
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);
}
}
May 30 '06 #4
Peter Pippinger wrote:
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...ctory_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
May 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by sonu | last post: by
1 post views Thread by Jerry Tovar | last post: by
3 posts views Thread by Arun | last post: by
4 posts views Thread by Generic Usenet Account | last post: by
1 post views Thread by SteveT | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.