hi every body
I have small system for emails which I must save all user in one file and all message in one file :( .
the problem is :
i want the user to write a message and when he end it click Ctrl+d then write Ctrl+d in end of message in the file to Differentiate between other messages .

I hope you will understand what I need.

best wishes .

7 Years
Discussion Span
Last Post by DarkC0de

IMO you'd be better off writing something like <*end*> between messages, rather than Ctrl-D in the file. Why would you want someone to enter Ctrl-D anyway?


if user want to write message with some lines
I will use

getline (cin,text,'Ctrl+Z');

I want to use Ctrl+Z if the user end message click it :)
this Idea

thanks waltp


Control-D represents the end of file. So either read until the end of File,
or Make some special characters to represent the end of fie. As suggested
you can do something like this :

const std::string END = "EOF"
while file.isNotEmpty {
    std::string  content ;
    getline( file, content, END );
   if( content == END ) break;

Just make sure your files ends with "EOF" or whatever.

Votes + Comments

^D terminates input. Hence, you cannot read it from the terminal directly (unless you modify the terminal's input state -- which I don't recommend).

If you are sure you are reading from the terminal, you can simply say:

string text;
  cout << "Please enter a bunch of lines. Press Ctrl-D to stop.\n";
  getline( cin, text, char(0) );  // any non-readable character will do

Thereafter you can continue to use cin.

Here is a moderately useful little function for you:

#include <cctype>
char Ctrl( char c )
  return std::toupper( c ) - 'A';

You can use it above in like manner: getline( cin, text, Ctrl('D') ); . You can use this version to read from your file too. (And don't forget to clear().)

To write a control character to file, just do:

cout << text << Ctrl('D') << flush;

Just be aware that if you want to read or write a ^M or a ^J to file, it must be opened in binary mode.

Be sure to check to see if you are operating from a terminal (human) or from a device (redirected input), using isatty(), and if it is not a terminal, an EOF really means EOF.

Finally, please be aware that such code will not compile/work on foreign systems (such as Windows).

Hope this helps.

Votes + Comments
Nice post
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.