您好,我正在编写一个简短的程序来实现 shell,但遇到了一个不寻常的问题。由于某种原因,我无法清除 std::cout 缓冲区。该程序不会打印消息。我知道一个简单的解决方案是切换到 std::cerr,但是有没有办法让消息用 cout 打印?我尝试过的事情:
std::cout.flush() std::endl在将任何内容写入标准输出之后插入。std::flush到输出流中std::cout.setf(std::ios::unitbuf);我发现这应该取消缓冲输出。非常感谢任何帮助,这是我的代码:
int main()
{
//Tryed this to unbuffer cout, no luck.
std::cout.setf(std::ios::unitbuf);
std::string input;
//Print out shell prompt and read in input from keyboard.
std::cout << "myshell> ";
std::getline(std::cin, input);
//**********************************************************************
//Step 1) Read in string and parse into tokens.
//**********************************************************************
char * buf = new char[input.length() + 1];
strcpy(buf, input.c_str());
int index = 0;
char * command[256];
command[index] = std::strtok(buf, " "); …Run Code Online (Sandbox Code Playgroud) 我是 C 新手,对不起,如果我的问题太基本了。我经常看到这样的代码:
printf("%d", counter);
fflush(stdout);
Run Code Online (Sandbox Code Playgroud)
我的猜测是,如果缓冲区未满,它将不会打印输出,因此您需要刷新stdout. 但是我试过不使用fflush,只是printf,我还把打印出来的东西印在屏幕上,那还有什么用flush呢?
我制作了简单的python程序来生成大文本文件:
import sys
import random
f = open('data.txt', 'w')
for i in range(100000000):
f.write(str(i) + "\t" + str(random.randint(0,1000)) + "\n")
f.close()
Run Code Online (Sandbox Code Playgroud)
当我使用CPython启动它时,它会占用所有可用的操作系统内存,并且不会向该文件写入任何内容
当我在Jython上启动时,我得到了OutOfMemoryException.
据我所知,它将所有内容存储在内存缓冲区中,并且在close()调用之前从未进行过刷新.
我的问题是:如何限制文件缓冲区并触发autoflush?我不想flush()手动调用,从性能的角度来看,我认为这是错误的.我希望flush()在文件缓冲区过载时自动调用.
谢谢!
我收到一个有趣的错误,表示标题已经通过ob_end_flush()发送; 当我运行以下代码时.这是我得到的确切错误
警告:不能更改头信息 - 头已经发出(输出开始/export/home/ua/games/gamescripts/login_backend2.php:47)在/export/home/ua/games/gamescripts/login_backend2.php在线57应该已经重定向到现在
第47行是ob_end_flush();
编辑:对不起我粘贴我的代码时有点粗心.我现在已经以正确的格式安排了它.
<?php
include_once('Services_JSON.php');
include 'connect.php';
include_once('functions.php');
error_reporting(E_ALL | E_WARNING | E_NOTICE);
ini_set('display_errors', TRUE);
$json = new Services_JSON();
$username= $_POST["username"];
$password = $_POST["password"];
$check=0;
$query="SELECT * FROM `game_users` WHERE `username` LIKE '$username'";
$result = mysql_query($query)
or
die("<br>Query Error: ".mysql_error());
while($row = mysql_fetch_row($result))
{
$user = $row[2];
$pass = $row[3];
$name = $row[1];
if($username == $user )
{
if($password == $pass)
{
$Day = 86400 + time(); // 1 Day
$name= "name";
$user= "user";
$pass= "password"; …Run Code Online (Sandbox Code Playgroud) 我编写了一个 MVC 框架,并且使用 ob_start('error_handler') 来捕获致命错误。这太棒了!
在 boot.php 中
// Start the error-logging
ob_start( 'error_logging' );
// Run the request
Request::run( URL::getInstance()->init()->formatURL() );
// Flush and disable output-buffering
ob_end_flush();
Run Code Online (Sandbox Code Playgroud)
错误记录功能:
function error_logging( $output )
{
// Get the last error
$trace = error_get_last();
// Initialize our final array of fields explaining the error
$final = array();
// Check if the error does not exist, return the original output unedited
if( !isset( $trace ) ) return $output;
// Loop through or error …Run Code Online (Sandbox Code Playgroud) 几天前,我通过阅读官方文档开始了 Rust 编程。现在,我试图通过阅读 Brian P. Hogan(The Pragmatic Programmers)的“程序员练习”一书来挑战我对 Rust 的理解。
第一个练习是编写一个程序,要求用户输入姓名并使用该姓名打印出问候语。输入、字符串连接和输出应该在三个不同的步骤中完成。
What is your name? Patrick
Hello, Patrick, nice to meet you.
Run Code Online (Sandbox Code Playgroud)
该名称将在与初始提示相同的行中输入。这是我的解决方案:
use std::io;
use std::io::Write;
fn main() {
print!("What is your name? ");
match io::stdout().flush() {
Ok(_) => print!(""),
Err(error) => println!("{}", error),
}
let mut name = String::new();
match io::stdin().read_line(&mut name) {
Ok(_) => {
name = name.trim().to_string();
if name.len() > 0 {
let greeting = "Hello, ".to_string() + &name + &", nice to meet you!".to_string();
println!("{}", …Run Code Online (Sandbox Code Playgroud) 在我的 REST 端点上收到请求后,我想回复状态200和连续生成和刷新的数据主体(从数据库获取,可能非常大)。我正在寻找一种使用Gorilla Mux的有效方法,因为它已在整个项目中使用。
我看到了,这是可能的Labstack回声,因为它的ResponseWriter 支持 http.Flusher(界面允许HTTP处理程序,以冲洗缓冲的数据到客户端)。不幸的是,似乎大猩猩的ResponseWriter不支持这一点。
问题:
不变量:
WebSockets并且应该将解决方案与 REST API 集成。我正在使用ObjectOutputStream将数据写入文件。以下是代码段。
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f)))
{
oos.writeObject(allObjects);
}
Run Code Online (Sandbox Code Playgroud)
问题:
文件损坏后,我看到的问题是在调试时遇到了上述查询。
我像这样使用反射来打印出System.out对象的属性:
System.out.println("Class: " + System.out.getClass().getName());
for (Field field : ObjectUtils.getAllFields(System.out)) {
field.setAccessible(true);
System.out.println("> " + field.getType().getSimpleName() + ' ' + field.getName() + " = " + field.get(System.out));
}
Run Code Online (Sandbox Code Playgroud)
这是结果:
Class: java.io.PrintStream
> boolean autoFlush = false
> boolean trouble = false
> Formatter formatter = null
> BufferedWriter textOut = java.io.BufferedWriter@43c1b556
> OutputStreamWriter charOut = java.io.OutputStreamWriter@587e5365
> boolean closing = false
> OutputStream out = org.apache.tools.ant.util.TeeOutputStream@22fcf7ab
Run Code Online (Sandbox Code Playgroud)
如您所见,autoflush设置为false。所以我的问题很简单-我怎么配置System.out已经autoflush设置为true?