By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Save Input from keyboard to file

P: n/a
Hello all,

I;m a beginner c++, and I;ve a problem... I made a tokenizer (that part was
no problem).

But now I need to catch keyboard input and put it in a file. I already made
code to write lines to file, and create a file.

But now comes the diffecult part: I need to catch keyboardinput until End Of
Input, and then write it to a txt file.

Does anyone know how to do this. I know the scanf function, but I guess that
one isn't working for this ?

Can anyone help me ? It must be very easy... but I;m getting crazy out
here.. I already searched the internet... but couldn;t find a good example
or something


Jun 13 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Joah Senegal wrote:
Hello all,

I;m a beginner c++, and I;ve a problem... I made a tokenizer (that part was
no problem).

But now I need to catch keyboard input and put it in a file. I already made
code to write lines to file, and create a file.

But now comes the diffecult part: I need to catch keyboardinput until End Of
Input, and then write it to a txt file.

Does anyone know how to do this. I know the scanf function, but I guess that
one isn't working for this ?

Can anyone help me ? It must be very easy... but I;m getting crazy out
here.. I already searched the internet... but couldn;t find a good example
or something


It might look something like this (off the cuff):

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ofstream file( "output.txt" );
if( !file )
{
cerr << "Unable to open file!\n";
return -1;
}

string str;
while( getline( cin, str ) )
{
file << str << '\n';
}
}

Compare this FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-15.4

Cheers! --M

Jun 13 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.