基于C的文件操作fopen fwrite fread fclose等

fopen

头文件:#include <stdio.h>

原型:FILE *fopen(const char *path, const char *mode);

参数:文件路径path;打开模式mode.

返回值:成功,返回FILE指针;失败,返回NULL.

功能:打开一个标准I/O流。

mode取值:

r 打开只读文件,该文件必须存在。
r+ 打开可读写的文件,该文件必须存在。
rb+ 读写打开一个二进制文件,只允许读写数据。
rt+ 读写打开一个文本文件,允许读和写。
w 打开只写文件,若文件存在则文件长度清为0,即该文件内容会消失。若文件不存在则建立该文件。
w+ 打开可读写文件,若文件存在则文件长度清为零,即该文件内容会消失。若文件不存在则建立该文件。
a 以附加的方式打开只写文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾,即文件原先的内容会被保留。(EOF符保留)
a+ 以附加方式打开可读写的文件。若文件不存在,则会建立该文件,如果文件存在,写入的数据会被加到文件尾后,即文件原先的内容会被保留。 (原来的EOF符不保留)
wb 只写打开或新建一个二进制文件;只允许写数据。
wb+ 读写打开或建立一个二进制文件,允许读和写。
wt+ 读写打开或着建立一个文本文件;允许读写。
at+ 读写打开一个文本文件,允许读或在文本末追加数据。
ab+ 读写打开一个二进制文件,允许读或在文件末追加数据。

fread fwrite

头文件:#include <stdio.h>

原型:size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

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

参数:要读写的数据指针ptr;要读写的块数nmemb;每块的字节数size;读写的文件指针stream.

返回值:成功,返回实际读的块数(不是字节数);如果返回与nmemb不相等,则可能到文件结尾或发生错误,从ferror和feof获取错误信息或检测是否到达文件结尾.

功能:对文件流stream读写操作。

举例:

/*
 ============================================================================
 Name        : FileOperator.c
 Author      : 
 Version     :
 Copyright   : www.cnblogs.com/vestinfo
 Description : The usage of fopen and fwrite.
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
	FILE *fp;

	if (!(fp = fopen("CreateByfopen.txt", "w+")))
	{
		perror("fopen");
	}

	char write_to_file[] = "The string is written to file.\n";

	if (fwrite(write_to_file, 1, strlen(write_to_file) + 1, fp) != (strlen(write_to_file) + 1))
	{
		perror("fwrite");
	}

//	if (fwrite(write_to_file, sizeof(write_to_file), 1, fp) != 1)
//	{
//		perror("fwrite");
//	}

	fclose(fp);

	return 0;
}

fclose

头文件:#include <stdio.h>

原型:int fclose(FILE *fp);

参数:要关闭的文件指针fp。

返回值:成功,返回0;出错,返回EOF,errno被设置。

功能:关闭文件。

在程序结束时一定要记得关闭打开的文件,不然可能会造成数据丢失的情况。

fseek

头文件:#include <stdio.h>

原型:int fseek(FILE *stream, long offset, int whence);

参数:要定位的文件流指针stream;偏移量offset;偏移起始位置whence.

返回值:成功,返回0;错误返回-1,并设置errno.

功能:定位文件内部的位置指针(不是文件指针)。如果执行成功,stream将指向以whence

(偏移起始位置:文件头SEEK_SET 0,当前位置SEEK_CUR 1,文件尾SEEK_END 2)为基准,

偏移offset(指针偏移量)个字节的位置。

如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置。

举例:

/*
 ============================================================================
 Name        : FileOperator.c
 Author      : 
 Version     :
 Copyright   : www.cnblogs.com/vestinfo
 Description : The usage of fwrite,fread and fseek.
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
	FILE *fp;

	if (!(fp = fopen("CreateByfopen.txt", "w+")))
	{
		perror("fopen");
	}

	char write_to_file[] = "The string is written to file.\n";

	if (fwrite(write_to_file, 1, strlen(write_to_file) + 1, fp) != (strlen(write_to_file) + 1))
	{
		perror("fwrite");
	}

	if (fseek(fp, 0, SEEK_SET) < 0)
	{
		perror("fseek");
	}

	char read_from_file[2046];
	if (fread(read_from_file, 1, strlen(write_to_file) + 1, fp) != (strlen(write_to_file) + 1))
	{
		perror("fread");
	}
	printf("read from file:%s", read_from_file);

	fclose(fp);

	return 0;
}

fputc  fgetc

头文件:#include <stdio.h>

原型:int fputc(int c, FILE *stream);

        int fgetc(FILE *stream);

参数:读写字符ASCII码c,读写文件流指针stream.

返回值:成功,返回该字符ASCII,错误返回EOF.

功能:对stream读写一个字符。

/*
 ============================================================================
 Name        : FileOperator.c
 Author      : 
 Version     :
 Copyright   : www.cnblogs.com/vestinfo
 Description : The usage of fputc and fgetc.
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
	FILE *fp;

	if (!(fp = fopen("CreateByfopen.txt", "w+")))
	{
		perror("fopen");
	}

	if (fputc('c', fp) < 0)
	{
		perror("fputc");
	}

	if (fseek(fp, 0, SEEK_SET) < 0)
	{
		perror("fseek");
	}

	char store_fgetc;
	if ((store_fgetc = fgetc(fp)) < 0)
	{
		perror("fgetc");
	}
	printf("fgetc:%c\n", store_fgetc);

	fclose(fp);

	return 0;
}

fgets  fputs

头文件:#include <stdio.h>

原型:

int fputs(const char *s, FILE *stream);

char *fgets(char *s, int size, FILE *stream);

参数:读写字符串s;文件流stream;从流中读出的字符个数size.

返回值:成功,返回s指针;失败,返回NULL.

功能:写一个字符串到流中。

从流中读一行或size-1字符。

注:如果size大于该行中实际字符个数,则读完改行所有字符。

如:

     如果一个文件的当前位置的文本如下

  Love ,I Have

  但是,如果用 

  fgets(str1,4,file1);

  则执行后str1="Lov",读取了4-1=3个字符,

  而如果用 

  fgets(str1,23,file1);

  则执行str1="Love ,I Have",读取了一行(包括行尾的'\n',并自动加上字符串结束符'\0')。

/*
 ============================================================================
 Name        : FileOperator.c
 Author      : 
 Version     :
 Copyright   : www.cnblogs.com/vestinfo
 Description : The usage of fputs and fgets.
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
	FILE *fp;

	if (!(fp = fopen("CreateByfopen.txt", "w+")))
	{
		perror("fopen");
	}

	if (fputs("I Love You", fp) == NULL)
	{
		perror("fputc");
	}

	if (fseek(fp, 0, SEEK_SET) < 0)
	{
		perror("fseek");
	}

	char store_fgets[4096];
	if ( fgets(store_fgets, 7, fp) == NULL)
	{
		perror("fgetc");
	}
	printf("fgets:%s\n", store_fgets);

	fclose(fp);

	return 0;
}

结果:

fgetc:I Love

fprintf    fscanf

用法通printf  scanf;只不过printf  scanf是写到控制台,fprintf    fscanf是写到流。

如:

fprintf(fp,"%2d%s",4,"Hahaha");

fscanf(fp,"%d%d" ,&x,&y);

feof

int feof(FILE *stream);

功能:检测是否已达文件尾,是返回非零值;否返回0.

/*
 ============================================================================
 Name        : FileOperator.c
 Author      : 
 Version     :
 Copyright   : www.cnblogs.com/vestinfo
 Description : The usage of feof.
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
	FILE *fp;

	fp = fopen("CreateByfopen.txt", "w+");

	fputs("I Love You", fp);

	printf("fgetc:%c\n", fgetc(fp));

	if (feof(fp))
	{
		printf("end of file\n");
	}
	else
	{
		printf("not end of file\n");
	}

	fseek(fp, 0, SEEK_SET);

	if (feof(fp))
	{
		printf("end of file\n");
	}
	else
	{
		printf("not end or error\n");
	}

	fclose(fp);

	return 0;
}

结果:

fgetc:�
end of file
not end or error

remove

原型:int remove(const char *path);

功能:删除文件。

如:remove("CreateByfopen.txt");

tmpfile

原型:FILE *tmpfile(void);

功能:生成一个临时文件,程序结束会自动删除。

如:FILE *fp = tmpfile();

原文地址:https://www.cnblogs.com/helloweworld/p/2731601.html