简体   繁体   English

在Unix和Windows中运行程序

[英]running a program in Unix vs in Windows

I'm compiling a simple program written in C and I'm using Eclipse as an IDE, both in Windows 7 and on my MacBook Pro. 我正在编译一个用C编写的简单程序,我在Eclipse 7和MacBook Pro上都使用Eclipse作为IDE。 Very simple program my friend wrote and asked me to help him with: 我的朋友写的非常简单的程序,并让我帮助他:

int a = 0;
char b[2];
printf("Input first class info:\n");
printf("Credit Hours: \n");
scanf("%d", &a);
printf("Letter Grade: ");
scanf("%s", b);

So when I run this on my mac, each line prints and when I encounter the scanf(), I can input and continue as expected. 因此,当我在我的Mac上运行它时,每行打印,当我遇到scanf()时,我可以按预期输入和继续。 In Windows, I have to input everything and then it will print all the lines. 在Windows中,我必须输入所有内容然后它将打印所有行。 I'm not sure why this is happening... what is the difference b/w Windows and Mac here? 我不确定为什么会发生这种情况......这与Windows和Mac有什么区别?

Mac: 苹果电脑:

Input first class info:
Credit Hours: 4
Letter Grade: B+

Windows: 视窗:

4
B+
Input first class info:
Credit Hours:
Letter Grade:

Thanks, Hristo 谢谢,Hristo

As mentioned by this thread on Windows: 正如Windows上的这个帖子所提到的那样:

You need to fflush(stdout) after your call to printf() . 在调用printf()之后,你需要fflush(stdout) printf()

Also, because of bug 27663 , doing printf() to the Eclipse console doesn't flush until printf()'s buffer becomes full. 此外,由于错误27663 ,在Eclipse控制台上执行printf()不会刷新,直到printf()的缓冲区变满。
That has various associated bugs for Windows console: bug 102043 and bug 121454 . 这有各种相关的Windows控制台错误错误102043错误121454

It's likely due to buffer caching differences. 这可能是由于缓冲区缓存的差异。

Try: 尝试:

fflush(stdout);

before your scanfs. 在您的scanfs之前。 This will force the output to be flushed to the screen when you need to see it. 当您需要查看时,这将强制将输出刷新到屏幕。

Windows and Mac are buffering console output differently. Windows和Mac以不同方式缓冲控制台输出。 If you want it to appear immediately you need to flush it by calling 如果您希望它立即显示,您需要通过调用来刷新它

fflush(stdout);

after the printf. 在printf之后。

My guess is that on Mac OS X, the "\\n" causes stdout to be flushed, while this is not so on Windows. 我的猜测是,在Mac OS X上,“\\ n”会导致stdout被刷新,而在Windows上却不是这样。 Try adding the following piece of code after your print statements and before your scanf statements: 尝试在print语句之后和scanf语句之前添加以下代码:

fflush(stdout);

Like Fyodor said, it's most likely a line-ending problem. 就像费奥多尔所说,这很可能是一个终结问题。

On Windows, the line ending is "\\r\\n" (carriage-return followed by line-feed). 在Windows上,行结尾是“\\ r \\ n”(回车后跟换行)。

On Mac OSX, the line ending is just "\\r", but "\\r\\n" also works, because it includes the "\\r". 在Mac OSX上,行结尾只是“\\ r”,但“\\ r \\ n”也有效,因为它包含“\\ r”。

On Unix/Linux the line-ending is usually just "\\n". 在Unix / Linux上,行结尾通常只是“\\ n”。

In addition to the answers about the need for fflush() - your code contains a buffer overflow. 除了有关fflush()需求的答案之外 - 您的代码包含缓冲区溢出。 The scanf() into b writes 3 bytes - { 'B', '+', '\\0' } - and your array doesn't have enough room to store the NUL terminator. scanf()到b写3个字节 - { 'B', '+', '\\0' } - 你的数组没有足够的空间来存储NUL终结符。 You either need a 3 character wide buffer, or to use something other than scanf(%s) with a for reading the 2 characters in. 您需要一个3个字符宽的缓冲区,或者使用除scanf(%s)以外的其他内容来读取2个字符。

您想使用\\r\\n而不是\\n

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

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