标签: stdio

stdin上的close/fclose是否保证是正确的?

似乎以下调用执行您期望的操作(关闭流并且不允许任何进一步的输入 - 在流上等待输入的任何内容都会返回错误),但它是否保证在所有编译器/平台上都是正确的?

close(fileno(stdin));
fclose(stdin);
Run Code Online (Sandbox Code Playgroud)

c stdio

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

freopen:恢复原始流

我需要将stdout转发到不同的文件,以分离生成的一些打印和恢复到正常的标准输出.

freopen以这种方式切换到文件:

char name[80];
memset(name, 0, 80);
strcpy(name, "./scripts/asm/");
strcat(name, m_func->m_name->m_value);
strcat(name, ".shasm");
freopen(name, "w", stdout);
Run Code Online (Sandbox Code Playgroud)

它确实有效,但在过程结束时(请注意,stdout以前一种方式重定向多次)我无法将其恢复为原始标准输出.我尝试了以下方法:

freopen("/dev/stdout", "w", stdout);
Run Code Online (Sandbox Code Playgroud)

但它似乎不起作用..只是为了我正在macosx上开发的信息.

我该怎么办?

提前致谢

c c++ stdio

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

并发/非阻塞控制台键盘输入

我正在研究java中的MUD.我每次打勾都会看到玩家输入,但是我正在使用Scanner阻止操作.我想要非阻塞输入.

我看过nio有一个Selector类的包,但是我不确定如何使用它System.in.我想,一旦我运行服务器,我肯定会需要它,但现在一切都是离线的.

我已经尝试过扩展主类Applet和重写keyDown,但这只是意味着在第一个之后不再接受输入.当然,我不再阻止任何东西了,但是没有更多的输入.keyDown我想,再也没有打过电话.

也许线程即使在执行阻塞操作时也可能被中断?

感谢您对此问题的任何见解.

java input stdio textinput

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

从Python中的os.system()命令重定向stdio

通常我可以通过更改值来改变Python中的stdout sys.stdout.但是,这似乎只会影响print陈述.那么,有什么方法可以抑制通过os.system()Python中的命令运行的程序的输出(到控制台)?

python os.system stdout stdio

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

缓冲标准I/O库

UNIX环境中的高级编程(第2版)一书中,作者在第5.5节(标准I/O库的流操作)中写道:

打开文件进行读写(类型中的加号)时,以下限制适用.

  • 输出不能直接跟随输入而没有中间fflush,fseek,fsetpos,或rewind.
  • 输入不能直接跟随输出而没有中间fseek,fsetposrewind,或输入其遇到文件结束操作.

我对此感到困惑.谁能解释一下这个呢?例如,在什么情况下输入和输出函数调用违反上述限制会导致程序出现意外行为?我想这些限制的原因可能与库中的缓冲有关,但我不太清楚.

c stdio buffering

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

在`fwprintf'之后使用`fprintf'时无输出

这只是在我测试我分离的更大程序的一部分时发生的.原始函数将以我需要的特殊方式从字符串中删除非ascii字符,就是这个程序

#include <stdio.h>
#include <wchar.h>

int main(int argc, char *argv[])
{
    fwprintf(stdout, L"-- Example\n");
    fprintf(stdout, "-- Example\n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

不会-- Example在我的linux(Fedora 22)系统上打印第二个.虽然fwprintf()再次使用或fprintf(stderr, "-- Example\n");将工作.

  • 这是预期的行为吗?为什么?

c linux printf wchar-t stdio

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

fwrite是否刷新'\n'上的缓冲区?

我有自己的执行_open(),_close(),_write(),_read().

我的代码:

FILE *f = fopen("0:test", "wb");  // calls _open()
fwrite("hello ", 6, 1, f);
fwrite("world\r\n\0", 8, 1, f); // calls _write(3, "hello world\r\n", 13)
fflush(f);                      // calls _write(3, "\0", 1)
fclose(f);                      // calls _close(3)
Run Code Online (Sandbox Code Playgroud)

BUFSIZE 是1024

编译器:arm-none-eabi-gcc/版本:5.4.1

即使我有旗帜,为什么要fwrite()解释?'\n'"wb"

是否fopen()解释文件名"0:test"

c stdio stm32 newlib

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

STDERR_FILENO在ubuntu上未声明

我正在尝试编译显示代码的示例堆栈跟踪.当我用以下代码编译test.c文件时:

gcc -g -rdynamic ./test.c -o test
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

./test.c: In function ‘handler’:
./test.c:16: error: ‘STDERR_FILENO’ undeclared (first use in this function)
./test.c:16: error: (Each undeclared identifier is reported only once
./test.c:16: error: for each function it appears in.)
Run Code Online (Sandbox Code Playgroud)

我的包含与原始邮政编码相同:

#include <stdio.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>
Run Code Online (Sandbox Code Playgroud)

我的机器是ubuntu 13.04.我错过了一些图书馆还是没有包含一些内容?

c stdio

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

写入Rust中的文件或标准输出

我正在学习Rust,我有点难过.

我正在尝试为用户提供将输出写入stdout或提供的文件名的选项.

我开始与那正是使用给出的示例代码extra::getopts位于这里.从那里,在do_work函数中,我正在尝试这样做:

use std::io::stdio::stdout;
use std::io::buffered::BufferedWriter;

fn do_work( input: &str, out: Option<~str> ) {
    println!( "Input:  {}", input );
    println!( "Output: {}", match out {
        Some(x) => x,
        None    => ~"Using stdout"
    } );
    let out_writer = BufferedWriter::new( match out {
        // I know that unwrap is frowned upon, 
        // but for now I don't want to deal with the Option.
        Some(x) => File::create( &Path::new( x ) ).unwrap(),
        None    => stdout()
    } ); …
Run Code Online (Sandbox Code Playgroud)

file stdio rust

6
推荐指数
2
解决办法
3672
查看次数

如何从 Rust 中的 stdin 读取的行中删除换行符和回车符?

我想从 stdin 读取一行并将其存储在字符串变量中,并将字符串值解析为 u32 整数值。read_line() 方法读取 2 个额外的 UTF-8 值,这会导致 parse 方法崩溃。

以下是我尝试删除回车符和换行符的内容:

use std::io;
use std::str;

fn main() {
    let mut input = String::new();
    match io::stdin().read_line(&mut input) {
        Ok(n) => {
            println!("{:?}", input.as_bytes());
            println!("{}", str::from_utf8(&input.as_bytes()[0..n-2]).unwrap());
        }
        Err(e) => {
            println!("{:?}", e);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在 Rust 中执行此操作最惯用的方法是什么?

newline stdio utf-8 carriage-return rust

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

标签 统计

stdio ×10

c ×6

rust ×2

buffering ×1

c++ ×1

carriage-return ×1

file ×1

input ×1

java ×1

linux ×1

newlib ×1

newline ×1

os.system ×1

printf ×1

python ×1

stdout ×1

stm32 ×1

textinput ×1

utf-8 ×1

wchar-t ×1