ในรหัสต่อไปนี้:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string x = "This is C++.";
ofstream of("d:/tester.txt");
of << x;
of.close();
ifstream read("d:/tester.txt");
read >> x;
cout << x << endl ;
}
Output :
This
เนื่องจากตัวดำเนินการ >> อ่านไม่เกินช่องว่างแรกฉันจึงได้ผลลัพธ์นี้ ฉันจะแยกบรรทัดกลับเข้าไปในสตริงได้อย่างไร
ฉันรู้จักรูปแบบนี้ของ istream& getline (char* s, streamsize n );
แต่ฉันต้องการเก็บไว้ในตัวแปรสตริง ฉันจะทำอย่างไร