In kí tự "\n" ra file

Hi mọi người !
Em muốn in kí tự \n ra màn hình hoặc ra file thì làm sao ạ ?
VD : trong file txt hiển thị là: 1\n2\n3\n.
Em sử dụng :

#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream file;
  file.open ("codebind.txt");
  file << "1\n2\n3\n";
  file.close();
  return 0;
} 

Em viết vậy nó toàn xuống dòng :frowning:. Anh chị nào có cao kiến giúp em với.
Em cảm ơn nhiều

Lười.

cout << "\\" << "n" << endl;

Tinh khôn hơn 1 tí:

cout << "1\\n\\n2\\n3" << endl;
1 Like

Em cảm ơn nhiều :smiley:

Để in ra ký tự '\', ta dùng "\\"

2 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?