zoukankan      html  css  js  c++  java
  • 75.非缓冲编程

    • IO
    • 创建文件并写文件
       1 #define _CRT_SECURE_NO_WARNINGS
       2 #include <stdio.h>
       3 #include <stdlib.h>
       4 #include<io.h>
       5 #include<fcntl.h>//文件打开
       6 #include<sys/stat.h>//系统底层
       7 //银行数据安全要求严格
       8 //键盘,鼠标,显卡设备,
       9 
      10 
      11 void main()
      12 {
      13     char str[256] = { 0 };
      14     scanf("%s", str);
      15 
      16     //创建文件并写文件
      17     int fhd = _open("C:\mem.txt", O_WRONLY | O_CREAT);
      18     if (fhd==-1)
      19     {
      20         printf("打不开");
      21     } 
      22     else
      23     {
      24         _write(fhd, str, sizeof(str));//即刻生效
      25         _close(fhd);//关闭文件
      26     }
      27     system("pause");
      28 }

      #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 随机读写

    • 非缓冲读取文件
       1 char str[256] = { 0 };
       2     int fhd = _open("C:\mem.txt", O_RDONLY | O_TEXT);
       3 
       4     if (fhd==-1)
       5     {
       6         printf("打不开");
       7     } 
       8     else
       9     {
      10         _read(fhd, str, 256);
      11         puts(str);
      12         _close(fhd);//关闭文件
      13     }
    • 非缓冲二进制读写 

    创建结构体

    1 struct info
    2 {
    3     char name[100];
    4     double db;
    5 
    6 };

    二进制写入

     1 struct info infos[8] = { { "wawa1", 9876.5 }, { "zengbin2", 88176.5 },
     2     { "zengbinreal", 1888876.5 }, { "zengbin4", 18276.5 }, { "zengbinchangesex", 8888876.5 },
     3     { "zengbin5", 2876.5 }, { "zengbin7", 38876.5 }, { "zengbin38", 238876.5 } };
     4 
     5     int fhdw = _open("C:\zeng.bin", O_CREAT | O_BINARY | O_WRONLY);
     6 
     7     if (fhdw==-1)
     8     {
     9         return;
    10     } 
    11     else
    12     {
    13         _write(fhdw, infos, sizeof(infos));
    14         _close(fhdw);
    15     }

    二进制读取

     1 struct info infos[8] = {0};
     2 
     3     int fhdr = _open("C:\zeng.bin",  O_BINARY | O_RDONLY);
     4     if (fhdr==-1)
     5     {
     6         return;
     7     } 
     8     else
     9     {
    10         _read(fhdr, infos, sizeof(infos));
    11         _close(fhdr);
    12     }
    13     for (int i = 0; i < 8;i++)
    14     {
    15         printf("娃娃名称%s娃娃价格 %f
    ", infos[i].name, infos[i].db);
    16     }

    _lseek移动

     1   struct info  info1 = { 0 };
     2     int fhdr = _open("C:\zeng.bin", O_BINARY | O_RDONLY);
     3     if (fhdr == -1)
     4     {
     5         return;
     6     }
     7     else
     8     {
     9         while (1)
    10         {
    11             int num;
    12             scanf("%d", &num);
    13             _lseek(fhdr, sizeof(info1)*(num - 1), SEEK_SET);
    14             _read(fhdr, &info1, sizeof(info1));
    15             printf("娃娃名称%s娃娃价格 %f
    ", info1.name, info1.db);
    16         }
    17         _close(fhdr);
    18     }
    • 非缓冲文本的读写

    设置路径:

    1 char path[128] = "xh-2.txt";
    2 char newpath[128] = "renren.txt";

    打开读取和写入

     1 //只读方式打开文件
     2     int fhdr = _open(path, O_RDONLY);
     3     //创建只写文件
     4     int fhdw = _open(newpath, O_CREAT | O_WRONLY);
     5     if (fhdr ==-1 || fhdw ==-1)
     6     {
     7         return -1;//操作失败
     8     } 
     9     else
    10     {
    11         //如果没到文件末尾
    12         while (!_eof(fhdr))
    13         {
    14             char str[256] = { 0 };
    15             int length=_read(fhdr, str, 256);//读取
    16             _write(fhdw, str, length);//写入
    17 
    18         }
    19 
    20         _close(fhdr);//文件不可以关闭两次,
    21         _close(fhdw);
    22     }

    查询文件中的数据

     1 int fhdr = _open("kaifang.txt", O_RDONLY);
     2     if (fhdr == -1 )
     3     {
     4         return -1;//操作失败
     5     }
     6     else
     7     {
     8         _lseek(fhdr, 1023, SEEK_SET);
     9         while (!_eof(fhdr))
    10         {
    11             char str[1024] = { 0 };
    12             int length = _read(fhdr, str, 1024);//读取
    13             
    14             char *p = strstr(str, "小王");
    15             if (p)
    16             {
    17                 printf("%s
    ", str);
    18             }
    19         
    20 
    21         }
    22 
    23 
    24 
    25         _close(fhdr);//文件不可以关闭两次,
    26     
    27     }

    完整代码:

     1 #define _CRT_SECURE_NO_WARNINGS
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 #include<io.h>
     5 #include<fcntl.h>//文件打开
     6 #include<sys/stat.h>//系统底层
     7 //银行数据安全要求严格
     8 //键盘,鼠标,显卡设备,
     9 
    10 char path[128] = "xh-2.txt";
    11 char newpath[128] = "renren.txt";
    12 
    13 void main12()
    14 {
    15     //只读方式打开文件
    16     int fhdr = _open(path, O_RDONLY);
    17     //创建只写文件
    18     int fhdw = _open(newpath, O_CREAT | O_WRONLY);
    19     if (fhdr ==-1 || fhdw ==-1)
    20     {
    21         return -1;//操作失败
    22     } 
    23     else
    24     {
    25         //如果没到文件末尾
    26         while (!_eof(fhdr))
    27         {
    28             char str[256] = { 0 };
    29             int length=_read(fhdr, str, 256);//读取
    30             _write(fhdw, str, length);//写入
    31 
    32         }
    33 
    34         _close(fhdr);//文件不可以关闭两次,
    35         _close(fhdw);
    36     }
    37 
    38 
    39     system("pause");
    40 }
    41 
    42 //fgets
    43 void main()
    44 {
    45     int fhdr = _open("kaifang.txt", O_RDONLY);
    46     if (fhdr == -1 )
    47     {
    48         return -1;//操作失败
    49     }
    50     else
    51     {
    52         _lseek(fhdr, 1023, SEEK_SET);
    53         while (!_eof(fhdr))
    54         {
    55             char str[1024] = { 0 };
    56             int length = _read(fhdr, str, 1024);//读取
    57             
    58             char *p = strstr(str, "小王");
    59             if (p)
    60             {
    61                 printf("%s
    ", str);
    62             }
    63         
    64 
    65         }
    66 
    67 
    68 
    69         _close(fhdr);//文件不可以关闭两次,
    70     
    71     }
    72 
    73 
    74     system("pause");
    75 }
  • 相关阅读:
    五分钟秒懂机器学习混淆矩阵、ROC和AUC
    五分钟学会Python装饰器,看完面试不再慌
    LeetCode54 螺旋矩阵,题目不重要,重要的是这个技巧
    Golang——变量的声明与定义
    LeetCode52题,别再问我N皇后问题了
    spark中的pair rdd,看这一篇就够了
    MySQL不香吗,为什么还要有noSQL?
    JBOSS安装配置详细教程
    Aspose.Words关于域的应用
    SqlServer PIVOT行转列
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8454395.html
Copyright © 2011-2022 走看看