• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

C++文件I/O示例

发布: 2007-7-01 20:40 | 作者: admin | 来源: | 查看: 15次 | 进入软件测试论坛讨论

领测软件测试网

// 国庆没事写着玩的,英文很烂,请把注意力集中在代码上,呵呵:)

// 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;
}

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网