我在做网络项目时遇到了这个问题.我能够缩小问题的范围,并像这样重现:
如果您运行此代码,它将不会在屏幕上显示文本.虽然如果你把\n放在文本的末尾或者在printf语句之后使用fflush(),它会显示文本.
int main(){
printf("started") ;
while(1){
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释这种行为吗?
我有这样的代码:
printf("Starting nets allocation...");
while(...)
{
...some operations...
}
puts("DONE");
Run Code Online (Sandbox Code Playgroud)
代码应该立即打印字符串"Starting nets allocation ..."然后,在循环之后,应该打印"DONE".
相反,程序首先执行循环,然后打印字符串"Starting nets allocation ... DONE"为什么会发生?我该如何解决这个问题?
我在重定向应用程序的标准输出时遇到问题.看起来这是.NET中的某种错误.
我正在运行Live555ProxyServer,但即使启动的控制台确实有写入输出,我也没有得到任何输出.此代码适用于任何其他控制台应用程序,但不适用于此代码.
void StartProcess()
{
var process = new Process();
process.StartInfo.FileName = @"live555ProxyServer.exe";
process.StartInfo.Arguments = "-R";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += process_OutputDataReceived;
process.Start();
process.BeginOutputReadLine();
}
void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Debug.WriteLine(e.Data);
}
Run Code Online (Sandbox Code Playgroud)
该应用程序的源代码可以在这里找到
我的 Windows 程序(使用 MSYS2 MINGW64 编译)stdout以大块输出其数据。使用aprintf()的调用\n无法正确刷新输出。
作为这个问题的变体,在什么条件下printf()不冲洗?
例如,以下代码在 MSYS2 MINGW64 上的块中输出:
#include <stdio.h>
int main() {
while(1) {
printf("test\n");
Sleep(1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我试图在论坛上回答一些问题,我遇到了很有趣的事情.这是代码:
int main()
{
int print_val = -1;
while(1)
{
printf("%d \n", ++print_val);
sleep(1);
}
}
Run Code Online (Sandbox Code Playgroud)
这很完美.现在乐趣进入..只需将第7行更改为
printf("%d ", ++print_val);(只需删除换行!)
现在没有输出..!
那么任何人都可以帮我理解sleep()函数的行为..?我认为需要查看sleep()而不是printf(),因为我尝试用fprintf()和putc()替换它,只给出相同的输出.
我在32位Ubuntu以及虚拟机中的32位Ubuntu上尝试过这段代码.
谢谢阿多恩
我无法理解为什么下面的代码会像这样工作..我的意思是:在每一秒延迟后不再打印"你好"......它会等待5秒并立即显示hellohellohellohellohello.
#include <stdio.h>
int i;
for(i=0; i<5; i++) {
printf("hello");
sleep(1);
}
Run Code Online (Sandbox Code Playgroud) 我有一个简单的c程序
#include <stdio.h>
int add(int, int);
int add (int a, int b) {
return a+b;
}
int main(void) {
int a, b, c;
printf("Enter the 1st number ");
scanf("%d",&a);
printf("Enter the 2nd number ");
scanf("%d",&b);
c = add(a, b);
printf("The value is %d ", c);
return (0);
}
Run Code Online (Sandbox Code Playgroud)
我正在编译程序,cc main.c
当我运行程序./a.out
时,控制台中没有任何输出。
我是 C 新手,对不起,如果我的问题太基本了。我经常看到这样的代码:
printf("%d", counter);
fflush(stdout);
Run Code Online (Sandbox Code Playgroud)
我的猜测是,如果缓冲区未满,它将不会打印输出,因此您需要刷新stdout. 但是我试过不使用fflush,只是printf,我还把打印出来的东西印在屏幕上,那还有什么用flush呢?
我编写此代码是为了“查找给定字符是否是数字”:
#include<stdio.h>
int main()
{
char ch;
printf("enter a character");
scanf("%c", &ch);
printf("%c", ch>='0'&&ch<='9');
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它已编译,但在获取输入后没有给出任何输出。然而,在将%c倒数第二行更改为%d格式说明符后,它确实起作用了。我有点困惑为什么%d有效但%c没有,尽管变量是char数据类型。
所以我很困惑.我正在阅读"从头开始编程"一书,并正在使用库.
printf工作得很好,只要我在字符串中包含一个"\n",但如果没有它,它将完全没有打印.
知道为什么会这样吗?
码:
.section .data
my_str:
.ascii "Jimmy Joe is %d years old!\n\0"
my_num:
.long 76
.section .text
.globl _start
_start:
pushl my_num
pushl $my_str
call printf
movl $1, %eax
movl $0, %ebx
int $0x80
此外,当我使用-m elf_i386进行32位模式和-dynamic-linker /lib/ld-linux.so.2 -lc进行链接时,我收到警告
ld:在搜索-lc时跳过不兼容的/usr/lib64/libc.so
如果这有任何区别,或者如果有人对如何直接加载32位库有任何建议.
谢谢!