ofstream, C++ Fileout

C++ 형태의 ofstream 쓰기


파일 입출력을 하기위한 옵션!

Parameters

[Winapi Site]
 

모드

설명

ios_base::out

출력용으로 파일을 연다.

ios_base::in

입력용으로 파일을 연다.

ios_base::app

파일 끝에 데이터를 덧붙인다데이터를 추가하는 것만 가능하다.

ios_base::ate

파일을 열자 마자 파일 끝으로 FP 보낸다. FP 임의 위치로 옮길  있다.

ios_base::trunc

파일이 이미 존재할 경우 크기를 0으로 만든다.

ios_base::binary

이진 파일 모드로 연다.

 

[CPlusPlus]

 

filename
C-string containing the name of the file to be opened.
mode
Flags describing the requested i/o mode for the file. This is an object of type ios_base::openmode, which consists on a combination of one or more of the following flags defined as member constants:
flag valueopening mode
app (append) Set the stream's position indicator to the end of the stream before each output operation.
ate (at end) Set the stream's position indicator to the end of the stream on opening.
binary (binary) Consider stream as binary rather than text.
in (input) Allow input operations on the stream.
out (output) Allow output operations on the stream.
trunc (truncate) Any current content is discarded, assuming a length of zero on opening.

 
Example 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// opening and closing a file
#include <iostream>
#include <fstream>
using namespace std;

int main () {

  ofstream outfile;

  outfile.open ("test.txt");

  // >> i/o operations here <<

  outfile.close();
  return 0;
}




참고, C++ 자리수 맞게 출력하는 방법

난 이래 푼다~

void CLSDManager::StorageResultofSVD( CvMat *_v )
{
	ofstream fout("ResultofSVD.txt", ios::app ); // Append

	double x,y,z;
	x  = cvmGet(_v, 0, 2 );
	y  = cvmGet(_v, 1, 2 );
	z  = cvmGet(_v, 2, 2 );	

	fout << fixed;
	fout << "X =";		
	fout.width(8);
	fout.precision(5);
	fout << x;

	fout << ", Y =";		
	fout.width(8);
	fout.precision(5);
	fout << y;

	fout << ", Z =";		
	fout.width(8);
	fout.precision(5);
	fout << z << endl;	
	
	fout.close();
}






 


댓글

Designed by JB FACTORY