相关疑难解决方法(0)

没有\n的printf在放置之前不显示文本(1)

可能重复:
为什么printf在调用后不会刷新,除非换行符在格式字符串中?(在C中)

我在做网络项目时遇到了这个问题.我能够缩小问题的范围,并像这样重现:

如果您运行此代码,它将不会在屏幕上显示文本.虽然如果你把\n放在文本的末尾或者在printf语句之后使用fflush(),它会显示文本.

int main(){
printf("started") ;
while(1){
}
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释这种行为吗?

c printf

3
推荐指数
1
解决办法
4778
查看次数

"printf"不会立即打印字符串

可能重复:
为什么printf在调用后不会刷新,除非换行符在格式字符串中?(在C中)

我有这样的代码:

printf("Starting nets allocation...");
while(...)
{
    ...some operations...
}
puts("DONE");
Run Code Online (Sandbox Code Playgroud)

代码应该立即打印字符串"Starting nets allocation ..."然后,在循环之后,应该打印"DONE".

相反,程序首先执行循环,然后打印字符串"Starting nets allocation ... DONE"为什么会发生?我该如何解决这个问题?

c printf

3
推荐指数
1
解决办法
6106
查看次数

Process.RedirectStandardOutput不起作用

我在重定向应用程序的标准输出时遇到问题.看起来这是.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)

该应用程序的源代码可以在这里找到

c# process output

3
推荐指数
1
解决办法
1733
查看次数

为什么带有 \n 的 printf() 在 Windows 上仍然无法刷新?

我的 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)

c windows flush mingw-w64 msys2

3
推荐指数
1
解决办法
1634
查看次数

在C ..中的sleep()行为?

可能重复:
为什么printf在调用后不会刷新,除非换行符在格式字符串中?

我试图在论坛上回答一些问题,我遇到了很有趣的事情.这是代码:

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上尝试过这段代码.

谢谢阿多恩

c linux sleep

2
推荐指数
1
解决办法
287
查看次数

睡在里面循环

我无法理解为什么下面的代码会像这样工作..我的意思是:在每一秒延迟后不再打印"你好"......它会等待5秒并立即显示hellohellohellohellohello.

#include <stdio.h>

int i;
for(i=0; i<5; i++) {
   printf("hello");
   sleep(1);
}       
Run Code Online (Sandbox Code Playgroud)

c linux sleep

2
推荐指数
1
解决办法
5388
查看次数

简单的c程序不打印输出

我有一个简单的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 console

2
推荐指数
1
解决办法
2万
查看次数

当 printf 可以自行打印时,为什么在 printf 之后使用 fflush?

我是 C 新手,对不起,如果我的问题太基本了。我经常看到这样的代码:

printf("%d", counter);
fflush(stdout);
Run Code Online (Sandbox Code Playgroud)

我的猜测是,如果缓冲区未满,它将不会打印输出,因此您需要刷新stdout. 但是我试过不使用fflush,只是printf,我还把打印出来的东西印在屏幕上,那还有什么用flush呢?

c flush stdio fflush

2
推荐指数
1
解决办法
357
查看次数

C 中格式说明符的作用是什么?

我编写此代码是为了“查找给定字符是否是数字”:

#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数据类型。

c format-specifiers

2
推荐指数
1
解决办法
162
查看次数

x86程序集 - 如果没有"\n",printf不会打印

所以我很困惑.我正在阅读"从头开始编程"一书,并正在使用库.

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位库有任何建议.

谢谢!

linux x86 assembly printf

1
推荐指数
1
解决办法
1904
查看次数

标签 统计

c ×8

linux ×3

printf ×3

flush ×2

sleep ×2

assembly ×1

c# ×1

console ×1

fflush ×1

format-specifiers ×1

mingw-w64 ×1

msys2 ×1

output ×1

process ×1

stdio ×1

windows ×1

x86 ×1