?
Solved

File I/O problem

Posted on 2011-10-20
4
Medium Priority
?
245 Views
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;
         
           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
0
Comment
Question by:devguru001
4 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 668 total points
ID: 36998780
Hi devguru001,

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

Open in new window

ZOPPO
0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 668 total points
ID: 36998826
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:

BOOL ok = (hFind != INVALID_HANDLE_VALUE)? TRUE : FALSE;
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.

Sara
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 664 total points
ID: 37000549
Um, since you want to copy these files, why don't you just call 'CopyFile()' (http://msdn.microsoft.com/en-us/library/aa363851(VS.85).aspx) for that? Or, even simpler, 'SHFileOperation()' (http://msdn.microsoft.com/en-us/library/bb762164(VS.85).aspx)? e.g.
SHFILEOPSTRUCT sfo;

ZeroMemory(&sfo,sizeof(sfo));

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
sfo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI;

SHFileOperation(&sfo);

Open in new window

0
 

Author Closing Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

850 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