Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1720913
  • 博文数量: 171
  • 博客积分: 11553
  • 博客等级: 上将
  • 技术积分: 3986
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-25 20:28
文章分类

全部博文(171)

文章存档

2012年(2)

2011年(70)

2010年(9)

2009年(14)

2008年(76)

分类: C/C++

2008-03-15 13:39:41

#include <fstream>
#include <iomanip>
#include <iostream>
////使用文件操作时,不要指望作用域会自动将文件关闭,显示的调用close()函数
int main(int argc, char* argv[])
{
    ////向文件中写入10个数字
    {
        std::ofstream fout("out.txt");
        if(!fout)
            std::cout << "failed to open out.txt" << std::endl;
        for(int i = 0; i < 10; ++i)
        {
            fout << std::setw(3) << i << std::endl;

        }
        fout.close();
    }

    ////直接显示文件中的所有内容
    {
        std::ifstream fout("out.txt");
        std::cout << fout.rdbuf() << std::endl;
        fout.close();
    }
    ////两个文件的复制,使用rdbuf得到文件的内容
    {
        std::ifstream fin("out.txt");
        std::ofstream fout("fcopy.txt");
        if(fin && fout == NULL)
        {
            std::cout <<"open file out.txt error" << std::endl;
            return -1;
        }
        fout << fin.rdbuf();
        fin.close();
        fout.close();
    }
    ////从文件中读取10个字符
    {
        std::ifstream fin("out.txt");
        if(!fin)
            std::cout << "failed to open out.txt" << std::endl;
        for(int i = 0; i < 10; ++i)
        {
            char c;
            fin >> std::setw(3) >> c;
            ////std::cout << c << std::endl;

        }
        fin.close();
    }

    ////将文件的内容添加行号
    {
        ////一行中最大字符数为256
#define MAX_COLS 256
        ////行号以8位数字表示
#define LINE_NUM 9
        char szLine[MAX_COLS] = {0};
        std::ifstream fin("out.txt");
        std::ofstream fout("lineno.txt");
        if(fin && fout == NULL)
        {
            std::cout << "open file out.txt or lineno.txt error" << std::endl;
            return -1;
        }
        int i = 0;
        char szLineNo[LINE_NUM] = {0};
        while (fin.getline(szLine,MAX_COLS))
        {
            sprintf(szLineNo,"%08d",i++);
            fout.write(szLineNo,LINE_NUM);        //写入行号
            fout.write("\t",1);                    //行号与文件内容以TAB分隔
            fout.write(szLine,strlen(szLine));    //写入文件中的一行内容
            fout << std::endl;                    //写入回车换行符

        }
        fin.close();
        fout.close();

    }
    ////向文件中写入二进制数据
    {
        std::ofstream fout("out.bin",std::ios::out | std::ios::binary);
        if(!fout)
        {
            std::cout << "open out.bin error" << std::endl;
        }
        for(int i = 0; i < 10;++i)
        {
            fout << i;
        }

        std::ofstream fbin("fbin.bin",std::ios::out | std::ios::binary);
        if(!fbin)
        {
            std::cout << "open fbin.bin error" << std::endl;
            return -1;
        }
        for (int j = 0; j < 10;j++)
        {
            fbin.write((char*)&j,sizeof(int));
        }
        fout.close();

    }

    {
        ////创建文件数据结构,并将数据写入文件
        ////结构以4字节对齐,sizeof(CFileData) = 16
        struct CFileData
        {
            int nNo;
            char szName[10];
        };
        std::ofstream fFileData("fFiledata",std::ios::binary | std::ios::out);
        if(!fFileData)
        {
            std::cout << "open file fFileData error" << std::endl;
            return -1;
        }
        for (int i = 0; i <10;++i )
        {
            CFileData fd;
            memset(&fd,0,sizeof(fd));
            fd.nNo = i;
            sprintf(fd.szName,"%d",i);
            fFileData.write((const char*)&fd,sizeof(fd));
        }
        ////将这个文件关闭否则后面的读取操作都会失败
        fFileData.close();

        ////从文件中第五个CFileData开始读取结构数据
        std::ifstream fInFile("fFileData",std::ios::binary | std::ios::in);
        if(!fInFile)
        {
            std::cout <<"open file fFileData error" << std::endl;
            return -1;
        }
        std::ios::pos_type pos = fInFile.tellg();
        std::cout << pos << std::endl;
        ////跳过前面的5个结构
        fInFile.seekg(5*sizeof(CFileData),std::ios::beg);
        for (int i = 0; i < 5;++i)
        {
            CFileData fd;
            memset(&fd,0,sizeof(fd));
            fInFile.read((char*)&fd,sizeof(fd));
            std::cout << "No. = " << fd.nNo << "\t" << "Name = " << fd.szName << std::endl;
        }
        fInFile.close();

    }

    return 0;
}

阅读(2580) | 评论(0) | 转发(0) |
0

上一篇:STL的IO操作之一

下一篇:汉字转化拼音

给主人留下些什么吧!~~