如何将ofstream用作结构体的一部分 – 函数参数。

huangapple go评论97阅读模式
英文:

How to use ofstream as a part of struct - arg of function

问题

I am trying to use ofstream, opened in function getlog() in function WriteLine(). I have a structure named LOG:

我试图在函数`getlog()`中打开的`ofstream`中使用它,而这个`LOG`结构是函数`WriteLine()`的参数。

struct LOG {
    wchar_t logfile[PARM_MAX_SIZE];
    std::ofstream* stream;
};

which is an argument of function WriteLine():
这是WriteLine()函数的一个参数:

When I am trying to use this stream, there is an error of reading:
当我尝试使用这个流时,出现了读取错误:

I am trying to use a reference on a LOG variable. I tried to convert ofstream* stream to ofstream& but I don't know how to do it properly, because initialization of a LOG is:

我试图使用`LOG`变量的引用。我尝试将`ofstream* stream`转换为`ofstream&`,但我不知道如何正确地做这件事,因为`LOG`的初始化是这样的:

static const LOG INITLOG {L"", NULL};

and I don't know how to change it to use a reference on a stream:
而且我不知道如何更改它以使用流的引用:

Functions:
函数:

LOG getlog(wchar_t logfile[]) {
    ofstream logf(logfile, ios::app);
    if (!logf.is_open()) {
        throw ERROR_THROW(112)
    }
    LOG log{};
    wcscpy_s(log.logfile, logfile);
    log.stream = &logf;
    return log;
}

void WriteLine(LOG log, char* c, ...) {
    char str[80]{};
    char** p = &c;
    while (*p[0]) {
        strcat_s(str, *p);
        p++;
    }
    str[strlen(str)] = '
LOG getlog(wchar_t logfile[]) {
    ofstream logf(logfile, ios::app);
    if (!logf.is_open()) {
        throw ERROR_THROW(112)
    }
    LOG log{};
    wcscpy_s(log.logfile, logfile);
    log.stream = &logf;
    return log;
}

void WriteLine(LOG log, char* c, ...) {
    char str[80]{};
    char** p = &c;
    while (*p[0]) {
        strcat_s(str, *p);
        p++;
    }
    str[strlen(str)] = '\0';
    ofstream* logf = log.stream;
    *logf << "str";
}
'
;
ofstream* logf = log.stream; *logf << "str"; }

main function:
主函数:

int _tmain(int argc, wchar_t* argv[]) {
    setlocale(LC_ALL, "rus");
    Log::LOG log = Log::INITLOG;
    try {
        Parm::PARM parms = Parm::getparm(argc, argv);
        log = Log::getlog(parms.log);
        Log::WriteLine(log, (char*)"Тест:", (char*)" без ошибок \n", "");
        /*Log::WriteLine(log, (wchar_t*)L"Тест:", (wchar_t*)L" без ошибок \n", L"");
        Log::WriteLog(log);*/
    }
    catch (Error::ERROR e) {
        cout << "Ошибка " << e.id << ": " << e.message << ", строка " << e.inext.line << ", позиция " << e.inext.col << endl << endl;
    }

    system("pause");
    return 0;
}
英文:

I am trying to use ofstream, opened in function getlog() in function WriteLine(). I have a structure named LOG:

struct LOG {
	wchar_t logfile[PARM_MAX_SIZE];
	std::ofstream* stream;
};

which is an argument of function WriteLine(). When I am trying to use this stream, there is an error of reading.

I am trying to use a reference on a LOG variable. I tried to convert ofstream* stream to ofstream&amp; but I don't know how to do it properly, because initialization of a LOG is:

static const LOG INITLOG {L&quot;&quot;, NULL};

and I don't know how to change it to use a reference on a stream.

Functions:

LOG getlog(wchar_t logfile[]) {
	ofstream logf(logfile, ios::app);
	if (!logf.is_open()) {
		throw ERROR_THROW(112)
	}
	LOG log{};
	wcscpy_s(log.logfile, logfile);
	log.stream = &amp;logf;
	return log;
}

void WriteLine(LOG log, char* c, ...) {
	char str[80]{};
	char** p = &amp;c;
	while (*p[0]) {
		strcat_s(str, *p);
		p++;
	}
	str[strlen(str)] = &#39;
LOG getlog(wchar_t logfile[]) {
ofstream logf(logfile, ios::app);
if (!logf.is_open()) {
throw ERROR_THROW(112)
}
LOG log{};
wcscpy_s(log.logfile, logfile);
log.stream = &amp;logf;
return log;
}
void WriteLine(LOG log, char* c, ...) {
char str[80]{};
char** p = &amp;c;
while (*p[0]) {
strcat_s(str, *p);
p++;
}
str[strlen(str)] = &#39;\0&#39;;
ofstream* logf = log.stream;
*logf &lt;&lt; &quot;str&quot;;
}
&#39;; ofstream* logf = log.stream; *logf &lt;&lt; &quot;str&quot;; }

main function

int _tmain(int argc, wchar_t* argv[]) {
	setlocale(LC_ALL, &quot;rus&quot;);
	Log::LOG log = Log::INITLOG;
	try {
		Parm::PARM parms = Parm::getparm(argc, argv);
		log = Log::getlog(parms.log);
		Log::WriteLine(log, (char*)&quot;Тест:&quot;, (char*)&quot; без ошибок \n&quot;, &quot;&quot;);
		/*Log::WriteLine(log, (wchar_t*)L&quot;Тест:&quot;, (wchar_t*)L&quot; без ошибок \n&quot;, L&quot;&quot;);
		Log::WriteLog(log);*/
	}
	catch (Error::ERROR e) {
		cout &lt;&lt; &quot;Ошибка &quot; &lt;&lt; e.id &lt;&lt; &quot;: &quot; &lt;&lt; e.message &lt;&lt; &quot;, строка &quot; &lt;&lt; e.inext.line &lt;&lt; &quot;, позиция &quot; &lt;&lt; e.inext.col &lt;&lt; endl &lt;&lt; endl;
	}

	system(&quot;pause&quot;);
	return 0;
}

答案1

得分: 0

Your getlog() function is creating a new stream and returning it, but is doing so by returning a pointer to a local variable that goes out of scope when the function exits. To avoid that, you will need to create the stream dynamically instead, eg:

struct LOG {
    std::wstring logfile;
    std::unique_ptr<std::ofstream> stream;
};

LOG getlog(const wchar_t* logfile) {
    LOG log;
    log.logfile = logfile;
    log.stream = std::make_unique<std::ofstream>(logfile, std::ios::app);
    if (!log.stream->is_open()) {
        throw ERROR_THROW(112)
    }
    return log;
}

template<typename... Args>
void WriteLine(LOG& log, Args&&... args) {
    (*(log.stream) << ... << args) << '\n';
}

int _tmain(int argc, wchar_t* argv[]) {
    setlocale(LC_ALL, "rus");
    try {
        Parm::PARM parms = Parm::getparm(argc, argv);
        Log::LOG log = Log::getlog(parms.log);
        Log::WriteLine(log, "Тест:", " без ошибок ");
        /*Log::WriteLine(log, L"Тест:", L" без ошибок ");
        Log::WriteLog(log);*/
    }
    catch (Error::ERROR e) {
        cout << "Ошибка " << e.id << ": " << e.message << ", строка " << e.inext.line << ", позиция " << e.inext.col << endl << endl;
    }

    std::system("pause");
    return 0;
}
英文:

Your getlog() function is creating a new stream and returning it, but is doing so by returning a pointer to a local variable that goes out of scope when the function exits. To avoid that, you will need to create the stream dynamically instead, eg:

struct LOG {
    std::wstring logfile;
    std::unique_ptr&lt;std::ofstream&gt; stream;
};

LOG getlog(const wchar_t* logfile) {
    LOG log;
    log.logfile = logfile;
    log.stream = std::make_unique&lt;std::ofstream&gt;(logfile, std::ios::app);
    if (!log.stream-&gt;is_open()) {
        throw ERROR_THROW(112)
    }
    return log;
}

template&lt;typename... Args&gt;
void WriteLine(LOG&amp; log, Args&amp;&amp;... args) {
    (*(log.stream) &lt;&lt; ... &lt;&lt; args) &lt;&lt; &#39;\n&#39;;
}

int _tmain(int argc, wchar_t* argv[]) {
    setlocale(LC_ALL, &quot;rus&quot;);
    try {
        Parm::PARM parms = Parm::getparm(argc, argv);
        Log::LOG log = Log::getlog(parms.log);
        Log::WriteLine(log, &quot;Тест:&quot;, &quot; без ошибок &quot;);
        /*Log::WriteLine(log, L&quot;Тест:&quot;, L&quot; без ошибок &quot;);
        Log::WriteLog(log);*/
    }
    catch (Error::ERROR e) {
        cout &lt;&lt; &quot;Ошибка &quot; &lt;&lt; e.id &lt;&lt; &quot;: &quot; &lt;&lt; e.message &lt;&lt; &quot;, строка &quot; &lt;&lt; e.inext.line &lt;&lt; &quot;, позиция &quot; &lt;&lt; e.inext.col &lt;&lt; endl &lt;&lt; endl;
    }

    std::system(&quot;pause&quot;);
    return 0;
}

huangapple
  • 本文由 发表于 2023年8月10日 19:25:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定