文章目录
带缓存 I/O 操作
带缓存 I/O 操作涉及到的函数有:fopen
、fdopen
、fread
、fwrite
、fseek
、fclose
等;这些函数处理的都是文件流;
fopen 函数
fopen 函数用于打开文件,具体的参数如下表所示:
fdopen 函数
fdopen
函数用于将文件描述词转化为文件流指针,具体参数见下表:
例如:
// fdopen.c
#include <stdio.h>
int main()
{
FILE *fp = fdopen(0, "w+");
fprintf(fp, "%s\n", "hello!");
fclose(fp);
}
fread 函数
fread
从文件流中读取数据,具体参数见下表:
以下是演示的例子:
// fread.c
#include <stdio.h>
#define nmemb 3
struct test {
int size;
char name[20];
}s[nmemb];
int main()
{
int i;
FILE *stream = NULL;
stream = fopen("./temp", "r");
fread(s, sizeof(struct test), nmemb, stream);
fclose(stream);
for(i = 0; i < nmemb; i++)
printf("name[%d] = %-20s:size[%d]=%d\n", i, s[i].name, i, s[i].size);
return 0;
}
fwrite 函数
fwrite
函数用于将数据写入文件流,具体参数如下所示:
例子如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define set_s(x, y) {strcpy(s[x].name, y);s[x].size=strlen(y);}
#define nmemb (3)
struct test{
int size;
char name[20];
}s[nmemb];
int main()
{
FILE *stream;
set_s(0, "Linux");
set_s(1, "FreeBSD");
set_s(2, "Windows2000.");
stream = fopen("./temp", "w");
fwrite(s, sizeof(struct test), nmemb, stream);
fclose(stream);
}
fseek 函数如下所示
fseek
用来重定位文件流指针,具体参数如下:
例子如下:
// fseek.c
#include <stdio.h>
int main()
{
FILE *stream;
long offset;
fpos_t pos;
stream = fopen("/etc/passwd", "r");
fseek(stream, 5, SEEK_SET);
printf("%s: %d: pffset = %ld\n", __FILE__, __LINE__, ftell(stream));
rewind(stream);
fgetpos(stream, &pos);
printf("%s: %d: offset = %d\n", __FILE__, __LINE__, pos);
pos.__pos = 10;
fsetpos(stream, &pos);
printf("%s: %d: offset = %d\n", __FILE__, __LINE__, ftell(stream));
fclose(stream);
return 0;
}
fclose 函数
fclose
用来关闭文件流,具体参数如下所示:
fgetc 函数、getc 函数 和 getchar 函数
fgetc
函数从流中读取一个字符,是函数;getc
函数从流中读取一个字符,是宏定义;getchar
函数从标准输入设备读取一个字符,不是函数,是getc(stdin)
宏定义;
函数类别 | fgetc 函数 | getc 函数 | getchar 函数 |
---|---|---|---|
相关函数 | fopen、fread、fscanf、getc | fopen、fread、fgetc、read | fopen、fread、fscanf、getc |
头文件 | #include < stdio.h > | #include < stdio.h > | #include < stdio.h > |
定义 | int fgetc(FILE *stream) | int getc(FILE *stream) | int getchar(void) |
函数说明 | 从流中读取一个字符; 若到文件末尾,返回 EOF; | 与 fgetc() 相同; 但 getc() 是宏定义,不是函数 | 从标准输入设备读取一个字符; 然后将字符从 unsigned char 转换成 int 后返回; |
返回值 | 若读到字符,返回字符; 若到文件末尾,返回 EOF; | 与 fgetc() 相同 | 返回读取的字符; 若返回 EOF 则标识有错误发生 |
附加说明 | getchar() 非真正函数,而是 getc(stdin) 的宏定义 |
例子如下:
#include <stdio.h>
int main()
{
FILE *fp = NULL;
int c, i;
fp = fopen("./temp", "r");
while((c=fgetc(fp)) != EOF)
printf("%c", c);
fclose(fp);
printf("\n");
for(i = 0; i < 5; i++) {
c = getchar();
putchar(c);
}
printf("\n");
return 0;
}
fputc函数、putc函数和 putchar 函数
fputc
将指定的字符写入文件流中,是函数;putc
将指定的字符写入文件流中,是宏定义;putchar
将字符写到标准输出设备,不是函数,是putc(stdout)
宏定义;
函数类别 | fputc 函数 | putc 函数 | putchar 函数 |
---|---|---|---|
相关函数 | fopen、fwrite、fscanf、putc | fopen、fwrite、fscanf、fputc | fopen、fwrite、fscanf、fputc |
头文件 | #include < stdio.h > | #include < stdio.h > | #include < stdio.h > |
定义 | int fputc(int c, FILE *stream) | int putc(int c, FILE *stream) | int putchar(void) |
函数说明 | 将参数 c 转为 unsigned char 后写入参数 stream ; | 与 fputc() 相同; 但 putc() 是宏定义,不是函数 | putchar 用来将参数 c 字母写到标准输出设备 |
返回值 | 返回成功写入的字符; 返回 EOF,表示写入出错; | 与 fgetc() 相同 | 返回写入的字符; 若返回 EOF 则标识有错误发生 |
附加说明 | putchar() 非真正函数,而是 putc(c, stdout) 的宏定义 |
例子如下:
// _putc_.c
#include <stdio.h>
int main()
{
FILE *fp = NULL;
char a[26] = "abcdefghijklmnopqrstuvwxyz";
int i;
fp = fopen("./temp", "w");
for(i = 0; i < 26; i++)
fputc(a[i], fp);
fclose(fp);
}