非缓冲文件编程(实时操作)

在下面情况下需要非缓冲文件编程。

//银行数据安全要求严格

//键盘,鼠标,显卡设备

 1.文本文件读写操作:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include<io.h>
#include<fcntl.h>//文件打开,设置文件打开模式,就是下面的一大列函数。
#include<sys/stat.h>//系统底层,sys表示驱动,stat表示文件的状态。


void main1()
{
    char str[256] = { 0 };
    scanf("%s", str);

    int fhd = _open("C:\mem.txt", O_WRONLY | O_CREAT);//既只写 ,又创建
    if (fhd==-1)
    {
        printf("打不开");
    } 
    else
    {
        _write(fhd, str, sizeof(str));//即刻生效
        _close(fhd);//关闭文件
    }

    system("pause");

}
//#define O_RDONLY _O_RDONLY 只读 //#define O_WRONLY _O_WRONLY 只写 //#define O_RDWR _O_RDWR 读写 //#define O_APPEND _O_APPEND 增加 //#define O_CREAT _O_CREAT 创建 //#define O_TRUNC _O_TRUNC //#define O_EXCL _O_EXCL //#define O_TEXT _O_TEXT 文本 //#define O_BINARY _O_BINARY 二进制 //#define O_RAW _O_BINARY //#define O_TEMPORARY _O_TEMPORARY 临时 //#define O_NOINHERIT _O_NOINHERIT //#define O_SEQUENTIAL _O_SEQUENTIAL //#define O_RANDOM _O_RANDOM 随机读写 void main2() { char str[256] = { 0 }; int fhd = _open("C:\mem.txt", O_RDONLY | O_TEXT); if (fhd==-1) { printf("打不开"); } else { _read(fhd, str, 256); puts(str); _close(fhd);//关闭文件 } system("pause"); }

2.文本文件查询:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include<io.h>
#include<fcntl.h>//文件打开
#include<sys/stat.h>//系统底层
//银行数据安全要求严格
//键盘,鼠标,显卡设备,

char path[128] = "Z:\I\尹成清华终极版C语言视频源码文档20150131\大数据相关数据\xh-2.txt";
char newpath[128] = "C:\renren.txt";

void main12()
{
    int fhdr = _open(path, O_RDONLY);
    int fhdw = _open(newpath, O_CREAT | O_WRONLY);
    if (fhdr ==-1 || fhdw ==-1)
    {
        return -1;//操作失败
    } 
    else
    {
        while (!_eof(fhdr))
        {
            char str[256] = { 0 };
            int length=_read(fhdr, str, 256);//读取
            _write(fhdw, str, length);//写入

        }



        _close(fhdr);//文件不可以关闭两次,
        _close(fhdw);
    }


    system("pause");
}

//fgets
void main()
{
    int fhdr = _open("I:\尹成清华终极版C语言视频源码文档20150131\大数据相关数据\kaifang.txt", O_RDONLY);
    if (fhdr == -1 )
    {
        return -1;//操作失败
    }
    else
    {
        _lseek(fhdr, 1023, SEEK_SET);
        while (!_eof(fhdr))
        {
            char str[1024] = { 0 };
            int length = _read(fhdr, str, 1024);//读取
            
            char *p = strstr(str, "曾彬");
            if (p)
            {
                printf("%s
", str);
            }
        

        }



        _close(fhdr);//文件不可以关闭两次,
    
    }


    system("pause");
}

弊端:文件是连成一片的,它是取一段长度作为一行,有可能实际的一行与下一行发生错位。如果是非缓冲文件编程,无法判断 ,不能读一个写一个需要建立双索引(开始和长度),

 3.二进制文本文件读写:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include<io.h>
#include<fcntl.h>//文件打开
#include<sys/stat.h>//系统底层
//银行数据安全要求严格
//键盘,鼠标,显卡设备,

struct info
{
    char name[100];
    double db;

};

void main1x()
{
    struct info infos[8] = { { "zengbin1", 9876.5 }, { "zengbin2", 88176.5 },
    { "zengbinreal", 1888876.5 }, { "zengbin4", 18276.5 }, { "zengbinchangesex", 8888876.5 },
    { "zengbin5", 2876.5 }, { "zengbin7", 38876.5 }, { "zengbin38", 238876.5 } };
    //printf("
%d", sizeof(struct info)); 112*8
    int fhdw = _open("C:\zeng.bin", O_CREAT | O_BINARY | O_WRONLY);

    if (fhdw==-1)
    {
        return;
    } 
    else
    {
        _write(fhdw, infos, sizeof(infos));
        _close(fhdw);
    }



    system("pause");
}

void main2x()
{
    struct info infos[8] = {0};

    int fhdr = _open("C:\zeng.bin",  O_BINARY | O_RDONLY);
    if (fhdr==-1)
    {
        return;
    } 
    else
    {
        _read(fhdr, infos, sizeof(infos));
        _close(fhdr);
    }
    for (int i = 0; i < 8;i++)
    {
        printf("娃娃名称%s娃娃价格 %f
", infos[i].name, infos[i].db);
    }


    system("pause");
}

void main3()
{
    

    struct info  info1 = { 0 };
    int fhdr = _open("C:\zeng.bin", O_BINARY | O_RDONLY);
    if (fhdr == -1)
    {
        return;
    }
    else
    {
        while (1)
        {
            int num;
            scanf("%d", &num);
            _lseek(fhdr, sizeof(info1)*(num - 1), SEEK_SET);
            _read(fhdr, &info1, sizeof(info1));
            printf("娃娃名称%s娃娃价格 %f
", info1.name, info1.db);
        }
    




        _close(fhdr);
    }

    system("pause");
}
原文地址:https://www.cnblogs.com/sjxbg/p/5894797.html