반응형
ifstream에서 문자열 변수로 줄 읽기
다음 코드에서 :
#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 );
있지만 문자열 변수에 저장하고 싶습니다. 어떻게 할 수 있습니까?
를 사용 std::getline()
에서 <string>
.
istream & getline(istream & is,std::string& str)
따라서 귀하의 경우에는 다음과 같습니다.
std::getline(read,x);
참고 URL : https://stackoverflow.com/questions/6663131/reading-a-line-from-ifstream-into-a-string-variable
반응형
'Program Tip' 카테고리의 다른 글
Postgresql의 where 절에서 별칭 열 사용 (0) | 2020.12.08 |
---|---|
Eclipse 프로젝트에서 경고 / 오류를 생성하는 폴더를 제외하는 방법은 무엇입니까? (0) | 2020.12.08 |
Linux에서 가장 높은 우선 순위 인 실시간 우선 순위 (0) | 2020.12.08 |
Android : VideoView에 onClickListener를 제공 할 수없는 이유는 무엇입니까? (0) | 2020.12.07 |
Internet Explorer 8에서 innerWidth를 가져 오는 방법 (0) | 2020.12.07 |