473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to handle Control-D in getline ?

Hi,
I have a program where I read a lot of user data. How can I handle
the case where the user press control-d ?
Is it possible to disable control-d or re-open the cin ?
I have made a small example which hopefully shows my problem.
Regards
Martin
#include <string>
#include <iostream>

int main()
{
std::string command;
while (true) {
getline(std::cin,command);
std::cout << std::cin.eof() << " " << command << std::endl;
}
}
===============================

tusk@skarbinika:~/programming/jiga/jiga/getinput$ ./getinput
hello world
0 hello world
1
1
1
[...continues, until I kill the program...]

Apr 9 '07 #1
2 13206
Martin M. Pedersen wrote:
Hi,
I have a program where I read a lot of user data. How can I handle the
case where the user press control-d ?
Is it possible to disable control-d or re-open the cin ?
I have made a small example which hopefully shows my problem.
Regards
Martin
#include <string>
#include <iostream>

int main()
{
std::string command;
while (true) {
getline(std::cin,command);
std::cout << std::cin.eof() << " " << command << std::endl;
}
}
===============================

tusk@skarbinika:~/programming/jiga/jiga/getinput$ ./getinput
hello world
0 hello world
1
1
1
[...continues, until I kill the program...]
Oops. Found the solution myself.

#include <string>
#include <iostream>

int main()
{
std::string command;
while (true) {
getline(std::cin,command);
if (std::cin.eof()==1) {
std::cin.clear();
std::cin.ignore();
continue;
}
std::cout << std::cin.eof() << " " << command << std::endl;
}
}

Apr 9 '07 #2
On Apr 9, 11:25 pm, "Martin M. Pedersen" <traxpla...@gmail.comwrote:
Martin M. Pedersen wrote:
I have a program where I read a lot of user data. How can I handle the
case where the user press control-d ?
Is it possible to disable control-d or re-open the cin ?
I have made a small example which hopefully shows my problem.
#include <string>
#include <iostream>
int main()
{
std::string command;
while (true) {
getline(std::cin,command);
std::cout << std::cin.eof() << " " << command << std::endl;
}
}
===============================
tusk@skarbinika:~/programming/jiga/jiga/getinput$ ./getinput
hello world
0 hello world
1
1
1
[...continues, until I kill the program...]
Isn't that what happens anyway? I can't think of many things
that would make true evaluate to false, so the program is
obviously designed to run forever.

Judging from your prompt (and the mention of control D), you're
obviously under Unix, so stty can be used to disable ^D. But
I'm not sure that that's what you want. Normally, ^D on an
empty line is the Unix convention for an end of file; something
like:

while ( getline( std::cin, command ) ) {
std::cout << " " << command << std::endl ;
}

handles that quite well. (Short of using stty, I'm not sure
that you can handle it other than at the beginning of the line.)
Oops. Found the solution myself.
#include <string>
#include <iostream>
int main()
{
std::string command;
while (true) {
getline(std::cin,command);
if (std::cin.eof()==1) {
Be careful here. If the input is redirected from a file, and
that file doesn't end in a newline, you'll throw out all data
from the last newline until the end of file.
std::cin.clear();
std::cin.ignore();
I'm not sure what you're actually ignoring here, are you? In
the usual configuration, ^D never makes it to the process. If
the ^D was at the beginning of a line, this line will read one
character (blocking until it gets it), and then throw it away.
continue;
}
std::cout << std::cin.eof() << " " << command << std::endl;
}
}
I rather doubt that just ignoring ^D is a good idea. More
likely, you're users would want it to be interpreted as an end
of file. And of course, the poor user who redirects input from
a file will get an endless loop in your case. My own solution
would be something along the lines:

while ( getline( std::cin, command ) ) {
process( command ) ;
}
if ( std::cin.gcount() 0 ) {
generateWarning( "File did not end with newline" ) ;
process( command ) ;
}

(This should also work under Windows, in a console window, with
^Z instead of ^D as the interactive end of file.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 10 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Altramagnus | last post by:
I have 30 - 40 type of different window. For each type I need about 20 instances of the window. When I try to create them, I get "Error creating window handle" My guess is there is a maximum...
0
by: Matt Warner | last post by:
Hi guys, A couple of people have already posted questions about similar issues but haven't had any response. Occasionally, sometimes after running the app for a few hours, it bombs out saying...
6
by: DraguVaso | last post by:
Hi, In my application, on some given actions while debugging in Visual Studio, I suddenly get a "System.ComponentModel.Win32Exception was unhandled" Message="Error creating window handle."...
3
by: Daniel H. | last post by:
Hi! When I create a new form programatically, I can save its handle in a collection of some type Later, I need to find that form based on a handle I saved and call one of its functions. The newly...
3
by: Frank Perry | last post by:
Howdy, I'm trying to write data out the com port. I have taken the code from the sample on the MSDN Library CD and used the parts that seem relevant. I can open the com port with CreateFile...
2
by: Fred Walker | last post by:
I am trying to figure out how to get a reference to a user control in the page that is using that control I have created several "Properties" within my UserControl that I want to access. Normally...
13
by: Abhishek | last post by:
Hi, how do I pass the handle of a control to the win32 api mouse_event. so that it will create the click event on that application only even if there is any other window in front of it. I dont...
16
by: ink | last post by:
Hi all, If I have a Windows 32 pointer to and object (Handle) and I know what that object is (Button) can I some how cast that pointer to a type of System.Windows.Forms.Button and then use its...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.