How to read files from a folder?


Any Ideas on how to read content(header) of multiple files from one folder? and based on that data construct another files in the same folder.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

trinitrotolueneDirector - Software EngineeringCommented:
Assuming you are on Windows....

I would go for just FindNextFile() and CopyFile(). The FindNextFile() gives you a data structure FIND_DATA which you can then use to decide whether the file in question is of importance to you or not.

You really don't need to read the header of a file. All you need are the attributes of a file and you can get these quite easily using File I/O APIs
titanium0203Author Commented:
Hi trinitrotoluene,

Thanks for the infos, but how can I use read or readstring to grab first 5 bytes or 20 characters in a binary files? I tried both functions but its not working.

titanium0203Author Commented:
Hi all

I got error as below when try to pass second argument in readstring.

code :

Error :

C:\temp\ListFile\ListFile.cpp(112) : error C2664: 'char *__thiscall CStdioFile::ReadString(char *,unsigned int)' : cannot convert parameter 1 from 'class CString' to 'char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:\temp\ListFile\ListFile.cpp(112) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
George TokasCommented:
Hi there,
Using C++ Builder (whatever version >= 5) and of course windows OS:
Drop a TFileListBox in the form visible or not.
Set the Directory to the path of the folder.
There will be a string list contains the paths of every file inside the folder.
Using a TMemoryStream and LoadFromFile() function you are loading the file.
Accessing the Memory property (array of binary data) you can examine the content.
You can use the same TMemoryStream object to clear and load another (or the next) file...
Thats for using C++ Builder.
Similar with Delphi.
I am sure that VC++ has similar ways...

George Tokas.
The compiler is telling you what you are doing wrong: you are trying to pass a CString to a function expecting a char*.  You need to obtain the char* buffer to your CString object by calling GetBuffer() instead:

#define MAX_BYTES_TO_READ 100
while( output.Readstring( Buffer1.GetBuffer( MAX_BYTES_TO_READ ), MAX_BYTES_TO_READ )
	// Need to release buffer before reading more into the CString object

Open in new window

See ("Reading and Writing Files") for more info on reading files with the MFC file classes.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.