If you can't read please download the document
Upload
zephr-chan
View
77
Download
9
Embed Size (px)
DESCRIPTION
第 9 章 C++ 的文件操作. “ 文件 ” ,一般是指内存以外的存储介质上一批数据的集合。 C++ 在语言层次上提供了文件操作的一系列函数用于完成文件的操作,打开、关闭文件,读取、写入文件数据等操作。. 1 。字符文件与二进制文件 字符文件 :又称 ASCII 文件或文本 TEXT 文件,它是以一个字节存放一个 ASCII 码,代表一个字符。例如, 32767 需要使用 5 个字节表示,即 : 51 50 55 54 55 ( 十进制表示的 ASCII) ,而浮点数 136.56 需要使用 6 个字节表示,即 49 51 54 46 53 54 。. - PowerPoint PPT Presentation
Citation preview
9 C++ C++ 1 ASCIITEXTASCII327675: 51 50 55 54 55 (ASCII)136.56649 51 54 46 53 54
136.56float44float4 011111111111111101111111111111110011001100111110001101110011011000110111ASCII3276732767
ASCII,,,, 2
3 C++fstream.hifstream ofstreamfstream
C++(1). ifstream infile; ofstream outfile; fstream iofile;
ifstream infile;ofsream outfile;fstream file;infile.open(myfile_in.txt); //outfile.open(myfile_out.txt);//file.open(myfile.txt, ios::in);// ifstream infile(myfile_in.txt);ofstream outfile(myfile_out.txt);fstream file(myfile.txt, ios::in);
(2)
ios.henum open_mode{in = 0x01, //00000001out = 0x02,//00000010ate = 0x04,//00000100app = 0x08,//00001000 trunc = 0x10,//00010000 nocreate = ox20,//00100000noreplace = 0x40,//01000000binary = 0x80//10000000};
|ios::out | ios::ate //ios::in | ios::binary//ios::out | ios::trunc//
ios::inios::outios::app ios::ateios::inios::ateios::appios::trunc0ios::outios::app, ios::ate, ios::inios::nocreateios::noreplaceios::ateios::appios::binary
:ifstreamopenopen(const char *, int = ios::in, int = filebuf::openprot);ios::in;
ofstreamopenopen(const char *, int = ios::out, int = filebuf::openprot);ios::out;
fstreamopenopen(const char *, int, int = filebuf::openprot);(3) close
ifstreamofstreamfstreamclose( ); infile.close( ), outfile.close( ), file.close( ); (4) 0
ifstream f1(file.dat);if(!f1){cout
2 char ch; // while(infile.get(ch)) // outfile.put(ch); //ch infile.close( ); outfile.close( );
3 char buff[300]; while(infile.getline(buff,300)) //\noutfile
6C++ C++read( )write( )read(char *buf, int size); sizebufwrite(char *buf, int size);bufsize
4While(!infile.eof( )){infile.read(buf, 4096);outfile.write(buf, 4096);} //eof( )00
7 C++C++istreamostreamistream seekg(long streampos);stramposseekg(long streamoff, seek_dir);seek_dirstreamofftellg( );
ostream seekp(long streampos);stramposseekp(long streamoff, seek_dir);seek_dirstreamofftellp( );
Seek_dirios::beg0ios::cur1ios::end2
infile.seekg(500);//500infile.seekg(-100,ios::cur); //100infile.seekg(100,ios::cur);//100outfile.seekp(-100,ios::end);//100
1.streamoff 2. 0