文件操作 fread fwrite fscanf fprintf fseek feof fgets fputs rewind


fopen:打开文件
FILE * fopen(const char * path, const char * mode);
常用,就不介绍了哈。

fscanf:将文件中的内容写入字符串或数字。
int fscanf(FILE * stream, const char * format, [argument...]);

FILE * stream,文件指针
const char * format,格式字符串
[argument...],输入列表
例子:
文档1.txt内容如下
123

zxc

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
	int i  =0;
	char c[10];
	FILE *p;
	p = fopen("1.txt", "a+");
	if (p == nullptr)
		printf("NULL\n");
 
	fscanf(p,"%d %s",&i,c);
	printf("%d\n%s\n", i,c);
 	getchar();
        fclose(p);
	return 0;
}
结果如下:
123

zxc


fprintf:将格式化的字符串写入文件。
int fprintf(FILE*stream,const char*format,[argument]……)
返回值是写入的字符数,发生错误返回负值。

例子:

#include <stdio.h>  
int main(void)   
{  
    FILE *f;  
    char str[] = "Hello World!";  
    f = fopen("Hello.txt", "w+");  
    int num = fprintf(f, "%s", str);  
    printf("num=%d\n",num);  
    getchar(); 
    fclose(p);
    return 0;  
}  
结果如下:
num=12
 
fread:用于从文件流中读取数据
size_t fread(void * ptr, size_t size, size_t nmemb, FILE * stream);

stream ,打开的文件指针, 
ptr, 指向欲存放读取进来的数据空间, 读取的字符数以参数size*nmemb 来决定. 
Fread()会返回实际读取到的nmemb 数目, 如果此值比参数nmemb 来得小, 则代表可能读到了文件的尾或有错误发生, 这时必须用feof()或ferror()来决定发生什么情况.

例子:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
	int i = 0;
	char c[10];
	memset(c,0, 10);
	FILE *p;
	p = fopen("1.txt", "r");
	int rs = feof(p);
	printf("%d\n", rs);

	if (p == nullptr)
		printf("NULL\n");

	fread(c, 1, 10, p);
	printf("%s\n",  c);
	rs = feof(p);
	printf("%d\n", rs);
 	getchar();
        fclose(p);
	return 0;
}
结果如下:
0
123
zxc
1


fwrite:向指定的文件中写入若干数据块。
size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);

例子:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
 	char c[] = "hello world";
	char _c[15];
 	FILE *p;
	p = fopen("h.txt", "w+"); 

	fwrite(c, 1, strlen(c), p);
 	fread(_c, 1, strlen(c), p);
	printf("%s\n", c);

	getchar();
        fclose(p);
	return 0;
}
feof:判断是否到达文件结尾
int feof(FILE * stream);

到达文件尾返回非0,其他情况返回0.

fgets:从文件流读取数据,每次读一行。没读完下一次接着这一行读。
char *fgets(char *buf, int bufsize, FILE *stream);

例子:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
	char c[20];
 	char _c[20];
	FILE *p;
	p = fopen("1.txt", "r");

 	fgets(c, 8, p);
	printf("%s\n", c);

	fgets(_c, 8, p);
	printf("%s\n", _c);

	getchar();
        fclose(p);
	return 0;
}
结果如下:
1234567
89abcde


fputs:把字符串写入到指定的流( stream) 中,但不包括空字符。
int fputs(const char *str, FILE *stream);

例子:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
	char c[] = "hello world";
	char _c[20];
 	FILE *p;
	p = fopen("1.txt", "w+");
 
	fputs(c, p);
	fseek(p, 0, 0);//重定位
	fgets(_c, 20, p);
	printf("%s\n", _c);
	fclose(p);
	getchar();
	return 0;
}
结果如下:
hello world

fseek:重定位流上的文件内部位置指针
执行后,stream将指向以fromwhere为基准,偏移offset(指针偏移量)个字节的位置,函数返回0。

int fseek(FILE *stream, long offset, int fromwhere);
offset,偏移量,正数正向偏移,负数负向偏移。
fromwhere,从哪里偏移,
SEEK_SET: 文件开头,0
SEEK_CUR: 当前位置,1
SEEK_END: 文件结尾,2

例子:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
	int i = 0;
	char c[10];
	memset(c,0, 10);
	FILE *p;
	p = fopen("1.txt", "r"); ;
	if (p == nullptr)
		printf("NULL\n");
	fread(c, 1, 10, p);
	printf("%s\n",  c); 
	fseek(p, 0, 0);
	fread(c, 1, 10, p);
	printf("%s\n", c);

 	getchar();
        fclose(p);
	return 0;
}
结果如下:
123
zxc
123

zxc

rewind:设置文件位置为给定流 stream 的文件的开头。

void rewind(FILE * stream);

例子:

#include <stdio.h>
 int main()
{
	char str[] = "rewind test";
	FILE *fp;
	char ch[20];
 
 	fp = fopen("file.txt", "w+");
	fwrite(str, 1, sizeof(str), fp);
	rewind(fp);//写入后文件位置指针变化,定位到开头

	fgets(ch,12,fp); 
	printf("%s\n", ch);
	 
	rewind(fp);//定位到开头
 
	fgets(ch, 15, fp);
 	printf("%s\n", ch);

 	fclose(fp);
	getchar();
	return(0);
}

结果如下:

rewind test

rewind test

猜你喜欢

转载自blog.csdn.net/znzxc/article/details/80462740