devguru001
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,&Fi ndFileData ) != 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_st r(),std::i os::in);
outfile.open(file_path_fix ed.c_str() ,std::ios: :out);
while(infile.getline(c_str ing,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
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\\*
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,&Fi
{
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_st
outfile.open(file_path_fix
while(infile.getline(c_str
{
//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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER