Read all files within directiry and write on single file

KaimTazz's Avatar, Join Date: Aug 2007
Go4Expert Member
Hello Everybody

I have an idea abt read one file content and write them on another file. Now suppose I have five(approx.) files within Directory(say d:\datafile\). I wish to read content of more than one files one by one and write those content on single file. I dont know exactly but there is function like getfilenext() or getnextfile() which returns information abt subsequent file.

Pls suggest me someway to do this...Both C n C++ version will be preffered.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
This is going to be somewhat OS dependent. What avenues have you explored (say, via Google) to date? Let me recommend the "Before you post a query" thread. It emphasizes the importance of information, in case you never happened to realize that importance on your own.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
If you are on Windows platform try exploring the FindFirstFile and FindNextFile API's
0
jwshepherd's Avatar, Join Date: Aug 2007
Team Leader
Seems like a lot of work in c when batch for this is much easier.
create a batch file called combine.bat
combine.bat d:\datafile\*.* d:\combined.txt
Code:
@if "%1" == "" goto usage
@if "%2" == "" goto usage

@echo combine %1 to %2

@if not exist %2 copy "empty.file" "%2"

@for %%x in (%1) do copy /B "%2"+"%%x" "%2" 

goto exit

:usage
@echo Usage: combine.bat 'file to process (can include wild cards)' 'file name (can include directory to save to)'
@echo e.g. combine.bat *.txt temp\combined.txt

:exit
0
KaimTazz's Avatar, Join Date: Aug 2007
Go4Expert Member
To copy single jpeg file I tried following way:-


Code:
char buffer[512];
int source,desti,bytes;
source = open(m_sourcepath,O_RDONLY|O_BINARY);
if(source==-1)
{		
	MessageBox("Unable to open " + m_sourcepath);
	exit(0);
}
desti = open(m_destinationpath,O_WRONLY|O_BINARY);
if(desti==-1)
{		
	MessageBox("Unable to open " + m_destinationpath);
	exit(0);
}
while(1)
{
	bytes = read(source,buffer,512);
	if(bytes>0)
		write(desti,buffer,bytes);
	else
		break;
}
close(source);
close(desti);
MessageBox("Copied");

Does it right way to copy jpeg file?
Compiler is giving error "identifier not found" for read(), write(), open(), close(). which header file I forget to include. I have already iuncluded fcntl.h.

Pls consider I am using Visual Studio 2003 and dialog based application for making this program. I dont prefer to use CFile.
0
KaimTazz's Avatar, Join Date: Aug 2007
Go4Expert Member
Its Done.....
I forget to include "io.h"..

Thanks Shabir for FindFirstFile n FindNextFile.
Now after including Find Function, I think My program will be ready