C++学习笔记4--文件操作

【ios中的枚举常量】
skipws:跳过空白符读数。空格、制表符\t、回车符\r和换行符\n统称为空白符。默认为设置。
left:靠左对齐输出数据。
right:靠右对齐输出数据。
insternal:显示占满整个域宽,用填充字符在符号和数值之间填充。
dec:用十进制输出数据。
hex:用十六进制输出数据。
showbase:在数值前显示基数符,八进制基数符是0,十六进制基数符是0x。
showpoint:强制输出的浮点数中带有小数点和小数尾部的无效数字0.
uppercase:用大写输出数据。
showpos:在数值前显示符号。
scientific:用科学技术发显示浮点数。
fixed:用固定小数点位数显示浮点数。
例:字符相加
#include <iostream>
#include <strstream>
using namespace std;
int main(int argc, char* argv[]) {
    char buf[] = "1002000";
    int i , j;
    istrstream s1(buf);
    s1 >> i;
    istrstream s2(buf, 3); //读进3个字符
    s2 >> j;
    cout << i + j <<endl;
    return 0;
}

进行文件操作之前首先要打开文件。打开文件有两种方式。
(1)在创建文件流时利用构造函数打开文件,即在创建流时加入参数。语法结构如下:
<文件流类><文件流对象名>(<文件名>,<打开方式>)
其中文件流可以是fstream、ifstream和ofstream中的一种。文件名指的是磁盘文件的名称,包


括磁盘文件的路径名。打开方式在ios中定义,有输入方式、输出方式、追加方式等。
ios::in :用输入方式打开文件,文件只能读取,不能改写。
ios::out :以输出方式打开文件,文件只能改写,不能读取。
ios::app :以追加方式打开文件,打开后文件指针在文件尾部,可改写。
ios::ate :打开一存在的文件,文件指针指向文件的尾部,可读可写。
ios::binary :以二进制方式打开文件。
ios::trunc :打开文件进行写操作,如果文件已经存在,清除文件中的数据。
ios::nocreate :打开已经存在的文件,如果文件不存在,打开失败,不创建。
ios::noreplace :创建新文件,如果文件已经存在,打开失败,不覆盖。
参数可以结合运算符“|”使用,例如:
ios::in|ios::out :以读写方式打开,对文件可读可写。
ios::in|ios:binary :以二进制方式打开文件,进行读操作。
使用想读路径打开文件test.txt进行写操作:
ofstream outfile("test.txt",ios::out);
使用绝对路径打开文件test.txt进行写操作:
ofstream outfile("c:\\test.txt", ios::out);
(2)利用open函数打开磁盘文件。语法结构如下:
<文件流对象名>.open(<文件名>,<打开方式>);
文件流对象是一个已经定义了的文件流对象。
ifstream infile;
infile.open("test.txt",ios::out);
使用两种方式中的任意一种打开文件后,如果打开成功,文件流对象为非0值;如果打开失败,


则文件流对象为0.可以使用以下语句检测一个文件是否打开成功:
void open(const char* filename,int mode,int prot=filebuf::openprot)
prot决定文件的访问方式,取值说明如下:
 0表示为普通文件。
 1表示为只读文件。
 2表示为隐含文件。
 3表示为系统文件。
例:创建文件
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
    ofstream ofile;
    cout << "Create file1" << endl;
    ofile.open("test.txt");
    if(!ofile.fail()) {
        ofile << "lasolmi" << endl;
        ofile.close();
        cout << "Create file2" << endl;
        ofile.open("test2.txt");
        if(!ofile.fail()) {
            ofile << "hello.world!" <<endl;
            ofile.close();
        }
    }
    return 0;
}

【文件的读写】
(1)文件流类型:输入流、输出流、输入/输出流。
ifstream ifile; //声明一个输入流
ofstream ofile; //声明一个输出流
fstream iofile; //声明一个输入/输出流
(2)文件流成员函数。
ofstream和ifstream类有很多用于磁盘文件管理的函数。
attach:在一个打开的文件和流之间建立连接。
close:刷新未保存的数据后关闭文件。
flush:刷新流。
open:打开一个文件并把它与流连接。
put:把一个字节写入流中。
rdbuf:返回与流连接的filebuf对象。
seekp:设置流文件指针位置。
setmode:设置流为二进制或文本模式。
tellp:获取流文件指针位置。
write:把一组字节写入流中。
(3)fstream成员函数。
get(c) :从文件读取一个字符
getline(str,n,'\n') :从文件读取字符存入字符串str中,知道读取n-1个字符或遇到'\n'时结



peek() :查找下一个字符,但不从文件中取出
put(c) :将一个字符写入文件
putback(c) :对输入流放回一个字符,但不保存
eof() :如果读取超过eof,返回true
ignore(n) :跳过n个字符,参数为空时,表示跳过下一个字符
例:使用ifstream和ofstream对象实现读写文件的功能。
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main(int argc, char* argv[]) {
    char buf[128];
    ofstream ofile("test.txt");
    for(int i=0;i<5;i++) {
        memset(buf,0,128);
        cin >> buf;
        ofile << buf;
    }
    ofile.close();
    ifstream ifile("test.txt");
    while(!ifile.eof()) {
        char ch;
        ifile.get(ch);
        if(!ifile.eof()) cout << ch;
    }
    cout << endl;
    ifile.close();
    return 0;
}

例:使用fstream向文本文件写入数据。
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
    fstream file("test.txt",ios::out);
    if(!file.fail()) {
        cout << "start write" << endl;
        file << "lasolmi" << endl;
    }
    else cout << "can not open" <<endl;
    file.close();
    return 0;
}

例:读取文本文件
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
    fstream file("test.txt",ios::in);
    if(!file.fail()) {
        while(!file.eof()) {
            char buf[128];
            file.getline(buf,128);
            if(file.tellg() > 0) {
                cout <<buf;
                cout <<endl;
            }
        }
    }
    else cout << "can not open" <<endl;
    file.close();
    return 0;
}

例:实现文件复制。
ifstream infile;
ofstream outfile;
infile.open(infilename);
if(!infile) {cout<<"fail open infile"<<endl;}
outfile.open(outfilename);
if(!outfile) {cout<<"fail open outfile"<<endl;}
while(infile.get(c)) outfile << c;
infile.close();
outfile.close();

ios提供了一下成员函数来检测或设置流的状态。
int rdstate();
int eof();
int fail();
int bad();
int good();
int clear(int flag=0);
为提高程序的可靠性,应在程序中检测I/O流的操作是否正常。例如用fstream默认方式打开文


件时,如果文件不存在,fail函数就能检测到错误发生,然后通过rdstate方法获得文件状态。
fstream file("test.txt");
if(file.fail()) {
    cout << file.rdstate() << endl;
}
文件的追加。
ofstream ofile("test.txt",ios::app);
if(!ofile.fail()) { ofile << "追加内容"; }
else cout << "can not open";
追加可以使用其他方法实现,例如先打开文件然后通过seekp方法将文件指针移到末尾,再向文


件中写入数据,整个过程和使用参数取值一样。使用seekp方法实现追加的代码如下:
fstream iofile("test.dat",ios::in|ios::out|ios::binary);
if(iofile) {
    iofile.seekp(0,ios::end); //为了写入移动
    iofile << endl;
    iofile << "我是新加入的";
    iofile.seekg(0); //为了写入移动
    char data[100];
    for(int i=0;!iofile.eof() && i<sizeof(data);i++) iofile.get(data[i]);
    cout << data;
}

文件结尾的判断:
 如果文件指针指向文件末尾,get方法获取不到数据就返回-1,这可作为判断结束的标志。
 同样功能使用eof方法也可以实现。
stream sp = ifile.tellg(); //sp为ifile指针当前位置
在指定位置读写文件。设置文件指针位置的函数:
seekg:位移字节数,相对位置用于输入文件中指针的移动。
seekp:位移字节数,相对位置用于输出文件中指针的移动。
tellg:用于查找输入文件中的文件指针位置。
tellp:用于查找输出文件中的文件指针位置。
ios::beg:文件头部。
ios::end:文件尾部。
ioe::cur:文件指针的当前位置。
例:输出文件指定位置的内容。
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc,char* argv[]) {
    ifstream ifile;
    char cFileSelect[20];
    cout << "input filename:";
    cin >> cFileSelect;
    ifile.open(cFileSelect);
    if(!ifile) {
        cout << cFileSelect << " can not open" << endl;
        return 0;
    }
    ifile.seekg(0,ios::end);
    int maxpos = ifile.tellg();
    int pos;
    cout << "Position:";
    cin >> pos;
    if(pos > maxpos)
        cout << "is over file length" << endl;
    else {
        char ch;
        ifile.seekg(pos);
        ifile.get(ch);
        cout << ch <<endl;
    }
    ifile.close();
    return 0;
}

文件和流的关联和分离:一个流对象可以在不同时间表示不同文件。在构造一个流对象时,不用


将流和文件绑定。使用流对象的open成员函数动态与文件绑定,如果要关联其他文件就调


用close成员函数关闭当前文件与流的连接,再通过open成员函数建立与其他文件的连接。
删除文件,例:
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc,char* argv[]) {
    char file[50];
    cout << "input file name:" << "\n";
    cin >> file;
    if(!remove(file))
        cout << "The file:" << file <<"已删除" << endl;
    else
        cout << "The file:" << file << "删除失败" <<endl;
    return 0;
}


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值