C++檔案I/O樣本

來源:互聯網
上載者:User
文章標題:C
原 作 者:querw
原 出 處:www.vczx.com
發 布 者:querw
發布類型:原創
發布日期:2004-10-05
今日瀏覽:8
總 瀏 覽:144
// 國慶沒事寫著玩的,英文很爛,請把注意力集中在代碼上,呵呵:)

// c++基於流的檔案操作使很多C轉過來的程式員在操作檔案時還是選擇了 FILE*

// :)其實,C++的流操作檔案也很方便啊

// 下面這個例子就是檔案使用執行個體代碼

#pragma warning(disable:4786)
#include <IOSTREAM>
#include <FSTREAM>
#include <string>
using namespace std;
int main()
{
// using std::cout;
// using std::endl;
cout<<" ********************* C ++ file opration demo ***********************"<<endl;
cout<<"using fstream or ifstream and ofstream"<<endl;
cout<<"fstream derive from istream and ostream, so most of method were defined in"<<endl;
cout<<"istream and ostream"<<endl;
cout<<"By the way, I like C style /"FILE */" indeed. :)"<<endl;

// Input file demo
cout<<endl;
cout<<"****Part 1: Input file demo****"<<endl;

fstream /*ifstream*/ infile;
// antoher way to declare special class or function in a namespace
using std::string;
string strfilename;
cout<<"Input In FileName:"<<endl;
cin>>strfilename;
infile.open(strfilename.c_str(),ios::in/*openmode*/);

// succeed?
if(!infile)
{
 cout<<"Cannot open file:"<<strfilename<<endl;
 return 1;
}

// read a byte
char ch1,ch2;
infile.get(ch1);
ch2 = infile.get();

// read a line
// get(char *,int max_size,char dimiliter) can do the same word as getline()
// but get() doesn't drop the dimiliter,
// use ignore() to drop the dimiliter(the last byte,'/n' by default)

char pszLine[1024];
memset(pszLine,0,1024);
infile.getline(pszLine,1024/*read count*/,'/n'/*read until reach '/n'*/);

// seek function
// seekg means seek for getting use for input file
infile.seekg(0/*offsize*/,ios_base::beg/*seek from ios_base::beg ios_base::cur ios_base::end*/);

// read to a buffer
infile.read(pszLine,1024);

// get readed size
int nReadedSize = infile.gcount();

// tell the position also tellg() and tellp()
// tellg() before read() and tellg() after read() then you can get the readed size
// return -1 if eof() turn true
int nCurPos = infile.tellg();

// test end ?
if(infile.eof())
{
 cout<<"input file reach file end"<<endl;
}

infile.close();

// output file demo
cout<<"****Part 2: Output file demo *****"<<endl;

fstream /*ofstream */outfile;
outfile.open("out.txt",ios::out);
if(!outfile)
{
 cout<<"Cannot open out file out.txt."<<endl;
 return 1;
}

// write a byte
char chout = 'a';
outfile.put(chout);

// write buffer
outfile.write(pszLine,strlen(pszLine));

// like ifstream ,seekp(),tellp()
outfile.seekp(10,ios_base::beg);
char buffer[] = "seekp and write a buffer";
outfile.write(buffer,strlen(buffer));

nCurPos = outfile.tellp();

outfile.close();

return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.