zoukankan      html  css  js  c++  java
  • Linux下C语言的文件操作

    代码:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <fcntl.h>
     4 /*************基本的函数API********************
     5 int open(const char *pathname, int oflag, int perms)
     6 oflag:
     7     O_RDONLY 只读
     8     O_WRONLY 只写
     9     O_RDWR   读写
    10     O_APPEND 追加
    11     O_CREAT  创建
    12     O_EXCL   测试
    13     O_TRUNC  删除
    14 perms:
    15     被打开的文件的存取权限,采用8进制
    16 int close(int fd)
    17 ssize_t read(int fd, void *buf, size_t count)
    18 fd:
    19     文件描述符
    20 buf:
    21     指定存储器读取数据的缓冲区
    22 count:
    23     指定读取数据的字节数
    24 ssize_t write(int fd, void *buf, size_t count)
    25 fd:
    26         文件描述符
    27 buf:
    28         指定存储器读取数据的缓冲区
    29 count:
    30         指定读取数据的字节数
    31 off_t lseek(int fd, off_t offset, int whence)
    32 fd:
    33         文件描述符
    34 offset:
    35         偏移量,每一读写操作需要移动的字节数,可向前、可向后
    36 count:
    37         当前位置的基点:
    38         SEEK_SET(当前位置是文件的开头)
    39         SEEK_CUR(当前位置为文件指针的位置,新位置为当前位置加上偏移量)
    40         SEEK_END(当前位置问文件的尾部,新位置为文件大小加上偏移量的大小)
    41 **********************************************/
    42 int main(void)
    43 {
    44     int fd,len;
    45     char *buf = "Hello World!
    ",Out[30];
    46     fd = open("a.txt", O_CREAT | O_TRUNC | O_RDWR, 0600);
    47     printf("open file:a.txt fd = %d
    ", fd);
    48     len = strlen(buf);
    49     int size  = write(fd, buf, len);
    50     close(fd);
    51     //Begin to read the file
    52     fd = open("a.txt", O_RDWR, 0600);
    53     lseek(fd, 0, SEEK_SET); //Before to read the file,you should call the function to make the fd point to begin of files
    54     size = read(fd, Out, 12);
    55     printf("size = %d
    read from file:
     %s
    ",size,Out);
    56     close(fd);
    57     return 0;
    58 }

     

    实例1   读取一张通过MATLAB读取JPG图片转换成TXT文本的文件内容:

    首先图像是这样的lena.jpg:

          

    通过MATALB读取进去之后,转换成灰度图像,如下所示处理结果如上图所示:

    I = imread('F:LeanringCLearninglena.jpg');
    Gray = rgb2gray(I);
    imshow(Gray)

    接下来我们在变量一栏中,复制粘贴所有的数据到TXT文本当中,如下所示:

                            MATLAB数据                                                          文本数据

       

    这样,我们通过分析文本中的数据分布格式,首先,文本挡住的所有数据都是只包含了图像的数据的,不包括了JPG图片格式相关的数据内容,其次,在我们复制粘贴的过程中的每两个数据之间的分隔方式都是通过TAB键来分隔的,同样的在每一行数据的结尾部分,都是通过回车键 或者换行符 来结尾的,所以根据这样的数据格式,我们设计如下的读取对应文本内容的C语言函数API,这里的TAB在ASCII的编码数据是:9 同样的, 和 的ASCII的编码是10和13,这样的话,通过if就能隔离开数据。

    void ImageReadFromTXT(int *data,int width,int height,char *dir)
    {
        FILE *Pic;
        int D=0,count=0,Bit[3]={0},i,j;
        Pic = fopen(dir,"rb");
        for(i=0;i<height;i++)
        {
            D = 0;
            for(j=0;j<width;j++)
            {
                count = 0;
                Bit[0] = 0;
                Bit[1] = 0;
                Bit[2] = 0;
                D = 0;
                while(1)
                {
                    fread(&D,sizeof(char),1,Pic);
                    if(D == 9 || D == 10 || D == 13) break;// D == 9
                    Bit[count] = D-48;
                    count++;
                }
                *(data+i*width+j) = Bit[0]*100+Bit[1]*10+Bit[2];
            }
        }
        fclose(Pic);
    }

     主函数内容如下:

     1 /***********************************************************
     2 从TXT文件中读取一个图片文件的数据,图片文件的数据首先通过
     3 MATLAB读取到变量中,然后复制粘贴到TXT文件当中处理。
     4 ***********************************************************/
     5     int width=300;
     6     int height =300;
     7     int data[width][height];
     8     ImageReadFromTXT(data,width,height,"lena.txt");
     9     printf("The first data is:%d
    ",data[0][0]);
    10     printf("The first data is:%d
    ",data[0][1]);
    11     printf("The first data is:%d
    ",data[0][2]);
    12     printf("The first data is:%d
    ",data[0][3]);
    View Code

    实验结果:

  • 相关阅读:
    iOS跳转系统设置界面
    Swift中GCD与NSOperation相关
    将某字段按逗号分隔展示
    Java参考资料
    iOS消息推送相关
    [转]8 Regular Expressions You Should Know
    Oracle表锁住处理
    iframe自适应高度处理方案
    Mac常用软件推荐
    java代码块
  • 原文地址:https://www.cnblogs.com/uestc-mm/p/7630152.html
Copyright © 2011-2022 走看看