Develop/C&CPP

fstream 1 basic_ifstream

크레이지J 2018. 6. 7. 15:02
반응형

cpp fstream

include <fstream>


++ basic_ifstream

Input file stream

ios_base <- basic_ios <- basic_istream <- basic_ifstream


+constructor

explicit basic_ifstream(const char * filename, ios_base::openmode mode=ios_base::in) ;

const char *filename 대신 const string &filename도 지원.

ex)

std::ifstream ifs("test.txt", std::ifstream::in) ;

char c = ifs.get() ;

or

std::ifstream ifs ;

ifs.open( "test.txt", std::ifstream::in) ;

char c = ifs.get() ;


+ bool is_open() const ;  // 파일이 열려있는지 체크. true/false

+ void close() ;    // file close


+operator >>

스트림에서 타입에 맞게 읽음.

int n ;

std::cin >> n ;


+get

int_type get() ;

basic_istream& get(char_type &c) ;

basic_istream& get(char_type *s, streamsize n) ;    // n-1까지 읽음. 마지막은 null

basic_istream& get(char_type *s, streamsize n, char_type delim) ;


char str[256] ;
std::cin.get(str, 256) ;


+getline()
라인을 읽음 또는 구분자 까지 읽음.  버퍼크기는 null을 포함한 크기로 지정.
getline(char_type *s, streamsize n) ;
getline(char_type *s, streamsize n, char_type dilim) ;

std::cin.getline(name, 256) ;

+read()

null 체크없이 무조건 크기대로 읽음. 

read(char_type *s, streamsize n) ;

ex)

std::ifstream is("test.txt", std::ifstream::binary) ;

if ( is ) {

   is.seekg(0, is.end) ;    // move to last

 int length = is.tellg();    // file size

is.seekg(0, is.beg) ;    // move to first


char *buf = new char[length] ;

is.read(buf, length) ;

if ( is ) // read ok

else // read only is.gcount() ;

is.close() ;

delete[] buf;

}


+gcount() ; 읽은 데이터 크기

input operation이 읽은 데이터 크기.

std::cin.getline(str, 20) ;

std::cin.gcount() ;    // 실제 읽은 데이터 크기.


+ignore() ; 데이터를 읽고 버림.

ignore(streamsize n=1, int_type delim) ;

n개의 문자가 추출되거나 delim일 때까지 읽고 버림.


+peek() ; 다음 문자를 읽어 리턴하지만 스트림에서 뽑아내지는 않고 내비둠.

ex) 수 또는 문자열 읽기

std::cout << "input number or wrod:";

std::cout.flush() ;    // ouput 버퍼 출력하여 비움.


std::cin >> std::ws ;     // 앞에오는 white space를 지움

std::istream::int_type c ;

c = std::cin.peek() ;    // 문자 한 개를 미리 보기.

if ( c==std::char_traits<char>::eof() ) return 1 ;    // eof

if ( std::isdigit( c ) ) {

int n ;

std::cin >> n ;

} else {

std::string str ;

std::cin >> str ;

}


+putback(char_type c)

문자 하나를 버퍼에 집어 넣고, 마지막 추출 위치를 back 시킨다.

위의 예제와 동일한 기능을 다음과 같이 사용할 수  있다.

char c = std::cin.get() ;

if ( (c>='0') && (c<='9') ) {

   int n ;

   std::cin.putback(c) ;    // 또는 std.cin.unget() ;

   std::cin >> n ;

}


+ tellg    / tellp

tellg ; 입력스트림에서 현재의 위치를 리턴한다. (파일 오프셋이 마지막에 있으면 파일 크기가 된다.)

tellp ; 출력 스트림에서는 tellp를 쓴다. 

is.seekg(0, is.end ) ;

int filesize = is.tellg() ;


+seekg / seekp

seekg ; 입력 스트림에서 추출할 다음 문자 위치를 지정한다.

seekp ; 출력 스트림에서는 seekp를 쓴다.

seekg(pos_type pos) ;    // 시작 위치 기준으로 위치.

seekg(off_type off, ios_base::seekdir_way) ;    // 상대 위치, 기준.

seekdir_way ; ios_base::beg, ios_base::cur, ios_base::end


std::ofstream outfile ;

outfile.open("test.txt") ;

outfile.write("This is an apple", 16) ;

long pos = outfile.tellp() ;    // file size ; 16. ==> point <EOF>

outfile.seekp(pos-7) ;    // ==> point 'n'

outfile.write(" sam", 4) ;

outfile.close() ;    // => This is a sample