-1

I would like to << stream to save all inputs from console into a file Here is how I tried

ofstream of("file.txt");
while(1)
{
   string str;
   cin>>str;
   of<<str;   
}

I don't see the non-English characters in the file (Edit: I mean they are Japanese, Chinese or Korean etc)

Dalton
  • 181
  • 1
  • 8
  • Please clarify. Do you mean that you typed "non-English" characters into the console? – Oliver Charlesworth Aug 13 '11 at 19:43
  • They are probably encoded as UTF-8. Read the file back into memory and print them on the console. If the console is set up correctly you will see the the non ASCII characters printed as expected. – Martin York Aug 13 '11 at 19:44
  • @Martin and Oli, thanks alot, I will try out what you suggest. I mean Korean, Arabic etc characters. – Dalton Aug 13 '11 at 20:01

3 Answers3

0

You could stream char by char. Then it would be a true binary copy.

ofstream of("file.txt");
while(1)
{
   char c;
   cin>>c;
   of<<c;   
}
Adi
  • 144
  • 1
  • 5
  • Thanks Adi, that's nice. I am learning the basics of stream, that is helpful. – Dalton Aug 13 '11 at 20:04
  • @Dalton: This is not the solution you were looking for. It doesn't work (try it and see). – David Hammen Aug 13 '11 at 23:24
  • If this is considered incorrect then please downvote and optionally leave a comment explaining why. – Kev Aug 13 '11 at 23:42
  • -1: There are three problems here: (1) That `while(1)`. The program doesn't terminate and doesn't respond to EOF in the input stream. (2) `cin>>c`. This skips over whitespace in the input file. It is not a true binary copy. (3) The OP asked for the ability to handle wide characters. This doesn't. – David Hammen Aug 13 '11 at 23:45
  • Yes right. I looked a little bit around, and i came to the point that a correct binary copy is not possible using the std::cin stream. At least not with the stream operators. The read member method could possibly help. – Adi Aug 14 '11 at 01:14
0

Streaming using the formatted extraction operator is a poor choice. Specifically, it will consume all of the white space.

If it were me, I would copy using std::getline or istream::rdbuf:

std::getline:

// Copy standard input to named file, one line at a time.
#include <iostream>
#include <fstream>

int main (int argc, char **argv) {
  std::string s;
  std::ofstream of(argv[1]);
  while(std::getline(std::cin, s)) {
    of << s << "\n";
  }
}

istream::rdbuf:

// Copy entire standard input stream to named file in one go
#include <iostream>
#include <fstream>

int main (int argc, char **argv) {
  std::ofstream(argv[1]) << std::cin.rdbuf();
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

Just a couple of points as a starter:

ofstream of("file.txt");

If you want to see Japanese, Chinese or Korean character you should not be using an ofstream here. You want a stream that writes wide characters: a std::wofstream. You will also haveendow that stream with a locale. See Why does wide file-stream in C++ narrow written data by default? for details.

Another point: You apparently are have a using namespace std;. You can find many questions here at Stack Overflow that indicate that this is a bad idea. Typing those extra five characters isn't very hard, it avoids problems with names from the standard library polluting your namespace, and it makes the code clearer.

while(1)

Your loop doesn't have any break statements to escape the loop, so this plus the while (1) means your program will never stop. It is just going to keep on going and going and going and going. You want it to stop (or should want it to stop) on encountering an error or end of file in the input stream.

A better approach is to use a construct such as

while (std::getline (std::cin, s))

to control the loop (except you need to use something special to get wide characters).

Community
  • 1
  • 1
David Hammen
  • 32,454
  • 9
  • 60
  • 108