美文网首页
C语言文件读写方法

C语言文件读写方法

作者: 第八区 | 来源:发表于2017-07-13 14:14 被阅读97次

[TOC]

fwrite

size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

  • ptr:指向保存数据的指针;
  • size:每个数据类型的大小
  • count:数据的个数
  • stream:文件指针
  • return 函数返回写入数据的个数
int write(const char *path) {
    FILE *file = fopen(path, "wb");
    if (file == NULL) {
        return 0;
    }
    int arr[4] = {0x00000012, 0x00001234, 0x00123456, 0x12345678};
    for (int i = 0; i < 4; i++) {
        fwrite(&arr[i], sizeof(int), 1, file);
    }
    fclose(file);
    return 1;
}

查看输出的文件,看到数据的存储是小端对齐


1.png

w wb的区别

wb 打开或新建一个二进制文件,在POSIX系统,包括Linux都会忽略该字符。windows文本文件打开时写入\n,会自动加上\r变成\r\n。而已二进制方式打开则不会加上\r。

int write(const char *path) {
    FILE *file = fopen(path, "wb+");
//    FILE *file = fopen(path, "w");
    if (file == NULL) {
        return 0;
    }
    char *p = "abc\n1234";
    int len = fwrite(p, sizeof(char), strlen(p), file);
    printf("write len=%d\n", len);
    fclose(file);
    return 1;
}

使用wb+时候结果为:

write len=8
-------------
abc
12341234
read  length=8

使用w打开时,结果为:

write len=8
-------------
abc
1234123
read  length=9

fread

int read(const char *path) {
    FILE *file = fopen(path, "rb");
    if (file == NULL) {
        return 0;
    }
    int len = 0;
    int total = 0;
    char buf[5] = {0};
    while (!feof(file)) {
        len = fread(buf, sizeof(char), 4, file);
        printf("%s", buf, len);
        total += len;
    }
    printf("\nread  length=%d", total);
    fclose(file);
    return 1;
}

注意:fread返回成功有效的读取的item元素的个数。

这里修改写下代码:

#include <stdio.h>
#include <mem.h>

char *PATH1 = "D:\\code\\CProject\\FileByte\\1";

int read(const char *);

int write(const char *);

int main() {
    write(PATH1);
    printf("-------------\n");
    read(PATH1);
    return 0;
}

int write(const char *path) {
//    FILE *file = fopen(path, "wb+");
    FILE *file = fopen(path, "w");
    if (file == NULL) {
        return 0;
    }
    char *p = "abc\n1234";
    int len = fwrite(p, sizeof(char), strlen(p), file);
    printf("write len=%d\n", len);
    fclose(file);
    return 1;
}

int read(const char *path) {
    FILE *file = fopen(path, "rb");
    if (file == NULL) {
        return 0;
    }
    int len = 0;
    int total = 0;
    //使用short
    short buf[20] = {0};
    while (!feof(file)) {
        len = fread(buf, sizeof(short), 20, file);
        for (int i = 0; i < len + 2; i++) {
            printf("%x-", buf[i]);
        }
        total += len;
    }
    printf("\nread  length=%d", total);
    fclose(file);
    return 1;
}

结果为:

write len=8
-------------
6261-d63-310a-3332-34-0-
read  length=4

总共9个字节,而实际有效读入了4个short。


2.png

相关文章

  • C语言文件读写方法

    [TOC] fwrite size_t fwrite ( const void * ptr, size_t siz...

  • C语言读写文件

    C语言文件读写### 标准文件读写 非标准文件读写 标准文件读写 头文件 include 打开文件 函数原型:FI...

  • C语言_文件

    @(C语言) [toc] 读文件 写文件 读写二进制文件

  • C语言读写文件

    从键盘输入字符后,写入到磁盘文件datafile1.txt中 读出磁盘文件datafile.txt中的内容,将它们...

  • C语言读写文件

    fread函数和fwrite函数 1.函数功能 用来读写一个数据块。 2.一般调用形式 fread(buffer,...

  • C语言读写文件

    C读写文件 使用 fopen(文件名,访问模式) 函数来打开文件,一般有getc/putc , fgets/fpu...

  • 【NDK 7】 c/c++ 文件和流操作

    C 语言的文件读写操作 头文件 : stdio.h 函数原型:FILE * fopen(const char * ...

  • python 学习笔记 023

    本节内容:文件读写 文件读写的方法和C兼容,通过文件描述符修改文件 1.读取文件 过程:1、打开文件2、读文件内容...

  • Python:文件的读写

    任何编程语言都会涉及到文件的读写操作,在python中文件的读写与c语言有异曲同工之妙。 在对文件的处理中,一般分...

  • pread()函数

    C语言lseek()函数:移动文件的读写位置 头文件: #include #include 定义函数: off_t...

网友评论

      本文标题:C语言文件读写方法

      本文链接:https://www.haomeiwen.com/subject/mgmwhxtx.html