标头不打印到日志文件 [英] Header Not Printing To Log File

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

问题描述

在任何情况下都不确定是否要启动新线程或....我有一个日志文件,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.

推荐答案

好,我们将再次尝试.

您有2个(或更多).cpp文件正在尝试使用变量myfile.因此,我们需要在全局范围内(而不是在任何函数或类内)定义变量myfile.然后,在.h文件中,我们需要再次在全局范围内将变量声明为外部变量.这是通过extern ofstream myfile;
完成的
因此,我们有:

abc.h:
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>

//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.


extern关键字添加到头文件的引用中,因此它看起来像extern ofstream myfile;.这表示该变量存在,但不需要再次定义,因为它是外部变量.

您仍然需要在1个.c/cpp文件中定义实际变量.仍然需要在某个地方定义它
Add the extern keyword to the reference in the header file, so it will look something like extern ofstream myfile;. This says that the variable exists, but does not need to be defined again, because it is external.

You will still need to define the actual variable in 1 of the .c/cpp files. It still needs to be defined somewhere


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆