繁体   English   中英

fstream 行为怪异

[英]fstream behaving weirdly

我是 c++ 的新手,但这太荒谬了!

// fstream output;
// string func();

// both have proven to be working somewhat properly, as I got something already
// written correctly in the output file and I tested func() in the cout

output << func(); // func() returns a string;

我写在文件中:

// literally nothing

但是当我这样做时

output << "what the hell" << endl;
output << func();

我明白了

what the hell
{// expected output}
what the hell
{// expected output}
...
what the hell
// last output still missing

我不知道可能是什么问题,在这一点上,我确信该程序只是为了惹恼我。

编辑:

string func()
{
    return "test\n";
}

我也刚发现只要手动往output里面放东西到最后什么都会写,像这样:

for(int=0; i<4; i++)
{
    output << func();
}
output << endl;

找我

test
test
test
test
// func line
// endl line

正如 One Man Monkey Squad 评论的那样,这只是不冲洗 fstream 的问题。

string func()
{
   return "test"
}

output << func() << endl;

给我想要的output,最后没有多余的线,超出正常的新线

暂无
暂无

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

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