• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

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.

2 Solutions
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 http://msdn.microsoft.com/en-us/library/6337eske%28v=vs.80%29.aspx ("Reading and Writing Files") for more info on reading files with the MFC file classes.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now