标头不打印到日志文件

标头不打印到日志文件

本文介绍了标头不打印到日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何情况下都不确定是否要启动新线程或....我有一个日志文件,rgar可以正常运行,直到无法打印头文件中的函数结果为止.我尝试在头文件的顶部引用"ofstream myfile;",但它表示我正在重新定义"myfile".如果删除引用,则会得到"myfile未声明的标识符".该怎么办?参考的上一篇文章.
功能未打印到日志文件 [ ^ ]

Wasn''t sure if to start a new thread or...in any case. I have a log file rgar is worinh ok up to the point that it won''t print the results of a function that is in a header file. I tried to refernce ?ofstream myfile;" at the top of my header file but it says that I am redrfining "myfile". If I remove the reference then I get "myfile undeclared identifier. What to do? Previous post for refernece.
Function Not Printing To Log File[^]

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <windows.h>

using namespace std;

typedef vector<win32_find_data> tFoundFilesVector;
std::string LastWriteTime;
//int getFileList(wstring filespec, tFoundFilesVector &foundFiles) //uni
int getFileList(const char * filespec, tFoundFilesVector &foundFiles) //ansi
{
    WIN32_FIND_DATA findData;
    HANDLE h;
    int validResult=true;

    int numFoundFiles = 0;
    //h = FindFirstFile(filespec.c_str(), &findData); //uni 
	h = FindFirstFile((LPCSTR)filespec, &findData); //ansi 
    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();>
    {
        fileTime = ((_int64)iter->ftLastWriteTime.dwHighDateTime << 32) + iter->ftLastWriteTime.dwLowDateTime;

        age = curTime - fileTime;
		if (age <= (_int64)100000000UL) //10 seconds
		{
			rename(string("c:\\Users\\DS\\Downloads\\").append(string(iter->cFileName)).c_str(),(string("c:\\FAIL\\").append(string(iter->cFileName)).c_str()));
			wcout << " Delete: '" <<endl;
			extern ofstream; myfile << "\n   Delete  :";
			wcout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << "  seconds" << endl;
			extern ofstream; 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;
			extern ofstream; myfile << "\n   Igbore  :";
			wcout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << "  seconds" << endl;
			extern ofstream; myfile << "\n   FILE  :"<< iter->cFileName << "', AGE: " << (_int64)age/10000000UL << "  seconds" << endl;
			//return;
		}
    }
		  return;
}



头文件.



The header file.

推荐答案

#pragma once //This stops the file from including more than once in the same file

#include <iostream>
#include <fstream>

//Now anything including this will know what myfile is

using namespace std;

extern ofstream myfile; //This tells anything that includes this file that the variable myfile exists somewhere, and what type it is.



one.cpp:



one.cpp:

#include "stdafx.h" //Commented out for my own use, you will still want to use this
//#include <iostream> //Dont need this, included in abc.h, which passes it on
//#include <fstream> //Dont need this, included in abc.h, which passes it on
#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(wstring filespec, tFoundFilesVector &foundFiles) //uni
int getFileList(const char *filespec, tFoundFilesVector &foundFiles) //ansi
{
    WIN32_FIND_DATA findData;
    HANDLE h;
    int validResult=true;

    int numFoundFiles = 0;
    //h = FindFirstFile(filespec.c_str(), &findData); //uni 
	h = FindFirstFile((LPCSTR)filespec, &findData); //ansi 
    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) //10 seconds
		{
			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;
		}
    }
	return;
}

//This main() function is used for testing by me.
int main() {
	myfile.open("C:\\times");
	tFoundFilesVector vec_files;
	getFileList("C:\\*.*", vec_files);
	showFileAge(vec_files);
	PrintFail();
	return 0;
}



fail.h:



fail.h:

#pragma once //This stops the file from including more than once in the same file

#include "abc.h"

//Note: it is a bad idea to define function code in header files.
//This can lead to multiple definitions of the same function
//The keyword inline here is used as a hack around that, but it is typically not good style
inline void PrintFail() {
	myfile << "Hello, im failing" << endl;
}



这是完整的代码,可以按照您的期望进行编译和运行.它说明每个文件的年龄,然后在输出文件的末尾显示"Hello,im failure".



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.



这篇关于标头不打印到日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 15:25