File I/O problem

Posted on 2011-10-20
Last Modified: 2012-05-12
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;
           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      
   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
Question by:devguru001
    LVL 30

    Accepted Solution

    Hi devguru001,

    I suspect for any reason the file isn't opened correctly so IMO first you should try to check it, i.e.:,std::ios::in);
    if ( !infile )
     std::cout << "Error opening file '" << file_path << "' (ec: " << GetLastError() << ")" << std::endl;

    Open in new window

    LVL 32

    Assisted Solution

    the FindFileFirst returns the first valid file of the result set. you should do the FindNextFile at end of loop or in the for step:

    for(int i=0; ok; i++, ok = FindNextFile(hFind,&FindFileData) )      

    Open in new window


    that way the FindNextFile was made after first loop circle.

    follow the advice Zoppo gave you and print out the wrong file_path.

    LVL 86

    Assisted Solution

    Um, since you want to copy these files, why don't you just call 'CopyFile()' ( for that? Or, even simpler, 'SHFileOperation()' ( e.g.
    sfo.wFunc = FO_COPY;
    sfo.pFrom = "C:\\Data\\*.txt\0\0"; // requires a double NULL terminator, not well documented
    sfo.pTo = "\"C:\\Data\\Office\\Work Related Documents\"\0\0"; // need to put quote arounds paths with spaces, incorrect in your original code

    Open in new window


    Author Closing Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now