int main(int argc, char *argV[]){
istream *br;
ifstream inFile;
if(argc == 2){
inFile.open(argV[1]);
if(inFile.is_open()){
cout << "file opened."; //if only "file opened" has an "\n then only it will print
br = &inFile; //and the program will freeze right after printing it
}
}
else if(argc <= 1){
br = &cin;
}
else{
cout << "Unrecognized commands";
}
cout << "test"; //having \n here allows the program to reach this line of code and
cout << "test2\n"; //everything before it
发生了一些奇怪的事情。除非字符串中包含“\ n”,否则不会打印到标准输出。例如。在底部的“test2 \ n”使得程序能够到达那些代码行,并且将关注到该点的所有事情,例如, “文件打开”行,因为test2已经打开了\ n并且打开了文件。如果将它们更改为“test1”test2“,程序将不会输出任何内容,包括”文件打开“。此外,如果将”文件打开“更改为”打开文件\ n“,则会打印该文件,但如果test1和test2没有\ n它们不会打印,因为它们位于“文件打开”中的\ n之后。为什么我的程序停止,除非包含“ n”
您是否尝试刷新流? – Default
什么是'istream * br;'用于? 1.你不应该需要一个指针,并且2.你不要初始化它! – crashmstr
你在unixish系统上?如果是这样,'./yourprogram;回声'有什么区别?你的shell提示是否包含回车并覆盖你的输出? – melpomene