目录
4.二进制输入输出(只适用于文件):fread()、fwrite()
>>> 文件操作一般流程:
1.创建文件指针:FILE* pf
2.打开文件:pf = fopen( 文件名,打开方式 )
3.判断文件打开是否成功:if (pf == NULL)
4.文件操作
5.关闭文件:fclose(pf)
>>> 文件的顺序读写
1.字符输入输出:fgetc()、fputc()
函数原型:#include <stdio.h>
int fgetc( FILE *stream );
返回值:fgetc return the character read as an int or return EOF to indicate an error or end of file.
int fputc( int c, FILE *stream );
返回值:The function returns the character written. For fputc a return value of EOF indicates an error
#include <stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//从test中读取一个字符
int ret = fgetc(pf);
printf("%c\n", ret);
ret = fgetc(pf);
printf("%c\n", ret);
//fgetc函数也可以从键盘读取字符
/*ret = fgetc(stdin);
printf("%c\n", ret);*/
fclose(pf);
pf = NULL;
return 0;
}
#include <stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//往test中写一个字符
int ret = fputc('v', pf);
//fputc函数也可以在屏幕显示字符
ret = fputc('n', stdout);
fclose(pf);
pf = NULL;
return 0;
}
2.文本行输入输出:fgets()、fputs()
函数原型:#include <stdio.h>
char *fgets( char *string, int n, FILE *stream );
返回值:returns string. NULL is returned to indicate an error or an end-of-file condition
int fputs( const char *string, FILE *stream );
返回值:returns a nonnegative value if it is successful. On an error, fputs returns EOF
#include <stdio.h>
int main()
{
char str[10] = { 0 };
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//注意预留\0的位置,下面代码表示取3个字符
fgets(str, 4, pf);
printf("%s\n", str);
fgets(str, 4, stdin);
printf("%s\n", str);
fclose(pf);
pf = NULL;
return 0;
}
#include <stdio.h>
int main()
{
char str[10] = { 0 };
FILE* pf = fopen("test.txt", "a");
if (pf == NULL)
{
perror("fopen");
return 1;
}
fputs("qwert\n", pf);
fputs("asdfg\n", pf);
fputs("qwert\n", stdout);
fclose(pf);
pf = NULL;
return 0;
}
3.格式化输入输出:fscanf()、fprintf()
函数原型:#include <stdio.h>
int fscanf( FILE *stream, const char *format [, argument ]... );
int fprintf( FILE *stream, const char *format [, argument ]...);
#include <stdio.h>
struct s
{
char arr[10];
int num;
float scr;
};
int main()
{
struct s st = { "abcdef", 10, 5.5f };
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//写文件
fprintf(pf, "%s %d %f", st.arr, st.num, st.scr);
//也可以屏幕显示
fprintf(stdout, "%s %d %f", st.arr, st.num, st.scr);
fclose(pf);
pf = NULL;
return 0;
}
#include <stdio.h>
struct s
{
char arr[10];
int num;
float scr;
};
int main()
{
struct s st;
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//读文件
fscanf(pf, "%s %d %f", st.arr, &(st.num), &(st.scr));
printf("%s %d %f\n", st.arr, st.num, st.scr);
//也可以键盘输入
fscanf(stdin, "%s %d %f", st.arr, &(st.num), &(st.scr));
printf("%s %d %f\n", st.arr, st.num, st.scr);
fclose(pf);
pf = NULL;
return 0;
}
4.二进制输入输出(只适用于文件):fread()、fwrite()
函数原型:#include <stdio.h>
size_t fread( void *buffer, size_t size, size_t count, FILE *stream );
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
#include <stdio.h>
struct s
{
char arr[10];
int num;
float scr;
};
int main()
{
struct s st = { "abcdef", 10, 5.5f };
FILE* pf = fopen("test.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//二进制形式写文件
fwrite(&st, sizeof(st), 1, pf);
fclose(pf);
pf = NULL;
return 0;
}
#include <stdio.h>
struct s
{
char arr[10];
int num;
float scr;
};
int main()
{
struct s st;
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
//读文件
fread(&st, sizeof(st), 1, pf);
printf("%s %d %f\n", st.arr, st.num, st.scr);
fclose(pf);
pf = NULL;
return 0;
}
>>> 文件的随机读写
1.fseek函数
-------根据文件指针的位置和偏移量来定位文件指针
int fseek ( FILE * stream, long int offset, int origin );
位置(origin):SEEK_CUR----Current position of file pointer
SEEK_END----End of file
SEEK_SET----Beginning of file
2.ftell函数
-------返回文件指针相对于起始位置的偏移量
long int ftell ( FILE * stream );
3.rewind函数
-------让文件指针的位置回到文件的起始位置
void rewind ( FILE * stream );
#include <stdio.h>
int main()
{
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
int ch;
//调整文件指针
fseek(pf, -2, SEEK_END);
//读取
ch = fgetc(pf);
printf("%c\n", ch);
//计算文件指针偏移量
int ret = ftell(pf);
printf("%d\n", ret);
//让文件指针回到起始位置
rewind(pf);
ch = fgetc(pf);
printf("%c\n", ch);
fclose(pf);
pf = NULL;
return 0;
}