zoukankan      html  css  js  c++  java
  • windows编程中的文件操作整理(一)

    根据类型整理如下:

    1、c语言中文件操作

    2、c++语言中的文件操作

    3、win32API中的文件操作

    4、MFC中的文件操作

    一、C文件系统

    1、概要:

      C文件系统有几个相互联系的函数构成,常见的函数会在下面介绍,这些函数要求头文件stdio.h,C++程序也可以使用C++风格的<stdio>。

      头文件stdio.h提供了I/O函数的原型并定义了3个类型:size_t、fpos_t、FILE,前两个是无符号整数,FILE供文件指针使用,如 FILE *fp 。

    2、常用函数:

      fopen()         打开一个文件

      fclose()         关闭一个文件

      putc()和fputc()    把一个字符写到文件中

      getc()和fgetc()    从一个文件读一个字符

      fgets()        从一个文件读一个字符串

      fputs()        把一个字符串写到文件中

      fseek()        移动文件位置指针

      fprintf()        输出到磁盘文件

      fscanf()        从磁盘文件中读取数据

      feof()         判断文件结束否

      rewind()       把文件位置指针重新置于起始位置

      fflush()        清空文件流并写到磁盘文件

      remove()       删除文件

      fread()        从文件中读取数据

      fwrite()        写数据到文件

    3、实例

      

    #include "stdio.h"
    #include "conio.h"
    #include "string.h"
    #include "stdlib.h"
    
    int main()
    {
        printf("file write operation test...\n");
        FILE *pfOut = fopen("file_test.dat", "wb");
        putc(' ', pfOut);
        putc(' ', pfOut);
        fputc(' ', pfOut);
        fputs(" hello world, ", pfOut);
        char *pOutput = "this is a test!";
        fwrite(pOutput, 1, strlen(pOutput), pfOut);
        int anTestOut[3];
        for (int i=0; i<3; i++)
            anTestOut[i] = rand();
        fwrite(anTestOut, sizeof(int), 3, pfOut);
        fseek(pfOut, 0L, SEEK_SET);                // 注意这里移到文件头,但是之后的输入会覆盖式写入!
        fprintf(pfOut, "%c%c%c", '*', '_', '*');
        fclose(pfOut);
        printf("press any key to finish file write operation...\n");
        getch();
    
        printf("file read operation test...\n");
        FILE *pfIn = fopen("file_test.dat", "rb");
        char c1,c2,c3;
        c1 = getc(pfIn);
        c2 = getc(pfIn);
        c3 = fgetc(pfIn);
        char szHello[128];
        memset(szHello, 0, sizeof(szHello));
        fgets(szHello, 128, pfIn);
        szHello[29] = 0;
        fseek(pfIn, -12L, SEEK_END);        // 读取最后3个整数
        int a1=0, a2=0, a3=0;
        //fscanf(pfIn, "%d%d%d", &a1, &a2, &a3);        // 不能用这种方式读取,这种方式适合文本类,不适合读取二进制类
        fread(&a1, sizeof(int), 1, pfIn);
        fread(&a2, sizeof(int), 1, pfIn);
        fread(&a3, sizeof(int), 1, pfIn);
        fprintf(stdout, "%c%c%c%s%d%d%d\n", c1, c2, c3, szHello, a1, a2, a3);        // 输出到屏幕上
        fclose(pfIn);
        printf("press any key to finish file read operation...\n");
        getch();
    
        printf("delete file operation test...\n");
        ::remove("file_test.dat");
    
        system("pause");
        return 0;
    }

     二、C++文件系统

    1、概要:

      在C++中,可以通过文件链接到一个流来打开该文件。在打开该文件之前,需要获得一个流。有三种形式的流:输入流、输出流和输入/输出流。为了创建一个输入流,需要将流声明为ifstream类;为了创建一个输出流,需要将流声明为ofstream类;为了创建一个输入输出流,需要将流声明为fstream。

      为了执行文件的I/O,必须在程序中包含头文件<fstream>。该头文件的定义包含了ifstream、ofstream和fstream,这些类分别从istream、ostream和iostream派生而来,而istream、ostream和iostream都是从ios派生而来。

    2、常用函数:

      open()        打开一个文件

      is_open()       检查文件是否被成功打开

      close()        关闭一个文件

      >>           从文本文件读取内容(遇到空格,换行,文件结束终止),类似于C的fscanf()

      <<           写入文本文件(遇到空格、换行、文件结束终止),类似于C的frpintf()

      put()         写入一个字符

      get()         读取一个字符

      getline()       按行读取字符(根据参数的条件)

      read()        从文件读

      write()        写到文件

      ignore()       从输入流中读取并且丢弃字符

      flush()        强行将数据写到磁盘

      seekg()       移动文件当前的获取指针(指定文件中下一次输入操作发生的位置,供读操作使用)

      seekp()       移动文件当前的放置指针(指定文件中下一次输出操作发生的位置,供写操作使用)

      tellg()        得到当前的获取指针(其返回值一般用作seekg函数的参数)

      tellp()        得到当前的放置指针(其返回值一般用作seekp函数的参数)

     3、实例1:

    #include <iostream>
    #include <fstream>
    #include "conio.h"
    using namespace std;
    
    int main()
    {
        ofstream ofs("file_test2.txt");
        if (!ofs)
        {
            cout << "cannot open file_test2.txt file.\n";
            return 1;
        }
    
        ofs << "age " << 20 << endl;
        ofs << "hight " << 170 << endl;
        ofs << "weight " << 66.5 << endl;
        ofs.close();
    
        ifstream ifs("file_test2.txt");
        if (!ifs.is_open())
        {
            cout << "cannot open file_test2.txt file.\n";
            return 1;
        }
    
        char item[20];
        float fValue;
        for (int i=0; i<3; i++)
        {
            ifs >> item >> fValue;
            cout << item << " " << fValue << endl;
        }
        ifs.close();
        
        fprintf(stdout, "press any key to exit\n");
        getch();
        return 0;
    }

     4、实例2:

     

    #include <fstream>
    #include <iostream>
    #include "conio.h"
    using namespace std;
    
    struct STPersonal
    {
        char            szName[32];
        double            dHeight;
        double            dWeight;
        int                nMoney;
        STPersonal()
        {
            szName[0] = 0;
            dHeight = 0;
            dWeight = 0;
            nMoney = 0;
        }
    };
    
    bool Write2File(std::string strFileName, STPersonal* pTest)
    {
        ofstream ofs(strFileName.c_str(), ios::out | ios::binary);
        if (!ofs)
        {
            cout << "cannot open file on write.\n";
            return false;
        }
        ofs.write((char *)pTest, sizeof(STPersonal));
        ofs.close();
    
        return true;
    }
    
    bool ReadFromFile(std::string strFileName, STPersonal* pTest)
    {
        ifstream ifs(strFileName.c_str(), ios::in | ios::binary);
        if (!ifs)
        {
    
            cout << "cannot open file on read.\n";
            return false;
        }
        ifs.read((char *)pTest, sizeof(STPersonal));
        ifs.close();
    
        return true;
    }
    
    bool SwapCharTest()        // 反转文件内容
    {
        ofstream ofs("file_test3_0.dat", ios::out | ios::binary);
        if (!ofs)
            return false;
        char *p1 = "abcdefghijklmnopqrstuvwxyz...0123456789";
        int nStrLen = (int)strlen(p1);
        ofs.write(p1, nStrLen);
        cout << "write to file: " << p1 << endl;
        ofs.close();
    
        printf("begin to swap...\n");
        fstream fs("file_test3_0.dat", ios::in | ios::out | ios::binary);
        if (!fs)
            return false;
        char c1, c2;
        for (int i=0, j=nStrLen-1; i<j; i++, j--)
        {
            fs.seekg(i, ios::beg);            // 移动当前的获取指针
            fs.get(c1);
            fs.seekg(j, ios::beg);
            fs.get(c2);
    
            fs.seekp(i, ios::beg);            // 移动当前的放置指针
            fs.put(c2);
            fs.seekp(j, ios::beg);
            fs.put(c1);
        }
        fs.close();
        
        ifstream ifs("file_test3_0.dat", ios::in | ios::binary);
        if (!ifs)
            return false;
        char p2[128];
        ifs.read(p2, nStrLen);
        p2[nStrLen] = 0;
        cout << "read from file: " << p2 << endl;
        ifs.close();
    
        return true;
    }
    
    int main()
    {
        STPersonal stTest;
        strcpy(stTest.szName, "zhangsan");
        stTest.dHeight = 170.5;
        stTest.dWeight = 68.2;
        stTest.nMoney = 123;
    
        Write2File("file_test3.dat", &stTest);
        ReadFromFile("file_test3.dat", &stTest);
    
        cout << "name: " << stTest.szName << endl;
        cout << "height: " << stTest.dHeight << endl;
        cout << "weight: " << stTest.dWeight << endl;
        cout << "money: " << stTest.nMoney << endl;
    
        SwapCharTest();
    
        printf("press any key to exit...\n");
        getch();
        return 0;
    }
  • 相关阅读:
    ORA01034:ORACLE not available 问题的解决方法
    利用Bulk Insert将Excel中的大批量数据入库
    【Hibernate】*.hbm.xml配置
    lib和dll文件的区别和联系
    oracle ,mysql总date的比较
    C++ Primer 4 CPP Note 1.5 类的简介
    C++ Primer 4 CPP Note 1.4 控制结构
    未找到方法: Dispose System.IO.Stream
    pragma comment的使用
    C++ Primer 4 CPP Note 2.1 基本内置类型
  • 原文地址:https://www.cnblogs.com/yuohoo/p/2625987.html
Copyright © 2011-2022 走看看