본문 바로가기

C++

텍스트 파일 통째로 std::string에 넣기

728x90

로 스트링 리터럴(raw string literal)을 사용하면 됩니다.

띄어쓰기, 행 바꿈 문자, 꺽쇠(< , >), 괄호 등의 특수 문자를 그대로 std::string 타입의 변수에 넣을 수 있습니다.

xml파일을 로 스트링 리터럴을 활용하여 헤더에 넣어 사용한 적이 있습니다.

\t, \n 같이 시퀀스들은 텍스트로 그냥 출력합니다.

 

코드 예제

std::string rawstring = R"(<annotation>
	<folder></folder>
	<filename>1.jpg</filename>
	<path>1.jpg</path>
	<source>
		<database></database>
	</source>
	<size>
		<width>1920</width>
		<height>1080</height>
		<depth>3</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>flower</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<occluded>0</occluded>
		<bndbox>
			<xmin>200</xmin>
			<xmax>400</xmax>
			<ymin>300</ymin>
			<ymax>600</ymax>
		</bndbox>
	</object>
</annotation>
)";

 

'C++' 카테고리의 다른 글

C++의 기초(Hello, World)  (0) 2022.03.12
폴더에서 파일 경로 불러오기  (0) 2022.03.06
Visual Studio 2022 + OpenCV4.5.4(prebuilt)  (1) 2021.11.14
용어(Effective C++)  (0) 2021.11.04
C++11 실행 시간 체크(Chrono library)  (0) 2021.09.14