Read all files within directiry and write on single file

Discussion in 'C' started by KaimTazz, Aug 17, 2007.

  1. KaimTazz

    KaimTazz New Member

    Joined:
    Aug 8, 2007
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    If you are on Windows platform try exploring the FindFirstFile and FindNextFile API's
     
  4. jwshepherd

    jwshepherd New Member

    Joined:
    Aug 13, 2007
    Messages:
    135
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    VP Technology Corporation
    Location:
    Texas
    Home Page:
    http://www.officialhackers.com
    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
    
    
     
  5. KaimTazz

    KaimTazz New Member

    Joined:
    Aug 8, 2007
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  6. KaimTazz

    KaimTazz New Member

    Joined:
    Aug 8, 2007
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice