一: – fgets( )
#include <stdio.h>
char *fgets(char *s, int size, FILE *stream);
功能:从文件中读取内容
参数:
s:保存读取到的内容
size:每次读取的最大个数
stream:文件指针
返回值:
成功:读取的数据的首地址
失败:NULL 如果文件内容读取完毕,也返回NULL
案例:
#include <stdio.h>#include <string.h>
int main(int argc, char const *argv[]){
//使用fgets从终端读取数据#if 0
//注意事项:
// 如果输入的字符串的长度小于fgets的第二个参数,则会将换行符也当做一个字符保存在第一个参数里面
//char buf[32] = {0};
//fgets(buf, sizeof(buf), stdin); //"hello world\n"
//处理由fgets从终端获取字符串中的\n
//buf[strlen(buf) - 1] = '\0';
// 如果输入的字符串的长度大于fgets的第二个参数,则只能读取到第二个参数-1个字符,最后一个位置自动补\0
//char buf[32] = {0}; //fgets(buf, 5, stdin);
// 如果保存fgets读取数据的字符串不够大,则会出现内存溢出的错误,所以保证第一个参数足够大
char buf[5] = {0};
fgets(buf, 10, stdin);
printf("buf = %s\n", buf);#endif
if(argc < 2)
{
fprintf(stderr, "Usage: %s filename\n", argv[0]);
return -1;
}
//使用fgets从文件中读取数据
FILE *fp;
if((fp = fopen(argv[1], "r")) == NULL)
{
perror("fail to fopen");
return -1;
}
//注意:如果fgets从文件中读取内容
// 1、不管一次读多少个字节,只要遇到每一行的行结束符\n,就会立即结束读取
// 2、如果fgets的第二个参数小于一行的内容,只会读取fgets的第二个参数-1个字符,最后一个位置补\0
#if 0
char buf[32] = {};
fgets(buf, 8, fp);
printf("[%s]\n", buf);
fgets(buf, 8, fp);
printf("[%s]\n", buf);
#endif
char buf[32] = {0};
while(fgets(buf, sizeof(buf), fp) != NULL)
{
printf("%s", buf);
}
return 0;
}
二:-- fputs( )
#include <stdio.h>
int fputs(const char *s, FILE *stream);
功能:向文件写入数据
参数:
s:要写入的内容
stream:文件指针
返回值:
成功:写入文件内容的字节数
失败:EOF
案例:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char const *argv[]){
//使用fputs向终端写入数据
//fputs("hello world", stdout);
//fflush(stdout);
//sleep(10);
//使用fputs向文件写入数据
FILE *fp;
if((fp = fopen("file.txt", "w+")) == NULL)
{
perror("fail to fopen");
return -1;
}
fputs("hello world\n", fp);
fputs("nihao beijing\n", fp);
//由于fputs写入内容后改变了文件的偏移量,所以此时文件的偏移量在最后一个字节的下一个位置
//所以读取数据是什么也读取不到
// char buf[32] = {0};
// fgets(buf, 32, fp);
// printf("buf = %s\n", buf);
return 0;}
三:-- fread( )
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:从文件中读取数据
参数:
ptr:保存读取的数据
size:每次读取的字节数
nmemb:一共读取的次数
stream:文件指针
返回值:
成功:实际读取的次数(对象数、块数)
失败:0 如果文件内容读取完毕,返回0
案例:
#include <stdio.h>
typedef struct{
int a;
int b;
char c;
char d[32];}MSG;
int main(int argc, char const *argv[]){
//使用fread从文件中读取数据
FILE *fp;
if((fp = fopen("file.txt", "r")) == NULL)
{
perror("fail to fopen");
return -1; }
// char buf[32] = {0};
// fread(buf, 2, 5, fp);
// printf("buf = %s\n", buf);
// int num;
// fread(&num, 4, 1, fp);
// printf("num = %d\n", num);
// int b[4] = {0};
// fread(b, 4, 4, fp);
// printf("%d %d %d %d\n", b[0], b[1], b[2], b[3]);
MSG msg;
fread(&msg, sizeof(msg), 1, fp);
printf("%d - %d - %c - %s\n", msg.a, msg.b, msg.c, msg.d);
return 0;
}
四:-- fwrite( )
#include <stdio.h>
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:向文件中写入数据
参数:
ptr:要写入的数据
size:一次写入的字节数
nmemb:一共写入的次数
stream:文件指针
返回值:
成功:实际写入的次数
失败:0
案例
#include <stdio.h>
typedef struct{
int a;
int b;
char c;
char d[32];}MSG;
int main(int argc, char const *argv[])
{
//使用fwrite向文件写入数据
FILE *fp;
if((fp = fopen("file.txt", "w")) == NULL)
{
perror("fail to fopen");
return -1;
}
//写入字符串
//char buf[] = "1234567890";
//fwrite(buf, 2, 5, fp);
//写入整数
//int a = 97868;
//fwrite(&a, 4, 1, fp);
//写入数组
//int a[4] = {100, 200, 300, 400};
//fwrite(a, 4, 4, fp);
//写入结构体
MSG msg = {666, 888, 'w', "zhangsan"};
fwrite(&msg, sizeof(msg), 1, fp);
return 0;
}
五:-- ftell( )
#include <stdio.h>
long ftell(FILE *stream);
功能:获取当前文件的偏移量
参数:
stream:文件指针
返回值:
获取当前文件的偏移量
六:-- fseek()
#include <stdio.h>
int fseek(FILE *stream, long offset, int whence);
功能:设置文件位置指针的偏移量
参数:
stream:文件指针
offset:偏移量 可正可负也可为0
whence:相对位置 SEEK_SET 文件起始位置 SEEK_CUR 文件当前位置 SEEK_END 文件末尾位置(最后一个字符后面一个位置)
返回值:
成功:0
失败:-1
七:-- rewind( )
#include <stdio.h>
void rewind(FILE *stream);
功能:将文件位置定位到起始位置
参数:
stream:文件指针
返回值:无
rewind(fp) <==> fseek(fp, 0, SEEK_SET);
案例:
#include <stdio.h>
int main(int argc, char const *argv[]){
//写操作的文件偏移量#if 0
FILE *fp;
if((fp = fopen("file.txt", "w")) == NULL)
{
perror("fail to fopen");
return -1;
}
fputs("abcdefghijklmnopq", fp);
//使用ftell获取文件偏移量
printf("offset = %ld\n", ftell(fp));
//使用fseek修改文件偏移量
//rewind(fp);
//fseek(fp, 0, SEEK_SET);
fseek(fp, -5, SEEK_END);
fputs("12345", fp);#endif
#if 0
FILE *fp;
if((fp = fopen("file.txt", "r")) == NULL)
{
perror("fail to fopen");
return -1;
}
char buf[32] = {0};
fgets(buf, 6, fp);
printf("buf = %s\n", buf);
printf("offset = %ld\n", ftell(fp));
fseek(fp, 3, SEEK_SET);
printf("offset = %ld\n", ftell(fp));
fgets(buf, 6, fp);
printf("buf = %s\n", buf);
#endif
FILE *fp;
//如果fopen打开或者创建一个文件时使用的是的a或者a+权限
//则写操作的偏移量无法改变,只能在文件的末尾位置
//但是读操作没有任何影响
if((fp = fopen("file.txt", "a+")) == NULL)
{
perror("fail to fopen");
return -1;
}
// char buf[32] = {0};
// fgets(buf, 6, fp);
// printf("buf = %s\n", buf);
// printf("offset = %ld\n", ftell(fp));
// fseek(fp, 3, SEEK_SET);
// printf("offset = %ld\n", ftell(fp));
// fgets(buf, 6, fp);
// printf("buf = %s\n", buf);
fputs("888888888", fp);
printf("offset = %ld\n", ftell(fp));
fseek(fp, 3, SEEK_SET);
printf("offset = %ld\n", ftell(fp));
fputs("666666", fp);
return 0;}