Why cout seems not act like line buffering - io

`
#include<iostream>
#include<Windows.h>
using namespace std;
int main()
{
cout << 'b';
Sleep(5000);
return 0;
}
`
The b is printed onto the screen immediately.
But the cout is acting in line buffering.
The b is expected to be printed after 5 seconds when "return 0;" flashes the I/O buffer by me.

Related

why cout not works before getche() in VC++ 6.0?

I want to show a message before an input :
cout<<"Enter a char:";
ch = getche();
But when running program it does not show the message and getche() works and after that the message is showed !!!
The whole program :
#include <iostream.h>
#include <conio.h>
int main(){
int ch;
cout<<"Enter a char:";
//cin.ignore();
ch = getche();
return 0;
}
The whole problem that, i want to do is this:
1- show a message
2- enter ONLY one char
3- do something immediately after entering the char
Adding endl soleved the problem :
cout<<"Enter a char:"<<endl;
ch = getche();

How to use try-catch to catch floating point errors?

#include <iostream>
#include <float.h>
#pragma fenv_access (on)
int main(int, char**argv)
{
unsigned int fp_control_word;
_controlfp_s(&fp_control_word, 0, 0);
const unsigned int new_fp_control_word = fp_control_word | _EM_INVALID | _EM_DENORMAL
| _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW | _EM_INEXACT;
_controlfp_s(&fp_control_word, new_fp_control_word, _MCW_EM);
try
{ std::cout << std::atof(argv[1]) / std::atof(argv[2]) << std::endl;
} catch (...)
{ std::cout << "caught exception" << std::endl;
}
}
I remember that it is possible to catch memory access errors on windows using a try-catch block.
There is already a question regarding this subject. But it is 10 years old and the code provided does not result in an exception, but in printing a NAN.
I was always curious about using this feature to abort some piece of numerical code in a nice way. The motivation is to abort some VERY COMPLEX piece of code immediately, if anywhere in this code a floating point exception occurred rather than keeping evaluating the rest of the code with NAN results -- which is rather slow and anyway does not make sense.
Please: I don't care if this is not supported by the C++ standard!
The question is, how to get this code to run into the catch-block -- e.g. by using the command line parameters 0.0 0.0!
For me it always prints out NAN.
What compiler options need to be used?
Or does the code need to be changed?
If one provokes a nullptr dereference in the try-block one will end up in the catch-block. But not for division by zero.
One needs to use the compiler option /EHa to enable structured exception handling.
Thanks to https://stackoverflow.com/users/17034/hans-passant for the solution.
Here comes the working code:
#include <iostream>
#include <float.h>
#pragma fenv_access (on)
int main(int, char**argv)
{
unsigned int fp_control_word;
_controlfp_s(&fp_control_word, 0, _MCW_EM);
const unsigned int new_fp_control_word = fp_control_word & ~(_EM_INVALID
| _EM_DENORMAL | _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW | _EM_INEXACT);
_controlfp_s(&fp_control_word, new_fp_control_word, _MCW_EM);
try
{ std::cout << std::atof(argv[1]) / std::atof(argv[2]) << std::endl;
} catch (...)
{ std::cout << "caught exception" << std::endl;
}
}

Reading and writing different files in their own threads from my main loop in C++11

I am trying to understand, then, write some code that has to read from, and write to many different files and do so from the main loop of my application. I am hoping to use the C++11 model present in VS 2013.
I don't want to stall the main loop so I am investigating spinning off a thread each time a request to write or read a file is generated.
I've tried many things including using the async keyword which sounds promising. I boiled down some code to a simple example:
#include <future>
#include <iostream>
#include <string>
bool write_file(const std::string filename)
{
std::cout << "write_file: filename is " << filename << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
std::cout << "write_file: written" << std::endl;
return true;
}
int main(int argc, char* argv[])
{
const std::string filename = "foo.txt";
auto write = std::async(std::launch::async, write_file, filename);
while (true)
{
std::cout << "working..." << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::cout << "write result is " << write.get() << std::endl;
}
}
I'm struggling to understand the basics but my expectation would be that this code would constantly print "working..." and interspersed in the output would be the write_file start and end messages. Instead, I see that the write_file thread seems to block the main loop output until the timer expires.
I realize I need to also consider mutex/locking on the code to actually write the file but I would like to understand this bit first.
Thank you if you can point me in the right direction.
Molly.
write.get() will wait for the async task to finish. You want to use wait_for() instead:
do {
std::cout << "working...\n";
} while(write.wait_for(std::chrono::milliseconds(100)) != std::future_status::ready);
std::cout << "write result is " << write.get() << "\n";

How to scan strings with spaces in cpp in a new line?

enter image description here3(no of cases)
hello world
a b c d
data structures and algorithms.
Let say above is the given input format.Each string starts in a new line.Can i know how to read in the above way in cpp.I tried with getline function.It didn't work.
The getline method should work just fine. The following code works fine.
#include <iostream>
using namespace std;
int main(){
string s[3];
getline(cin, s[0]);
getline(cin, s[1]);
getline(cin, s[2]);
cout << s[0] << endl << s[1] << endl << s[2];
return 0;
}
EDIT:
After your clarification, it seems that the problem is actually that the buffer is not being cleared properly. So just use cin.ignore(); after cin >> t;
Refer to the code below:
#include <iostream>
using namespace std;
int main(){
int t;
cin >>t;
cin.ignore(); // This should clear the input buffer
string s;
getline(cin, s);
cout << s << endl;
return 0;
}

Python3: Popen with infile and outfile doesn't stop at the end of infile

I want to call an interactive command line program (c++ using cin/cout).
I want to define the input through an input file.
I want the output to be written in an output file.
After all the inputs inside the input file are used, the executed program should be killed, even, if it would naturally run on.
Target machine is windows 10.
To achieve this, I tried using the following:
from subprocess import *
with open("input.txt", "r") as ifile:
with open("output.txt","w") as ofile:
p = Popen(["./example.exe"], stdin=ifile, stdout=ofile, stderr=None, universal_newlines=True )
p.wait()
I also tried it with call instead of Popen, but same result.
I also tried p.stdin.write/ readline, but everything I came up with hangs (eg, because the exe program waits inside cin) or mashes up the order.
This is my cpp code for testing:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
string inputString("");
for (int i = 0; i< 200; ++i){
cout << "type a number: " << endl;
cin >> inputString;
if(inputString == "1"){
cout << "eins" << endl;
} else if (inputString == "2"){
cout << "zwei" << endl;
} else if (inputString == "blub"){
break;
}
else{
cout << "unknown" << endl;
}
}
return 0;
}
I would expect this the python code to stop running, after all input lines inside the input file are used.
Instead, the last line gets repeatedly used as input (arbitrary* number of times) or the program gets terminated in the middle.
*arbitrary: Outfile always has exactly 400 lines.

Resources