简体   繁体   English

如何使用GCC在C程序中的函数之间暂停?

[英]How to pause between functions in a program in C with GCC?

So my problem is my program runs too fast that I can't see how it behaves. 所以我的问题是我的程序运行得太快,以至于我无法看到它的行为方式。 It's supposed to make the text crawl along the edges of the terminal. 它应该使文本沿终端的边缘爬行。 I tried to use sleep() to make a short pause between printf(...) s so that it I can see where the text us going while it prints. 我尝试使用sleep()printf(...) s之间做一个短暂的暂停,以便我可以在打印时看到文本的位置。

This is what it looks like: 这就是它的样子:

http://i.imgur.com/B6FFbNp.gif http://i.imgur.com/B6FFbNp.gif

So I put sleep() function after the printf s so that it will pause before starting the loop again and make the text move slowly. 所以我在printf之后放了sleep()函数,这样它会在再次启动循环之前暂停并使文本缓慢移动。 But what happens is it pauses the programs indefinitely before it even starts. 但是,它会在程序开始之前无限期地暂停程序。 This also happens with usleep and system("pause 1") . 这也发生在usleepsystem("pause 1") This is what it looks like: 这就是它的样子:

http://i.imgur.com/krGW3lB.gif http://i.imgur.com/krGW3lB.gif

================================================================================== ================================================== ================================

EDIT: 编辑:

Okay, I figured it out on my own. 好吧,我自己想出来了。 It seems that sleep() only works if I put \\n in my strings. 似乎sleep()只有在我的字符串中放入\\n才有效。 I don't know why. 我不知道为什么。 I didn't even read this in the damn manuals. 我甚至没有在该死的手册中读过这篇文章。

So if you have 所以,如果你有

printf("HELLO\n");
sleep(3);
printf("HELLO\n");
sleep(3);
printf("HELLO\n");

It will result in: 它将导致:

HELLO

[pause for 3 seconds] [暂停3秒]

HELLO

[pause for 3 seconds] [暂停3秒]

HELLO

but if you remove the newline characters it will: 但如果删除换行符,它将:

[pause for 9 seconds] HELLO HELLO HELLO [暂停9秒] HELLO HELLO HELLO

I don't know why this happens but it just does 我不知道为什么会这样,但事实就是如此

================================================================================== EDIT: ================================================== ================================编辑:

This is how I wanted my program to work: http://i.imgur.com/DXv7E60.gif 这就是我希望我的程序工作的方式: http//i.imgur.com/DXv7E60.gif

Thank you for your answers 谢谢您的回答

Your observations do not due to sleep() not working, but to the fact that printf() uses stdout , which is line buffered. 你的观察不是因为sleep()不起作用,而是因为printf()使用stdout ,这是行缓冲的。 "line buffered" means, that what has been written to stdout is buffered until the end of the line is reached before the buffer's content it is flushed out. “line buffered”意味着,已经写入stdout被缓冲,直到在缓冲区内容被刷新之前到达行尾。

So after the first printf("HELLO"); 所以在第一次printf("HELLO"); the output does not go to the screen but stays in the buffer, then sleep(1); 输出不会进入屏幕而是停留在缓冲区中,然后进入sleep(1); is executed and makes you wait. 被执行并让你等待。 Then for the next printf("HELLO"); 然后是下一个printf("HELLO"); the output still does not go to the screen, but the following sleep(1); 输出仍然没有进入屏幕,但下面的sleep(1); again makes you wait for 1 second ... and so on until a line end if reached by your program printing a '\\n' or by the program's end, which implicitly flushes the output buffer to the console. 再次让你等待1秒......依此类推,直到你的程序通过程序打印'\\n'或程序结束到达行结束,这会隐式地将输出缓冲区刷新到控制台。

You can avoid the behaviour by flushing stdout 's output buffer explicitly after every printf() : 您可以通过在每个printf()之后显式刷新stdout的输出缓冲区来避免此行为:

#include <stdio.h>
#include <unistd.h>

int main(void)
{
  printf("HELLO");
  fflush(stdout);
  sleep(3);
  printf("HELLO");
  fflush(stdout);
  sleep(3);
  printf("HELLO");
/* fflush(stdout); */ /* not needed as buffers are implicitly flushed on the program's end */

  return 0;
}

A quick hack I usually do is make some buffer array (eg. char buf[10]) and place an fgets() in between iterations, which forces the program to wait for a newline from the user. 我通常做的快速黑客是制作一些缓冲区数组(例如char buf [10])并在迭代之间放置一个fgets(),这迫使程序等待来自用户的换行符。 So, for example, if we had: 所以,例如,如果我们有:

.
.
.
for(i = 0; i < 1000000; ++i)
    printf("%d\n", i);

we could then do 我们可以做

.
.
.
char buf[10];
for(i = 0; i < 1000000; ++i){
    printf("%d\n", i);
    fgets(buf, 10, stdin);
}

and control the iterations with the enter key. 并使用回车键控制迭代。

We could also stop every nth iteration by using a modulus. 我们也可以通过使用模数来停止每第n次迭代。 Using the above example, we will now stop every 100 iterations: 使用上面的示例,我们现在将停止每100次迭代:

.
.
.
char buf[10];
for(i = 0; i < 1000000; ++i){
    printf("%d\n", i);
    if(i % 100 == 0)
        fgets(buf, 10, stdin);
}

A more time consuming but effecient way is to use a dedicated debugger like GDB. 更耗时但有效的方法是使用像GDB这样的专用调试器。

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

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