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

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

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:

  1. 我试图在函数`getlog()`中打开的`ofstream`中使用它,而这个`LOG`结构是函数`WriteLine()`的参数。
  2. struct LOG {
  3. wchar_t logfile[PARM_MAX_SIZE];
  4. std::ofstream* stream;
  5. };

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:

  1. 我试图使用`LOG`变量的引用。我尝试将`ofstream* stream`转换为`ofstream&`,但我不知道如何正确地做这件事,因为`LOG`的初始化是这样的:
  2. static const LOG INITLOG {L"", NULL};

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

Functions:
函数:

  1. LOG getlog(wchar_t logfile[]) {
  2. ofstream logf(logfile, ios::app);
  3. if (!logf.is_open()) {
  4. throw ERROR_THROW(112)
  5. }
  6. LOG log{};
  7. wcscpy_s(log.logfile, logfile);
  8. log.stream = &logf;
  9. return log;
  10. }
  11. void WriteLine(LOG log, char* c, ...) {
  12. char str[80]{};
  13. char** p = &c;
  14. while (*p[0]) {
  15. strcat_s(str, *p);
  16. p++;
  17. }
  18. str[strlen(str)] = '
    LOG getlog(wchar_t logfile[]) {
  19.     ofstream logf(logfile, ios::app);
  20.     if (!logf.is_open()) {
  21.         throw ERROR_THROW(112)
  22.     }
  23.     LOG log{};
  24.     wcscpy_s(log.logfile, logfile);
  25.     log.stream = &logf;
  26.     return log;
  27. }
  28. void WriteLine(LOG log, char* c, ...) {
  29.     char str[80]{};
  30.     char** p = &c;
  31.     while (*p[0]) {
  32.         strcat_s(str, *p);
  33.         p++;
  34.     }
  35.     str[strlen(str)] = '\0';
  36.     ofstream* logf = log.stream;
  37.     *logf << "str";
  38. }
  39. ';
  40. ofstream* logf = log.stream;
  41. *logf << "str";
  42. }

main function:
主函数:

  1. int _tmain(int argc, wchar_t* argv[]) {
  2. setlocale(LC_ALL, "rus");
  3. Log::LOG log = Log::INITLOG;
  4. try {
  5. Parm::PARM parms = Parm::getparm(argc, argv);
  6. log = Log::getlog(parms.log);
  7. Log::WriteLine(log, (char*)"Тест:", (char*)" без ошибок \n", "");
  8. /*Log::WriteLine(log, (wchar_t*)L"Тест:", (wchar_t*)L" без ошибок \n", L"");
  9. Log::WriteLog(log);*/
  10. }
  11. catch (Error::ERROR e) {
  12. cout << "Ошибка " << e.id << ": " << e.message << ", строка " << e.inext.line << ", позиция " << e.inext.col << endl << endl;
  13. }
  14. system("pause");
  15. return 0;
  16. }
英文:

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

  1. struct LOG {
  2. wchar_t logfile[PARM_MAX_SIZE];
  3. std::ofstream* stream;
  4. };

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:

  1. 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:

  1. LOG getlog(wchar_t logfile[]) {
  2. ofstream logf(logfile, ios::app);
  3. if (!logf.is_open()) {
  4. throw ERROR_THROW(112)
  5. }
  6. LOG log{};
  7. wcscpy_s(log.logfile, logfile);
  8. log.stream = &amp;logf;
  9. return log;
  10. }
  11. void WriteLine(LOG log, char* c, ...) {
  12. char str[80]{};
  13. char** p = &amp;c;
  14. while (*p[0]) {
  15. strcat_s(str, *p);
  16. p++;
  17. }
  18. str[strlen(str)] = &#39;
    LOG getlog(wchar_t logfile[]) {
  19. ofstream logf(logfile, ios::app);
  20. if (!logf.is_open()) {
  21. throw ERROR_THROW(112)
  22. }
  23. LOG log{};
  24. wcscpy_s(log.logfile, logfile);
  25. log.stream = &amp;logf;
  26. return log;
  27. }
  28. void WriteLine(LOG log, char* c, ...) {
  29. char str[80]{};
  30. char** p = &amp;c;
  31. while (*p[0]) {
  32. strcat_s(str, *p);
  33. p++;
  34. }
  35. str[strlen(str)] = &#39;\0&#39;;
  36. ofstream* logf = log.stream;
  37. *logf &lt;&lt; &quot;str&quot;;
  38. }
  39. &#39;;
  40. ofstream* logf = log.stream;
  41. *logf &lt;&lt; &quot;str&quot;;
  42. }

main function

  1. int _tmain(int argc, wchar_t* argv[]) {
  2. setlocale(LC_ALL, &quot;rus&quot;);
  3. Log::LOG log = Log::INITLOG;
  4. try {
  5. Parm::PARM parms = Parm::getparm(argc, argv);
  6. log = Log::getlog(parms.log);
  7. Log::WriteLine(log, (char*)&quot;Тест:&quot;, (char*)&quot; без ошибок \n&quot;, &quot;&quot;);
  8. /*Log::WriteLine(log, (wchar_t*)L&quot;Тест:&quot;, (wchar_t*)L&quot; без ошибок \n&quot;, L&quot;&quot;);
  9. Log::WriteLog(log);*/
  10. }
  11. catch (Error::ERROR e) {
  12. 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;
  13. }
  14. system(&quot;pause&quot;);
  15. return 0;
  16. }

答案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:

  1. struct LOG {
  2. std::wstring logfile;
  3. std::unique_ptr<std::ofstream> stream;
  4. };
  5. LOG getlog(const wchar_t* logfile) {
  6. LOG log;
  7. log.logfile = logfile;
  8. log.stream = std::make_unique<std::ofstream>(logfile, std::ios::app);
  9. if (!log.stream->is_open()) {
  10. throw ERROR_THROW(112)
  11. }
  12. return log;
  13. }
  14. template<typename... Args>
  15. void WriteLine(LOG& log, Args&&... args) {
  16. (*(log.stream) << ... << args) << '\n';
  17. }
  18. int _tmain(int argc, wchar_t* argv[]) {
  19. setlocale(LC_ALL, "rus");
  20. try {
  21. Parm::PARM parms = Parm::getparm(argc, argv);
  22. Log::LOG log = Log::getlog(parms.log);
  23. Log::WriteLine(log, "Тест:", " без ошибок ");
  24. /*Log::WriteLine(log, L"Тест:", L" без ошибок ");
  25. Log::WriteLog(log);*/
  26. }
  27. catch (Error::ERROR e) {
  28. cout << "Ошибка " << e.id << ": " << e.message << ", строка " << e.inext.line << ", позиция " << e.inext.col << endl << endl;
  29. }
  30. std::system("pause");
  31. return 0;
  32. }
英文:

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:

  1. struct LOG {
  2. std::wstring logfile;
  3. std::unique_ptr&lt;std::ofstream&gt; stream;
  4. };
  5. LOG getlog(const wchar_t* logfile) {
  6. LOG log;
  7. log.logfile = logfile;
  8. log.stream = std::make_unique&lt;std::ofstream&gt;(logfile, std::ios::app);
  9. if (!log.stream-&gt;is_open()) {
  10. throw ERROR_THROW(112)
  11. }
  12. return log;
  13. }
  14. template&lt;typename... Args&gt;
  15. void WriteLine(LOG&amp; log, Args&amp;&amp;... args) {
  16. (*(log.stream) &lt;&lt; ... &lt;&lt; args) &lt;&lt; &#39;\n&#39;;
  17. }
  18. int _tmain(int argc, wchar_t* argv[]) {
  19. setlocale(LC_ALL, &quot;rus&quot;);
  20. try {
  21. Parm::PARM parms = Parm::getparm(argc, argv);
  22. Log::LOG log = Log::getlog(parms.log);
  23. Log::WriteLine(log, &quot;Тест:&quot;, &quot; без ошибок &quot;);
  24. /*Log::WriteLine(log, L&quot;Тест:&quot;, L&quot; без ошибок &quot;);
  25. Log::WriteLog(log);*/
  26. }
  27. catch (Error::ERROR e) {
  28. 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;
  29. }
  30. std::system(&quot;pause&quot;);
  31. return 0;
  32. }

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:

确定