Computer/C++
ofstream 파일 출력시 stream position 처리
Gu Youn
2007. 6. 17. 00:37
1. 상황 출력할 자료의 개수를 모르는 상태에서 "값 + 구분자"로 출력을 하고 각 자료는 개행으로 구분을 한다면, 마지막 값에 구분자가 포함되어 개행문자 앞에 필요없는 구분자가 들어간 상태가 됨으로 마지막 값 뒤에 붙은 구분자를 제거해야 한다. 2. 개념 stream position을 이동하여 마지막 필요없는 구분자에 개행문자를 출력한다. 3. 소스 ofstream out("Output.txt"); //out.open("Output.txt"); for(~~~) { while(!ComponentQueue->IsEmpty()) { CQueueElement element = ComponentQueue->Dequeue(); out << element.Vertex << "+"; } //stream position 변경 부분 std::streamoff i = out.tellp(); out.seekp(i-1); //마지막에 출력한 공백의 위치로 stream poisition이 이동한다. out << std::endl; } 4. 출력 예 ㄱ. stream position 변경 코드 적용 전의 결과 0+2+5+8+7+ 1+ 3+4+6+ ㄴ. stream position 적용 결과 0+2+5+8+7 1 3+4+6 |