FILE fopen, fseek and other learning

1. fopen() function:

1. Function: In C language, the fopen() function is used to open the file of the specified path and obtain the pointer to the file.

2. Function prototype:

FILE * fopen(const char * path,const char * mode);
    -- path: 文件路径,如:"F:\Visual Stdio 2012\test.txt"
    -- mode: 文件打开方式,例如:
             "r" 以只读方式打开文件,该文件必须存在。
             "w" 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。
            "w+" 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。
             "a" 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。(EOF符保留)
             "a+" 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。(原来的EOF符不保留)
            "wb" 只写打开或新建一个二进制文件,只允许写数据。
            "wb+" 读写打开或建立一个二进制文件,允许读和写。
             "ab" 追加打开一个二进制文件,并在文件末尾写数据。
             "ab+"读写打开一个二进制文件,允许读,或在文件末追加数据。   
    --返回值: 文件顺利打开后,指向该流的文件指针就会被返回。如果文件打开失败则返回NULL,并把错误代码存在errno中。        

2. fwrite() function:

1. Function: In the C language, the fwrite() function is commonly used to write the data in a memory area to the local text.

2. Function prototype:

size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
    -- buffer:指向数据块的指针
    -- size:每个数据的大小,单位为Byte(例如:sizeof(int)就是4)
    -- count:数据个数
    -- stream:文件指针

Note: The return value varies with the format of the call:

    (1) Call format: fwrite(buf,sizeof(buf),1,fp);

    Successful writing returns a value of 1 (ie count)


    (2) Call format: fwrite(buf,1,sizeof(buf),fp);

    If it is successfully written, it will return the number of data actually written (unit: Byte)

3. Precautions:

    After writing the data, call fclose() to close the stream. If the stream is not closed, after each data read or write, the file pointer will point to the next data location to be written or read.

Example explanation:

Code 1: The following code can write 1024 characters (int) into a text file, in the call of fwrite, size is sizeof(int), count is DATA_SIZE
 

#include "stdafx.h"
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define DATA_SIZE 1024
 
int main()
{
    unsigned int *dataPtr = NULL;
    dataPtr = (unsigned int *)malloc(sizeof(int)*DATA_SIZE);
    for(unsigned int i=0;i<DATA_SIZE;i++)
    {
        dataPtr[i] = i; //初始化缓存区
    }
    FILE *fp = fopen("F:\\Labwindows cvi\\test.txt","w");
    fwrite(dataPtr,sizeof(int),DATA_SIZE,fp);
       fclose(fp);
       free(dataPtr);
    system("pause");
    return 0;
}

3. fread() function:


1. Function: read data from a file stream
2. The function prototype is as follows:
 

	size_t fread(void *buffer, size_t size, size_t count, FILE *stream);
	  -- buffer:指向数据块的指针
	  -- size:每个数据的大小,单位为Byte(例如:sizeof(int)就是4)
	  -- count:数据个数
	  -- stream:文件指针

4. fseek function

In the FILE file structure, there is a pointer, which will move every time the read and write function of the file is called;

Such as fgets / fputs, getc / putc, fscanf / fprintf, fread / fwrite and other functions;

By default, the pointer moves from front to back;

The position pointed by the pointer inside the file can be changed by the fseek function;


The prototype of the fseek function is as follows: Reset the position of the internal pointer of the file;

#include <stdio.h>
int fseek(FILE *stream, long offset, int fromwhere);

The position of the set pointer is the starting position + offset;

The int fromwhere parameter is the starting position, and there are three options:

File header SEEK_SET 0
current position SEEK_CUR 1
file end SEEK_END 2

long offset offset parameter, which can be positive or negative;

If the execution is successful, it returns 0, if it fails, it returns non-0, and sets the error code;

Example of fseek function code:  first write out 10 bytes of data, then read the first 2 bytes normally, and finally skip 4 bytes and read 2 bytes;

#include <stdio.h>

int main()
{
    // 以写文本的方式向文件中写出数据
    FILE *p = fopen("D:/File/number.dat", "w");
    // 写出 10 个字符
    char array[10] = {1,2,3,4,5,6,7,8,9,10};
    // 将 10 个字符写出到文件中
    fwrite(array, 1, sizeof(array), p);
    // 关闭文件
    fclose(p);


    // 正常读取文件前 2 字节
    FILE *p1 = fopen("D:/File/number.dat", "r");
    // 读取数据缓冲区
    char array1[2];
    // 读取前 2 字节
    fread(array1, 1, sizeof(array1), p1);
    // 打印前 2 字节数据
    printf("%d , %d\n", array1[0], array1[1]);


    // 跳过 4 字节读取文件 2 字节
    FILE *p2 = fopen("D:/File/number.dat", "r");
    // 读取数据缓冲区
    char array2[2];

    // 跳过 4 字节 , 此时当前位置是 0
    fseek(p2, 4, SEEK_CUR);

    // 读取 2 字节
    fread(array2, 1, sizeof(array2), p2);
    // 打印前 2 字节数据
    printf("%d , %d", array2[0], array2[1]);


    return 0;
}

おすすめ

転載: blog.csdn.net/cyy1104/article/details/130557261