Link to home
Start Free TrialLog in
Avatar of devguru001
devguru001Flag for South Africa

asked on

File I/O problem

Hello Experts

I am attempting to write a code that copies files from one location to another.  This is what Iv come up with so far.

#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <string>
#include <fstream.h>

using namespace std;


int _tmain(int argc, TCHAR *argv[])
{
   WIN32_FIND_DATA FindFileData;
   HANDLE hFind = FindFirstFile("C:\\Data\\*.txt", &FindFileData);      //Get all files in directory "C:\Data"
   int count = 0;
   std::string mstr = " ";
   std::string file_path = " ";
   std::string file_path_dest = " ";
   std::fstream infile,outfile;
   std::string tempstr= " ";  
   char c_string[1000] = {"\0"};
 
  for(int i=0;FindNextFile(hFind,&FindFileData) != 0 ; i++)      
  {  
        mstr = FindFileData.cFileName;      //Get name of next file
       
        file_path = "C:\\Data\\";       //Reload Original path into variable
        file_path_dest = "C:\\Data\\Office\\Work Related Documents\\";
       
        file_path += mstr;            
        file_path_fixed +=  mstr;
         
           infile.open(file_path.c_str(),std::ios::in);
           outfile.open(file_path_fixed.c_str(),std::ios::out);
                     
           while(infile.getline(c_string,1000,'\n'))            //THIS DOES NOT EXECUTE AFTER FIRST FILE IS PROCESSED
           {
               //Make changes at relevant positions of the file
           
            tempstr = c_string;
           
            outfile<<tempstr<<endl;      //Write line to new file      
           }
           
           infile.close();      
           outfile.close();
         
        count++;
   }
   
   FindClose(hFind);
   
   getch();
   return 0;
}

Trouble is, this code only works for the FIRST file only but it does not read/open the second file onwards.  When I check destination folder (C:\\Data\\Office\\Work Related Documents\\), all files are present but have a size of 0KB.....Any assistance will be highly appreciated.

Thank you
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of devguru001

ASKER

Thank you all for your comments experts.  Zoppo was right.  The file was not opened successfully.  I will be trying to solve that problem.