简体   繁体   English

通过系统()拧紧\\ n \\ r \\ n

[英]Screwing up \n\r\t by system()

I have read and tried many solutions and nothing solved my problem. 我已阅读并尝试了许多解决方案,但没有解决我的问题。

I am generating a dll out of c++ code and want to display the printf() or the std::cout inside of a console window. 我正在用c ++代码生成一个DLL,并希望在控制台窗口中显示printf()或std :: cout。 For testing I just create a .exe out of the c++ code but that is not solving the problem! 为了测试我只是从c ++代码创建一个.exe但是没有解决问题!

I know that system("Pause") is a bad habit, but this is just an easy way to get the same problem. 我知道系统(“暂停”)是一个坏习惯,但这只是解决同样问题的一种简单方法。 Instead of calling system("Pause") I'm doing a system()-call that is calling the cl.exe and is compiling a dll out of a .c file(Like: system("cl...). The generation of the dll out of the other c file is working without any problems. But after I called the system function for compiling the dll file the printf and std::out are not displaying the right text in the console window. 而不是调用系统(“暂停”)我正在做一个系统() - 调用cl.exe并正在编译一个.c文件的dll(如:system(“cl ...)”。)从其他c文件生成的dll工作没有任何问题。但是在我调用系统函数来编译dll文件之后,printf和std :: out没有在控制台窗口中显示正确的文本。

here is my example code that is screwing up the display of the right characters: 这是我的示例代码,它搞砸了正确字符的显示:

#include "stdafx.h"
#include <iostream>
#include <windows.h>

int _tmain(int argc, _TCHAR* argv[])
{
    AllocConsole();
    freopen("CONOUT$", "w", stdout); 
    std::cout << "Everything works fine\n";
    // Calling the cl.exe by cmd...
    system("Pause");
    std::cout << "now its strewd up the newline is not working\n" << std::endl;
    FreeConsole();
    return 0;
}

Here is a picture of the output: 这是输出的图片:

输出std :: cout

I tried the fflush before and after calling the system(). 我在调用system()之前和之后尝试了fflush。

So here are my further thoughts: 所以这是我的进一步想法:

  • Could I call the cl.exe by a CreateProcess and would that solve my problem?And when yes, how will I do that and to implement the environment variables out of the batch vcvars32.bat. 我可以通过CreateProcess调用cl.exe并解决我的问题吗?如果是,我将如何做到这一点并从批处理vcvars32.bat中实现环境变量。 So I want to create a dll out of a .c located both in the same folder. 所以我想创建一个位于同一文件夹中的.c的dll。
  • Or could write a batch file that is calling the whole cl.exe and vcvars32.bat file process. 或者可以写一个调用整个cl.exe和vcvars32.bat文件进程的批处理文件。
  • Or is there another way to display the values out of a dll instead of calling AllocConsole()? 或者是否有另一种方法来显示dll中的值而不是调用AllocConsole()?

I hope you can help me maybe I'm missing something! 我希望你能帮助我,也许我错过了什么!

So in order to try these error on my laptop (because I'm travelling today) I set up the same code at my laptop. 因此,为了在我的笔记本电脑上尝试这些错误(因为我今天正在旅行),我在笔记本电脑上设置了相同的代码。 Because I forgot to uncheck Security Development Lifecycle (SDL) checks there was an error when calling freopen("CONOUT$", "w", stdout); 因为我忘记取消选中Security Development Lifecycle (SDL) checks所以在调用freopen("CONOUT$", "w", stdout);时出错freopen("CONOUT$", "w", stdout); .Because the error message at building the code inside of visual studio 2015 was: 。因为构建visual studio 2015内部代码的错误消息是:

Severity Code Description Project File Line Error C4996 'freopen': This function or variable may be unsafe. 严重性代码说明项目文件行错误C4996'freopen':此函数或变量可能不安全。 Consider using freopen_s instead. 请考虑使用freopen_s。 To disable deprecation, use _CRT_SECURE_NO_WARNINGS. 要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。 See online help for details. 详细信息请参见在线帮助。 ConsoleApplication1 c:\\users\\noxious\\documents\\visual studio 2015\\projects\\consoleapplication1\\consoleapplication1\\consoleapplication1.cpp 12 ConsoleApplication1 c:\\ users \\ noxious \\ documents \\ visual studio 2015 \\ projects \\ consoleapplication1 \\ consoleapplication1 \\ consoleapplication1.cpp 12

I searched for the function freopen_s and exact at a example of freopen_s I saw the following code Outputting Unicode to Allocated Console Issue : 我在freopen_s的一个例子中搜索了函数freopen_s和exact我看到以下代码将Unicode输出到Allocated Console问题

freopen_s(&stream, "CONOUT$", "w+t", stdout);

I tried this one and acually my problem was solved! 我试过这个,我的问题解决了! BUT it wasn't the freopen_s that solved the problem it was just the inside of it "w+t" so I looked it up and saw that w+ is refering to the following type of access cstdio: freopen : 但是不是freopen_s解决了问题,它只是它内部的"w+t"所以我查了一下,看到w+引用了以下类型的访问cstdio:freopen

"w+" Create an empty file for reading and writing. “w +”创建一个空文件进行读写。 If a file with the same name already exists its content is erased before it is opened. 如果已存在具有相同名称的文件,则其内容在打开之前将被删除。

So I tried a few combinations and got this resulst: 所以我尝试了几种组合并得到了这个结果:

  • wt not working wt不工作
  • w+t working (but the t is always standard even if you use w+ is the same as w+t w+t工作(但即使你使用w+ ,t总是标准的,与w+t相同
  • w+ working w+工作

So I think I have just to setup an empty file for reading and writing! 所以我想我只需要设置一个空文件进行读写! I will looking further deep into that because I'm not quite sure what is the difference and if it is still solving the problem when using the dll instead of the exe file! 我将进一步深入研究,因为我不太确定有什么区别,如果在使用dll而不是exe文件时它仍在解决问题! Hope this might help some other people who are trying to solve same errors! 希望这可能会帮助其他试图解决相同错误的人! But just a "+" was delaying my work now for more than one day! 但只是一个“+”现在推迟了我的工作超过一天!

Edit 5.10.2015: So I tested this function with the "w+" instead of "w" and it fixed my problem!Even using a dll instead of a generated exe! 编辑5.10.2015:所以我用“w +”而不是“w”测试了这个函数,它解决了我的问题!即使使用dll而不是生成的exe!

It looks like the process is changing the output codepage on the console, then not changing it back when it's done. 看起来该过程正在改变控制台上的输出代码页,然后在完成后不再更改它。 Not sure why this would happen, but if that's the cause, it should be sufficient to save and restore the codepage: 不知道为什么会发生这种情况,但如果这是原因,那么保存和恢复代码页就足够了:

AllocConsole();
freopen("CONOUT$", "w", stdout); 
std::cout << "Everything works fine\n";
// Calling the cl.exe by cmd...
UINT codepage = GetConsoleOutputCP();
system("Pause");
SetConsoleOutputCP(codepage);
std::cout << "Should work now." << std::endl;
FreeConsole();
return 0;

BTW, it might also be screwing up the input codepage. 顺便说一下,它也可能搞砸输入代码页。 You can use GetConsoleCP() and SetConsoleCP() for that. 您可以使用GetConsoleCP()SetConsoleCP()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM