Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Read all files within directiry and write on single file (http://www.go4expert.com/forums/read-files-directiry-write-single-file-t5900/)

KaimTazz 17Aug2007 05:14

Read all files within directiry and write on single file
 
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.

DaWei 17Aug2007 06:12

Re: Read all files within directiry and write on single file
 
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.

shabbir 17Aug2007 10:25

Re: Read all files within directiry and write on single file
 
If you are on Windows platform try exploring the FindFirstFile and FindNextFile API's

jwshepherd 17Aug2007 11:03

Re: Read all files within directiry and write on single file
 
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


KaimTazz 21Aug2007 16:19

Re: Read all files within directiry and write on single file
 
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.

KaimTazz 21Aug2007 18:10

Re: Read all files within directiry and write on single file
 
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


All times are GMT +5.5. The time now is 11:54.