C++ IO操作API及注意事项(包含一个日志类的实现)

C++是一个抽象程度比C高很多的语言,在使用C++时,编译器做了很多工作,如果我们不对C++的某些特性的实现机制进行了解,那么编程时也许会有很多疑惑,我们也许知道怎样做才是正确的,但不知道为什么要这样做,所以,学习C++时,尽量多了解下底层实现机制,多看看操作系统相关方面的知识,对我们无论是学习某个编程语言,还是弄懂程序的运行原理都是非常有益的。IO操作是属于操作系统的,并不是属于C++的,C++只是提供了一个IO操作的编程接口的标准,不同的操作系统可能有着不同的IO操作接口,但是都可以根据这些操作提供一个符合C++ IO标准的库,这样就可以在不同的操作系统上,使用C++ 统一的IO操作。

C++ IO类之间的关系

头文件 类型
iostream istream,wistream 从流中读取数据,w开头的针对wchar_t类型的
iostream ostream,wostream 向流写入数据
iostream iostream,wiostream 读写流

fstream ifstream,wifstream
fstream ofstream,wofstream
fstream fstream,wfstream

sstream istringstream,wistringstream
sstream ostringstream,wostringstream
sstream stringstream,wstringstream

输入流均继承自istream,输出流均继承自ostream,

IO对象无拷贝或赋值

ofstream out1,out2,
out1=out2; //错误,不能对IO对象赋值
ofstream out3(out2); //错误,不能用IO对象初始化IO对象

IO对象有条件状态

badbit 代表流已崩溃
failbit 代表IO操作失败
eofbit 代表流达到文件尾
goodbit 代表流未出错
s.eof() 若eofbit置位则返回true
s.fail()
s.bad()
s.good()
s.clear() 将流中所有条件状态位复位,使流有效,返回值为void
s.clear(flags) 根据flags将条件状态位置位 cin.clear(cin.rdstate() & ~cin.faibit & ~cin.badbit) 将failbit和badbit复位
s.setstate(flags) 设置流的状态
s.rdstate() 返 回流的当前状态 返回类型为strm::iostate

IO操作

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ofstream myfile;
    myfile.open("example.txt");
    myfile<<"this is a statement!\n";
    myfile.close();
    return 0;
}

open (filename,mode)

文件模式
ios::in 以读的方式打开
ios::out 以写的方式打开
ios::app 以追加的方式打开,每次写操作均定位到文件末尾
ios::ate 打开文件后,立即定位到文件末尾
ios::trunc 截断文件
ios::binary 以二进制方式进行IO

out模式只能对ofstream或fstream设定
in模式只能对ifstream或fstream设定
默认情况下以out模式打开的文件同时包含trunc模式,即输出流若没指定app模式,文件中的内容会被清空。
app和ate模式有很大的区别,具体区别看这里
is_open() 可以用来判断打开文件是否成功,成功返回true;

设置流指针的位置

  • 获得位置
    streampos tellg(); //g代表该函数的操作对象是输入流,
    streampos tellp(); //p代表该函数的操作对象是输出流

  • 设置位置
    seekg(position);
    seekp(position);
    seekg(offset,direction); //相对于direction方向的偏移量,可以为:ios::end, ios::beg, ios::cur
    seekp(offset,direction);

ios::beg offset counted from the beginning of the stream
ios::cur offset counted from the current position
ios::end offset counted from the end of the stream

#include <iostream>
#include <fstream>
using namespace std;

int main () {
  streampos begin,end;
  ifstream myfile ("example.bin", ios::binary);
  begin = myfile.tellg();
  myfile.seekg (0, ios::end);
  end = myfile.tellg();
  myfile.close();
  cout << "size is: " << (end-begin) << " bytes.\n";
  return 0;
} 

read和write

read(memblock,size);
write(memblock,size);

#include <iostream>
#include <fstream>
using namespace std;

int main () {
  streampos size;
  char * memblock;

  ifstream file ("example.bin", ios::in|ios::binary|ios::ate);
  if (file.is_open())
  {
    size = file.tellg();
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);
    file.close();

    cout << "the entire file content is in memory";

    delete[] memblock;
  }
  else cout << "Unable to open file";
  return 0;
}

getline()

  输入流对象调用的函数
  #include <fstream>
  istream& getline( char* buffer, streamsize num );
  istream& getline( char* buffer, streamsize num, char delim );

  #include <string>
  istream& getline( istream& is, string& s, char delimiter = '\n' );
    
#include <iostream>
#define MAX 100
using namespace std;
int main()
{
    char cstr[MAX];
    string str;
    cin.getline(cstr,MAX);
    cout<<cstr<<endl;
    getline(cin,str);
    cout<<str<<endl;
    return 0;
}

自己实现一个日志类

#include <iostream>
#include <fstream>
#include <memory>
#include <ctime>
using namespace std;

class EasyLog{
public:
    static EasyLog* getInstance(){
        if(_instance==NULL){
            _instance=new EasyLog();
        }
        return _instance;
    }
    void Log(const string &str,const string& filepath);
private:
    EasyLog(){}
    virtual ~EasyLog(){delete _instance;}  
    static EasyLog* _instance;
};

EasyLog* EasyLog::_instance;
void EasyLog::Log(const string& str,const string& filepath){
    time_t t;
    t=time(0);
    char tmp[100];
    strftime(tmp,sizeof(tmp),"[%Y.%m.%d %X %A]",localtime(&t));
    ofstream out(filepath,ios::app);
    out<<tmp<<" : "<<str<<endl;
    return ;
}

int main(){
    EasyLog::getInstance()->Log("This is a test statement","log.txt");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
 一、ASCII 输出   为了使用下面的方法, 你必须包含头文件<fstream.h>(译者注:在标准C++中,已经使用<fstream>取 代< fstream.h>,所有的C++标准头文件都是无后缀的。)。这是 <iostream.h>的一个扩展集, 提供有缓 冲的文件输入输出操作. 事实上, <iostream.h> 已经被<fstream.h>包含了, 所以你不必包含所有这两个 文件, 如果你想显式包含他们,那随便你。我们从文件操作的设计开始, 我会讲解如何进行ASCII I/O 操作。如果你猜是"fstream," 恭喜你答对了! 但这篇文章介绍的方法,我们分别使用"ifstream"?和 "ofstream" 来作输入输出。   如果你用过标准控制台流"cin"?和 "cout," 那现在的事情对你来说很简单。 我们现在开始讲输出部 分,首先声明一个对象。 ofstream fout;   这就可以了,不过你要打开一个文件的话, 必须像这样调用ofstream::open()。 fout.open("output.txt");   你也可以把文件名作为构造参数来打开一个文件. ofstream fout("output.txt");   这是我们使用的方法, 因为这样创建和打开一个文件看起来更简单. 顺便说一句, 如果你要打开的文 件不存在,它会为你创建一个, 所以不用担心文件创建的问题. 现在就输出到文件,看起来和"cout"的操 作很像。 对不了解控制台输出"cout"的人, 这里有个例子。 int num = 150; char name[] = "John Doe"; fout << "Here is a number: " << num << " "; fout << "Now here is a string: " << name << " ";   现在保存文件,你必须关闭文件,或者回写文件缓冲. 文件关闭之后就不能再操作了, 所以只有在你 不再操作这个文件的时候才调用它,它会自动保存文件。 回写缓冲区会在保持文件打开的情况下保存文 件, 所以只要有必要就使用它。回写看起来像另一次输出, 然后调用方法关闭。像这样: fout << flush; fout.close();    现在你用文本编辑器打开文件,内容看起来是这样:   Here is a number: 150 Now here is a string: John Doe   很简单吧! 现在继续文件输入, 需要一点技巧, 所以先确认你已经明白了流操作,对 "<<" 和">>" 比较熟悉了, 因为你接下来还要用到他们。继续…   二、ASCII 输入   输入和"cin" 流很像. 和刚刚讨论的输出流很像, 但你要考虑几件事情。在我们开始复杂的内容之前 , 先看一个文本:   12 GameDev 15.45 L This is really awesome!   为了打开这个文件,你必须创建一个in-stream对象,?像这样。 ifstream fin("input.txt");   现在读入前四行. 你还记得怎么用"<<" 操作符往流里插入变量和符号吧?好,?在 "<<" (插入)?操作 符之后,是">>" (提取) 操作符. 使用方法是一样的. 看这个代码片段.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值