Code Linh Tinh

1. Add a character '\n' into a string

Today, i want to teach you how to add a character '\n' at the end of string. It is very easy.


string str;
while (!file.eof())
{
 getline(file, str);
 str.resize(str.size() + 1);
 str[str.size() - 1] = '\n';
 cout << str;
}


2. What is different between function int get() and istream& get(char &c)

Mục Tiêu: Đọc từng ký tự xuất ra console
Vấn Đề: File.get(ch); //Bị lỗi lặp vô hạn
              ch = File.get(); // OK



#include <iostream>
#include <fstream>

using namespace std;

void main()
{
 fstream File("input.txt", ios::in | ios::out);
 char ch;
 while (1)
 {
  ch = File.get();    // OK
  //File.get(ch);      //Bị lỗi lặp vô hạn
  cout << ch;
  if (ch == EOF) break;
 }
 system("pause");
}
Giải Đáp: Hàm File.get(ch) không đọc EOF và vẫn giữ nguyên ch trước đó

The overload that doesn't take arguments will return EOF when the read fails. The other overloads don't affect the passed parameter



























Share on Google Plus

About Unknown

Phan Quang nguyen
    Blogger Comment
    Facebook Comment

0 nhận xét:

Đăng nhận xét