C++基础:数据文件操作

c++的文件流处理其实很简单,前提是你能够理解它。文件流本质是利用了一个buffer中间层。有点类似标准输出和标准输入一样。

c++ IO的设计保证IO效率,同时又兼顾封装性和易用性。本文将会讲述c++文件流的用法。

有错误和疏漏的地方,欢迎批评指证。

需要包含的头文件: <fstream>

名字空间: std

也可以试用<fstream.h>

fstream提供了三个类,用来实现c++对文件的操作。(文件的创建,读写)。
  ifstream       -- 从已有的文件读

  ofstream      -- 向文件写内容

  fstream        - 打开文件供读写

支持的文件类型

实际上,文件类型可以分为两种: 文本文件和二进制文件.

文本文件保存的是可读的字符, 而二进制文件保存的只是二进制数据。利用二进制模式,你可以操作图像等文件。用文本模式,你只能读写文本文件。否则会报错。

例一: 写文件

声明一个ostream变量

调用open方法,使其与一个文件关联 
写文件 
调用close方法.

#include <fstream.h>

void main

{

ofstream file;

file.open("file.txt");

file<<"Hello file/n"<<75;

file.close();

}
可以像试用cout一样试用操作符<<向文件写内容.
Usages:

file<<"string/n";

file.put('c');
例二:  读文件

1. 声明一个ifstream变量.

2. 打开文件.

3. 从文件读数据

4. 关闭文件.


#include <fstream.h>

void main

{

ifstream file;

char output[100];

int x;

file.open("file.txt");

file>>output;

cout<<output;

file>>x;

cout<<x;

file.close();

}

同样的,你也可以像cin一样使用>>来操作文件。或者是调用成员函数
Usages:

file>>char *;

file>>char;

file.get(char);

file.get(char *,int);

file.getline(char *,int sz);

file.getline(char *,int sz,char eol);
1.同样的,你也可以使用构造函数开打开一个文件、你只要把文件名作为构造函数的

第一个参数就可以了。


ofstream file("fl.txt");

ifstream file("fl.txt");
上面所讲的ofstream和ifstream只能进行读或是写,而fstream则同时提供读写的功能。
          void main()


{

fstream file;

file.open("file.ext",iso::in|ios::out)

//do an input or output here

file.close();

}
open函数的参数定义了文件的打开模式。总共有如下模式


属性列表

ios::in 读

ios::out 写

ios::app 从文件末尾开始写

ios::binary 二进制模式

ios::nocreate 打开一个文件时,如果文件不存在,不创建文件。

ios::noreplace 打开一个文件时,如果文件不存在,创建该文件

ios::trunc 打开一个文件,然后清空内容

ios::ate 打开一个文件时,将位置移动到文件尾

Notes

默认模式是文本 
默认如果文件不存在,那么创建一个新的 
多种模式可以混合,用|(按位或) 
文件的byte索引从0开始。(就像数组一样) 
我们也可以调用read函数和write函数来读写文件。

文件指针位置在c++中的用法:


ios::beg 文件头

ios::end 文件尾

ios::cur 当前位置
例子:

file.seekg(

="nu0">0,ios::end);

int fl_sz = file.tellg();

file.seekg(0,ios::beg); 
常用的错误判断方法:


good() 如果文件打开成功

bad() 打开文件时发生错误

eof() 到达文件尾
例子:

char ch;

ifstream file("kool.cpp",ios::in|ios::out);

if(file.good()) cout<<"The file has been opened without problems;

else cout<<"An Error has happend on opening the file;

while(!file.eof())

{

file>>ch;

cout<<ch;

}    

 

 

以下为数据文件读取时的常用操作

 

 

#include  < iostream >
#include 
< fstream >
#include 
<string >

using   namespace  std;

// 输出空行
void  OutPutAnEmptyLine()
{
    cout
<<"/n";
}


// 读取方式: 逐词读取, 词之间用空格区分
// read data from the file, Word BWord
// when used in this manner, we'll get space-delimited bits of text from the file
// but all of the whitespace that separated words (including newlines) was lost. 
void  ReadDataFromFileWBW()
{
    ifstream fin(
"data.txt");  
    
string s;  
    
while( fin >> s ) 
    
{    
        cout 
<< "Read from file: " << s << endl;  
    }

}


// 读取方式: 逐行读取, 将行读入字符数组, 行之间用回车换行区分
// If we were interested in preserving whitespace, 
// we could read the file in Line-By-Line using the I/O getline() function.
void  ReadDataFromFileLBLIntoCharArray()
{
    ifstream fin(
"data.txt"); 
    
const int LINE_LENGTH = 100
    
char str[LINE_LENGTH];  
    
while( fin.getline(str,LINE_LENGTH) )
    
{    
        cout 
<< "Read from file: " << str << endl;
    }

}


// 读取方式: 逐行读取, 将行读入字符串, 行之间用回车换行区分
// If you want to avoid reading into character arrays, 
// you can use the C++ string getline() function to read lines into strings
void  ReadDataFromFileLBLIntoString()
{
    ifstream fin(
"data.txt");  
    
string s;  
    
while( getline(fin,s) )
    
{    
        cout 
<< "Read from file: " << s << endl; 
    }

}


// 带错误检测的读取方式
// Simply evaluating an I/O object in a boolean context will return false 
// if any errors have occurred
void  ReadDataWithErrChecking()
{
    
string filename = "dataFUNNY.txt";  
    ifstream fin( filename.c_str());  
    
if!fin ) 
    
{   
        cout 
<< "Error opening " << filename << " for input" << endl;   
        exit(
-1);  
    }

}


int  main()
{
    ReadDataFromFileWBW(); 
//逐词读入字符串 
    OutPutAnEmptyLine(); //输出空行

    ReadDataFromFileLBLIntoCharArray(); 
//逐词读入字符数组
    OutPutAnEmptyLine(); //输出空行

    ReadDataFromFileLBLIntoString(); 
//逐词读入字符串
    OutPutAnEmptyLine(); //输出空行

    ReadDataWithErrChecking(); 
//带检测的读取
    return 0;
}
原帖地址:
http://www.cnblogs.com/kevin2010_vip/archive/2010/02/03/1662853.html

 

你可能感兴趣的:(ios,C++,File,buffer,input,output)