正在加载...

C++标准库 之 iostream库的学习笔记(三) ifstream类的使用和介绍

十月 20, 2008 感谢 【ZeroDream】 基情投稿  
文章分类: C/C++ | 被摸了 106 次

该继续看ifstream类了。

ifstream继承自istream类,istream类只有一个iostream库中创建好的cin对象,对应一个输入设备就是pc机的键盘,而ifstream类则没有在fstream中有创建好的对象,原因上一篇文章已经说了。

ifstream是文件输入类,输入的源是文件,目标是内存,从文件向内存输入,也就是读取文件的意思了。

如果想读取一个文件,简单的示例代码如下:

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

int _tmain(int argc, _TCHAR* argv[])
{

    ifstream hFileRead(
"c:\\1.txt", ios::in, _SH_DENYRW); // denies read and write

    
if (!hFileRead)
    
{
        cout 
<< "read fail!" << endl;
        exit(
1);
    }


    
char ch;
    
string content;
    
while (hFileRead.get(ch)) // the get method retuan false on read end
    {
        content 
+= ch; // string support oprator+=, use overload
        cout.put(ch);    
    }

    system(
"pause"); // other process can't read or write the file
    hFileRead.close();
    system(
"pause"); 

    
return 0;
}


get方法会读取一个字符,当读到文件末尾就会返回false。
并且示例了_SH_DENYRW的共享方式打开文件的后果,在第一个暂停处无法打开c:\1.txt,当调用close才可以正常打开。

主要工作函数:
get
getline
read

另外有几个辅助函数:
peek
putback
ignore
gcount
seekg
seekdir
tellg

实际开发过程中使用std::getline这个全局函数来获取一行字符,使用如下:


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

int _tmain(int argc, _TCHAR* argv[])
{
    fstream hFile;
    hFile.open(
"c:\\1.txt", ios::out | ios::in,  _SH_DENYWR); //only deny write
    
    
string str;
    
while(std::getline(hFile, str))
    cout 
<< str << endl;
    hFile.close();

    cin.
get();

    
return 0;
}

C++标准库 之 iostream库的学习笔记(二)fstream库以及ofstream类的使用

十月 20, 2008 感谢 【ZeroDream】 基情投稿  
文章分类: C/C++ | 被摸了 96 次

iostream库不仅支持终端设备的输入输出,还支持文件的输入输出,和文件有关的输入输出类声明在fstream头文件中,有三个类负责文件的输入输出

1) ifstream类:从istream类派生。
2) ofstream类:从ostream类派生。
3) fstream类:从iostream类派生。

由于文件的输入输出和键盘鼠标的输入输出是不一样的,一般pc机只有一个键盘设备,所以iostream库内部声明了一个istream类的对象cin,这个对象负责从键盘获取数据,而文件设备在系统中是由许多的,所以iostream库内部无法给你为机器的每个文件都创建一个负责获取数据的ifstream对象和负责写入数据的ofstream对象,所以我们要针对一个文件进行读取或写入数据的时候都要自己创建一个ifstream或ostream类的对象来用。

ofstream类的默认构造函数如下:

ofstream::ofstream(const char* filename, int mode = ios::outint openport = filebuf::openport);

filename是要打开的文件名,
mode是打开的方式,
openport是打开文件的属性。

mode可以设置的方式如下:
ios::app        以追加的方式打开
ios::ate        文件打开后定位到文件尾
ios::binary    以二进制方式打开文件,默认是以文本方式打开
ios::in          文件以读(输入)方式打开
ios::out        文件以写(输出)方式打开
ios::trunc     如果文件存在,则把文件清空。
以上属性用“|”(按位或)连接起来。

openprot属性如下:
0    普通文件
1    只读文件
2    隐含文件
4    系统文件
以上属性可以用加或者按位或方式组织起来,比如1|2和3都代表既是只读又是隐含文件。

在windows操作系统中可以不要第三个参数,如果加入第三个参数,那第三个参数是打开文件的共享方式,也就是打开这个文件时,其他进程是否可以读写该文件。
共享方式参数可以是下面的值:
0x10                   //_SH_DENYRW   Denies   read   and   write   access   to   the   file
0x20                   //_SH_DENYWR   Denies   write   access   to   the   file
0x30                   //_SH_DENYRD   Denies   read   access   to   the   file.
0x40                   //_SH_DENYNO   Permits   read   and   write   access  
其他值都会报 "Invalid   sharing   flag "的错误。

    ofstream hFile("c:\\1.txt", ios::out, _SH_DENYRW); // _SH_DENYRW is deny read and write
    
    
if (!hFile) // if the file could open, hFile is a handle, else is zero
    {
        cout 
<< "write fail!" << endl;
        cout 
<< "access is denies,maybe the file is
 readonlys,or use deny read opened of other process.
" << endl;
    }

    
else
    
{
        hFile 
<< "by coderlee writes";
        cout 
<< "write success!" << endl;
    }

    hFile.close(); 
// opened file need close.

上面是写文件的事例代码,先打开文件,然后判断是不是0,如果是0,则提示write fail否则写文件,提示write success.

C++标准库 之 iostream库的学习笔记(一)iostream库的介绍和istream,ostream类以及cin,cout对象

十月 20, 2008 感谢 【ZeroDream】 基情投稿  
文章分类: C/C++ | 被摸了 36 次

c语言的标准输入输出库是stdio.h  是一个函数库而不是类库。
其中包括了我们最常使用的scanf  printf  都是一些独立的全局函数,因为C语言是不支持类的。

c++的标准输入输出库iostream 是一个类库,以类的形式组织,使用该库中的类要先引用命名空间:using namespace std;
最常使用的是cin和cout,这两个都是对象,cin是istream类的对象,cout是ostream类的对象,而输入的cin>>与输出时的cout<<中的左移<<与右移>>分别是istream类与ostream类的操作符重载。

iostream库里面创建了3个标准流对象:
1 cin 表示标准输入的istream对象,cin可以使我们从设备读取数据。
2 cout 表示标准输出的ostream对象,cout可以使我们向设备写入数据。
3 cerr 表示标准错误的ostream对象,cerr是导出程序错误消息的地方,只能向屏幕设备写数据。

标准的流对象都有默认的设备:
cout << data;  cout默认的设备是显示器缓冲区。
cin >> data;  cin默认的设备是键盘缓冲区。

iostream库由以下几个库组成:fstream, iomainip, ios, iosfwd, iostream, istream, ostream, sstream, streambuf, strstream。
istream用来访问操作系统的输入流,ostream访问操作系统的输出流,iostream同时继承了这两个类。

在ostream类中,重载了许多的左移<<操作符,对每种基本数据类型都做了重载,比如
&ostream operator<<(ostream &temp, int source);
&ostream operator<<(ostream &temp, char source);
&ostream operator<<(ostream &temp, char* source);
由于以上重载返回的数据类型都是ostream的引用,ostream又可以作为左值,所以可以实现cout<<"abc"<<endl<<123;

同样在istream类中,也重载了许多右移>>操作符,对每种基本数据类型都做了重载,比如
&istream operator>>(istream &temp,int source);
&istream operator>>(istream &temp,char source);

以上是终端标准输入输出设备的输入输出,也就是一般pc机的键盘和显示器的输入输出。

Page 1 of 11