主要讲解系统提供的读写文件的函数简单使用方式,以及简单的示例代码与运行结果

fgetcfputc

此函数主要用于字符的读入与写出出,一般一次读入或写出一个字符。

函数原型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// focus 重点
int fgetc(FILE *stream);

int getc(FILE *stream);

int getchar(void);

int ungetc(int c, FILE *stream);


// focus 重点
int fputc(int c, FILE *stream);

int putc(int c, FILE *stream);

int putchar(int c);

函数介绍

函数描述

1
2
3
4
5
6
7
8
9
10
11
12
13
fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error.

getc() is equivalent to fgetc() except that it may be implemented as a macro which evaluates stream more than once.

getchar() is equivalent to getc(stdin).

ungetc() pushes c back to stream, cast to unsigned char, where it is available for subsequent read operations. Pushed-back characters will be returned in reverse order; only one pushback is guaranteed.

fputc() writes the character c, cast to an unsigned char, to stream.

putc() is equivalent to fputc() except that it may be implemented as a macro which evaluates stream more than once.

putchar(c); is equivalent to putc(c,stdout).

简单解释:

fgetc() fgetc()从流中读取下一个字符,并将其作为 unsigned char 类型转换返回 int ,或文件末尾的 EOF 或 error 。

getc() 简单意思就是和fgetc()函数实现一样,但是使用的时候需要注意宏问题。

查看其实现可以看到其宏定义为#define getc(_fp) _IO_getc (_fp),跟踪代码发现在 libio.h 中 extern int _IO_getc (_IO_FILE *__fp); 是用函数实现的。因而在 linux 中他们两没啥区别。此处详解可以查看引用内容!

getchar() 函数等价于getc(stdin)

ungetc() 将c推回流,强制转换为 unsigned char ,以便后续的读操作使用 c 。

fputc() 将字符 c(强制转换为 unsigned char )写入流

putc()简单意思就是和fputc()函数实现一样,但是使用的时候需要注意宏问题。

putchar(c);等价于putc(c,stdout)

返回值介绍

1
2
3
4
fgetc(), getc() and getchar() return the character read as an unsigned char cast to an int or EOF on end of file or
error.

ungetc() returns c on success, or EOF on error.

简单解释:

fgetc(), getc()getchar() 读取的字符将其转换为 int 类型进行返回,或者 EOF 或者 error

ungetc() 成功时返回 c ,错误时返回 EOF

fgetsfputs

此函数主要用于字符串的读入与写出,主要以字符串为单位。

函数原型

1
2
3
4
5
6
7
8
9
10
11
12

// focus 重点
char *fgets(char *s, int size, FILE *stream);

char *gets(char *s);



// focus 重点
int fputs(const char *s, FILE *stream);

int puts(const char *s);

关于fgets函数参数解释如下:

  • s : 用于存放数据的字符串指针
  • size : 最多读取多少个字符
  • stream : 读取文件

函数介绍

函数描述

1
2
3
4
5
6
7
8
9
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s.  Reading stops after an EOF or a newline.  If a newline is read, it is stored  into  the
buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with a null byte ('\0'). No check for buffer overrun is performed (see BUGS
below).

fputs() writes the string s to stream, without its terminating null byte ('\0').

puts() writes the string s and a trailing newline to stdout.

简单解释:

fgets() 从文件中读取最多或少于 size 的字符,并存储到 s 的 buffer 中。读取过程中遇到EOF或换行符结束。

gets() 从 stdin (标准输入)中读取一行直到遇到新的一行或 EOF(结束符)为止,并用(’\0’)替换。不检查缓存是否溢出。

fputs() 写一个字符串到流中,不包含终止空字节(‘\0’)。

puts() 将字符串 s 和末尾换行符写入标准输出。

返回值介绍

1
2
3
gets() and fgets() return s on success, and NULL on error or when end of file occurs while no characters have been read.

puts() and fputs() return a nonnegative number on success, or EOF on error.

简单解释:

gets()fgets() 成功返回读取的字符串指针,失败和遇到结束符都返回 NULL。

如果成功,puts()fputs() 返回一个非负数,如果错误,则返回EOF。

简单且可运行代码

C中最快速的读写文本中的一行字符。

环境为linux环境,text.txt文件中你随便写小于100字符的内容即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define MAX_ARRAY 100

int main(int argc, char *argv[]) {

char *list = NULL, line[MAX_ARRAY] = {0};
FILE *in_file = fopen("./text.txt", "r+");

int rs = 1;
while((list = fgets(line, 100, in_file)) != NULL) {
printf("读取内容:%s\n", line);
printf("读取内容list:%s\n", list);
}

fclose(in_file);
return 0;
}

freedfwrite

此函数主要用于数据块的读入与写出,数据块的含义是二进制。

函数原型

1
2
3
4
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);

函数解释

函数描述

1
2
3
The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.

The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.

简单解释:

fread() 函数从 stream 所指向的流中,读取 nmemb 个元素的数据,其中每个元素的大小为size大小。

fwrite() 函数将写 nmemb 个元素的数据到 stream 所指向的流中,其中每个元素的大小为 size 大小。

返回值介绍

1
2
3
On  success,  fread()  and  fwrite()  return  the  number  of items read or written.  This number equals the number of bytes transferred only when size is 1.  If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).

fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.

简单解释:

如果成功,fread()fwrite() 返回已读或已写的条目数。这个数字仅在 size 为 1 时等于传输的字节数。如果发生错误,或者到达文件的末尾,返回值是短项计数(或零)。

fread() 不区分文件结束和错误,调用者必须使用 feof(3)ferror(3) 来确定发生了什么。

简单且可运行代码

C中读取视频流文件,直接将流文件全部加载到内容中并输出。

环境为linux环境。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<malloc.h>

int main(int argc, char *argv[]) {
FILE *file = fopen("./lena_256x256_yuv420p.yuv", "r+");
unsigned char *buffer = (unsigned char*) malloc(256*256*3/2);

fread(buffer, 1, 256*256*3/2, file);
// 注意:256*256*3/2 != malloc_usable_size(buffer)
// 因为malloc会创建的会进行对齐,为了保证对齐那么就会增加8bytes,98304 + 8 = 98312
printf("%d\n",malloc_usable_size(buffer));
int i = 0;
for(i = 0; i< 256*256*3/2; i++) {
printf("%c", *buffer++);
}

free(buffer);
fclose(file);
}

其中关于对齐的内容可以看C语言中的字节对齐

fscanffprintf

此函数主要用于格式化读取与写出数据内容,主要侧重于格式化。数据类型以格式化要求为准。

函数原型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// #include <stdio.h>
int scanf(const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
int sscanf(const char *str, const char *format, ...);

//#include <stdarg.h>

int vscanf(const char *format, va_list ap);
int vsscanf(const char *str, const char *format, va_list ap);
int vfscanf(FILE *stream, const char *format, va_list ap);



//#include <stdio.h>
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);

// #include <stdarg.h>

int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);

函数解释

函数描述

1
2
3
4
5
The scanf() function reads input from the standard input stream stdin, fscanf() reads input from the stream pointer stream, and sscanf() reads its input from the character string pointed to by str.

The vfscanf() function is analogous to vfprintf(3) and reads input from the stream pointer stream using a variable argument list of pointers (see stdarg(3). The vscanf() function scans a variable argu‐ment list from the standard input and the vsscanf() function scans it from a string; these are analogous to the vprintf(3) and vsprintf(3) functions respectively.

The functions in the printf() family produce output according to a format as described below. The functions printf() and vprintf() write output to stdout, the standard output stream; fprintf() and vfprintf() write output to the given output stream; sprintf(), snprintf(), vsprintf() and vsnprintf() write to the character string str.

引用

C语言中fgetc、fputc和getc、putc的区别是什么

fgetc和getc,哪个速度快?