Ok, we'll try this again.
you have 2 (or more) .cpp files which are trying to use the variable
myfile
. So, what we need is to define the variable
myfile
in the global scope (not inside any functions or classes). Then, in a .h file, we need to declare the variable as external, again in the global scope. This is done with
extern ofstream myfile;
So, we have:
abc.h:
#pragma once //This stops the file from including more than once in the same file
#include <iostream>
#include <fstream>
using namespace std;
extern ofstream myfile;
one.cpp:
#include "stdafx.h" //Commented out for my own use, you will still want to use this
#include "abc.h"
#include <stdio.h>
#include <string>
#include <vector>
#include <windows.h>
#include "fail.h"
typedef WIN32_FIND_DATA win32_find_data;
ofstream myfile;
typedef vector<win32_find_data> tFoundFilesVector;
std::string LastWriteTime;
int getFileList(const char *filespec, tFoundFilesVector &foundFiles) {
WIN32_FIND_DATA findData;
HANDLE h;
int validResult=true;
int numFoundFiles = 0;
h = FindFirstFile((LPCSTR)filespec, &findData); if (h == INVALID_HANDLE_VALUE)
return 0;
while (validResult)
{
numFoundFiles++;
foundFiles.push_back(findData);
validResult = FindNextFile(h, &findData);
}
FindClose(h);
return numFoundFiles;
}
void showFileAge(tFoundFilesVector &fileList)
{
unsigned _int64 fileTime, curTime, age;
tFoundFilesVector::iterator iter;
FILETIME ftNow;
CoFileTimeNow(&ftNow);
curTime = ((_int64) ftNow.dwHighDateTime << 32) + ftNow.dwLowDateTime;
for (iter=fileList.begin(); iter<fileList.end(); ++iter)
{
fileTime = ((_int64)iter->ftLastWriteTime.dwHighDateTime << 32) + iter->ftLastWriteTime.dwLowDateTime;
age = curTime - fileTime;
if (age <= (_int64)100000000UL) {
rename(string("c:\\Users\\DS\\Downloads\\").append(string(iter->cFileName)).c_str(),(string("c:\\FAIL\\").append(string(iter->cFileName)).c_str()));
wcout << " Delete: '" <<endl;
myfile << "\n Delete :";
wcout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << " seconds" << endl;
myfile << "\n FILE :"<< iter->cFileName << "', AGE: " << (_int64)age/10000000UL << " seconds" << endl;
remove(string("c:\\FAIL\\").append(string(iter->cFileName)).c_str());
ShellExecute(NULL, "open", "http://www.vvv.info/",NULL, NULL, SW_SHOWNORMAL);
}
else
{
wcout << " Ignore: '" <<endl;
myfile << "\n Igbore :";
wcout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << " seconds" << endl;
myfile << "\n FILE :"<< iter->cFileName << "', AGE: " << (_int64)age/10000000UL << " seconds" << endl;
}
}
return;
}
int main() {
myfile.open("C:\\times");
tFoundFilesVector vec_files;
getFileList("C:\\*.*", vec_files);
showFileAge(vec_files);
PrintFail();
return 0;
}
fail.h:
#pragma once //This stops the file from including more than once in the same file
#include "abc.h"
inline void PrintFail() {
myfile << "Hello, im failing" << endl;
}
This is complete code which compiles and runs as you would expect. It says how old each file is, and then prints "Hello, im failing" at the end of the output file.