종종 별다른 구조없이 줄바꿈과 공백만으로 구분되는 텍스트 데이터를 읽을 필요가 있다.
읽는 사람(프로그램)을 배려하지 않은 무례한 형식이라고도 할 수 있는데,
이런 형식을 읽는 간단한 방법을 프로그램 예제를 통해서 소개한다.
바보같은 읽기용 실험 프로그램을 만드는 수고정도는 덜어줄 수 있을거라고 기대한다
#include파일내용#include int main(int argc, char** argv) { std::ifstream stream; stream.open("sample.txt", std::ios::binary); if(stream.is_open() == false) return 0; int num_var(7); while(true) { int id; stream >> id; if(stream.eof()) break; std::cout << id << '\t'; for(int i=0; i > v; std::cout << v << '\t'; } std::cout << std::endl; std::ifstream::pos_type pos = stream.tellg(); std::ifstream::int_type character = stream.get(); if(character == '\r') character = stream.get(); if(character == '\n') { if(stream.eof()) break; if(stream.fail()) break; //첫번째 줄바꿈 character = stream.get(); if(character == '\r') character = stream.get(); if(character == '\n') { //두번째 줄바꿈(연속 줄바꿈) std::cout << "two line feeds found" << std::endl; } } stream.seekg(pos); } }
ID와 7개의 데이터가 있는 형식
반복 사이에 빈 줄이 하나 들어가는 매우 무례한 형식이다.
9648 -0.95013864E-02 0.37615862E-01 0.35936206E-02 0.10000000E+04 0.23387932E+02 0.24184522E-03 0.48205384E-05 9649 0.15433150E-01 0.45755006E-01 0.27949386E-02 0.10000000E+04 0.24999840E+02 0.26961154E-03 0.27396534E-05 9650 0.70509665E-01 -0.28248650E+00 -0.35628763E+00 0.10000000E+04 0.12957797E+03 0.56852810E-02 0.65976667E-03 9651 -0.70090354E-02 -0.40278405E-01 0.11953050E-02 0.10000000E+04 0.25439789E+02 0.38995227E-03 0.16247226E-05 9648 -0.95013864E-02 0.37615862E-01 0.35936206E-02 0.10000000E+04 0.23387932E+02 0.24184522E-03 0.48205384E-05 9649 0.15433150E-01 0.45755006E-01 0.27949386E-02 0.10000000E+04 0.24999840E+02 0.26961154E-03 0.27396534E-05 9650 0.70509665E-01 -0.28248650E+00 -0.35628763E+00 0.10000000E+04 0.12957797E+03 0.56852810E-02 0.65976667E-03 9651 -0.70090354E-02 -0.40278405E-01 0.11953050E-02 0.10000000E+04 0.25439789E+02 0.38995227E-03 0.16247226E-05