given the following code snippet:
int num;
for (;;) {
cout << "enter number: ";
cin >> num;
//cin.clear();
while (fgets(line, MAXLINE, stdin) != NULL) {
after input is performed by cin, fgets doesnt block whereas if cin was
not called, then fgets would block until it receives input from stdin
how can i set the state of stdin so that after cin is called, fgets
blocks as expected?
Charles 4 6944
"Charles Wilkins" <2boxers_at_comcast.net> wrote in message
news:et********************************@4ax.com... given the following code snippet:
int num;
for (;;) { cout << "enter number: "; cin >> num; //cin.clear();
while (fgets(line, MAXLINE, stdin) != NULL) {
after input is performed by cin, fgets doesnt block whereas if cin was not called, then fgets would block until it receives input from stdin
how can i set the state of stdin so that after cin is called, fgets blocks as expected?
Charles
What's expected? It doesn't block because there is still an unread newline
after the number. I/O in C and C++ is character based not line based. Use
cin.ignore(INT_MAX, '\n');
to ignore all pending input up to the next newline. #include <limits.h> for
the INT_MAX constant.
john
On Thu, 7 Aug 2003 09:12:02 +0100, "John Harrison"
<jo*************@hotmail.com> wrote: "Charles Wilkins" <2boxers_at_comcast.net> wrote in message news:et********************************@4ax.com.. . given the following code snippet:
int num;
for (;;) { cout << "enter number: "; cin >> num; //cin.clear();
while (fgets(line, MAXLINE, stdin) != NULL) {
after input is performed by cin, fgets doesnt block whereas if cin was not called, then fgets would block until it receives input from stdin
how can i set the state of stdin so that after cin is called, fgets blocks as expected?
Charles
What's expected? It doesn't block because there is still an unread newline after the number. I/O in C and C++ is character based not line based. Use
cin.ignore(INT_MAX, '\n');
to ignore all pending input up to the next newline. #include <limits.h> for the INT_MAX constant.
john
this did what i was looking for. thank you.
Charles Wilkins wrote: given the following code snippet:
int num;
for (;;) { cout << "enter number: "; cin >> num; //cin.clear();
while (fgets(line, MAXLINE, stdin) != NULL) {
after input is performed by cin, fgets doesnt block whereas if cin was not called, then fgets would block until it receives input from stdin
Aside from the comments made else-thread, a more fundumental question
is, why are you mixing stream and file I/O paradigms? Why not just use
one or the other?
/david
--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
On Thu, 07 Aug 2003 08:55:24 GMT, to********@hotmail.com (tom_usenet)
wrote: On Thu, 07 Aug 2003 03:33:48 -0400, Charles Wilkins <2boxers_at_comcast.net> wrote:
given the following code snippet:
int num;
for (;;) { cout << "enter number: "; cin >> num; //cin.clear();
while (fgets(line, MAXLINE, stdin) != NULL) {
after input is performed by cin, fgets doesnt block whereas if cin was not called, then fgets would block until it receives input from stdin
how can i set the state of stdin so that after cin is called, fgets blocks as expected?
The problem is that, after your call to cin >> num, you've left a newline character in the stream, which fgets reads in immediately and returns. So all you need to do if empty that newline from the stream:
cin >> num; cin.ignore(std::numeric_limits<int>::max(), '\n');
Also, why are you mixing C stdio and iostreams? It will work, but what's wrong with getline? e.g.
std::string line; while (std::getline(std::cin, line)) {
Note you still need the ignore code, since whether you are using stdio or iostreams, the rest of the last line that you passed to the terminal will still be there.
Tom
nothing is wrong with getline.
the fgets loop was part of an older model.
i simply was adding to this model as part of an exercise. for this
purpose I chose cin as a matter of preference.
beyond the scope of this experiment / exercise, i do not intentionally
seek to mix C stdio and iostreams
thanks,
Charles This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tanel |
last post by:
Hello,
I need to read a result of the first script (that takes some time to
run) from the second script so that the first script doesn't block the
second. Here is a short example. do_smth_else()...
|
by: Charles Erwin |
last post by:
Is there any way, upon scanning in a file line by line to avoid
missing the last line if there is not a newline character (aka you
have to hit return on the last line of input in your file). I was...
|
by: Rob Somers |
last post by:
Hey all
I am writing a program to keep track of expenses and so on - it is not
a school project, I am learning C as a hobby - At any rate, I am new
to structs and reading and writing to files,...
|
by: TTroy |
last post by:
Hello,
I have found some peculiar behaviour in the fgets runtime library
function for my compiler/OS/platform (Dev C++/XP/P4) - making a C
console program (which runs in a CMD.exe shell).
The...
|
by: David Mathog |
last post by:
Every so often one of my fgets() based programs encounters
an input file containing embedded nulls. fgets is happy to
read these but the embedded nulls subsequently cause problems
elsewhere in...
|
by: santosh |
last post by:
Hi,
A book that I'm currently using notes that the fgets() function does
not
return until Return is pressed or an EOF or other error is encountered.
It then at most (in the absence of...
|
by: FireHead |
last post by:
Hello C World & Fanatics
I am trying replace fgets and provide a equavivalant function of
BufferedInputReader::readLine.
I am calling this readLine function as get_Stream.
In the line 4 where...
|
by: uidzer0 |
last post by:
Hey everyone,
Taken the following code; is there a "proper" or dynamic way to
allocate the length of line?
#include <stdio.h>
#include <errno.h>
int main(int argc, char **argv) {
FILE *fp;
|
by: Sheth Raxit |
last post by:
Machine 1 :
bash-3.00$ uname -a
SunOS <hostname5.10 Generic_118822-30 sun4u sparc SUNW,Sun-Fire-280R
bash-3.00$ gcc -v
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/2.95.3/...
|
by: Bill Cunningham |
last post by:
I was talking with someone about fgets and he said that fgets puts the
\n in a string but not \0. I decided to test this assumption because my
documentation didn't say if fgets put \0 after a...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |