File operations related functions
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include<stdlib.h>
Basic Operations
int main()
{
int a = 10000;
FILE* pf = fopen("test.txt", "wb");
fwrite(&a, 4, 1, pf);//二进制的形式写到文件中
// 4个字节 1个数 写入
fclose(pf);
pf = NULL;
return 0;
}
fputc write file
//FILE
//FILE *fopen( const char *filename, const char *mode );
//fputc 写文件
int main()
{
FILE* pf = fopen("test.txt", "w");
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//写文件
fputc('b', pf);
fputc('i', pf);
fputc('t', pf);
fclose(pf);
pf = NULL;
return 0;
}
fgetc read file
int main()
{
FILE* pf = fopen("test.txt", "r");//虽然r只有一个字母(非字符串),但是还是需要双引号
//int ch = 0 ;
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//读文件
ch = fgetc(pf);//fgetc的返回值是int 但是输出还是用%c
//putchar(ch);
printf("%c\n", ch);
fclose(pf);
pf = NULL;
return 0;
}
fputs ( "hello", pf) ;
write a bunch of string
int main()
{
FILE* pf = fopen("test.txt", "w");
int ch = 0 ;
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
fputs("hello world", pf);
fclose(pf);
pf = NULL;
return 0;
}
fgets (arr, 20, pf) ;
read data or print int return value or% c
int main()
{
FILE* pf = fopen("test.txt", "r");
char arr[20] = {0};
int ch = 0 ;
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
fgets(arr, 20, pf);
printf("%s", arr);
fgets(arr, 20, pf);
printf("%s", arr);
fgets(arr, 20, pf);
printf("%s", arr);
fclose(pf);
pf = NULL;
return 0;
}
//scanf
//printf
//格式化的输入输出函数---标准输入/输出
//fscanf-->四个函数中,只有这个是读
//fprintf
//格式化的形式写入文件或者标准输出-fprintf
//格式化的形式从文件或者标准输入读取-fscanf
//sscanf
//sprintf
// the data structure of a variable format is converted in the form of a string stored
sprintf (buf, "% S% D", s.name , s.age);
// will s.name , write s.age into buf
// string into the structure [buf is the array name]
fprintf / sprintf (buf, "% S% D", s.name , s.age); -> (all the later parameters (structural body) write address before the parameter (file pointer)), the location parameters do not need to take back the address [key]
sscanf (buf, "% S% D", tmp.name , & (tmp.age));
// corresponding buf writing tmp (structure) need to take the address, and the name of the array is tmp.name
struct S
{
char name[20];
int age;
};
//
//序列化和反序列化
//xml
//json-cjson
//
int main()
{
struct S s = { "张三", 20 };
char buf[20] = { 0 };
struct S tmp = { 0 };
sprintf(buf, "%s %d", s.name, s.age);
sscanf(buf, "%s %d", tmp.name, &(tmp.age));
//sscanf/fscanf --- 都是将前面的buf指针,写入或者读入后面的结构体
printf("%s %d\n", tmp.name, tmp.age);
system("pause");
return 0;
}
// read [Memory] important to understand
the fscanf (PF, "% s% D", s.name , & (s.age));
// read ps (file pointer) to the contents of the structure s
struct S
{
char name[20];
int age;
};
//stdin -标准输入-键盘 FILE*
//stdout-标准输出-屏幕 FILE*
//stderr-标准错误-屏幕 FILE*
int main()
{
struct S s = {0};
FILE* pf = fopen("test.txt","r");
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
fscanf(pf, "%s %d", s.name, &(s.age));
//fscanf(stdin, "%s %d", s.name, &(s.age));//ok
//打印
printf("%s %d\n", s.name, s.age);
fclose(pf);
pf = NULL;
system("pause");
return 0;
}
// Write
fprintf (PF, "% s% D", s.name , s.age);
// writes s structure member inside the file (file pointer pf)
int main()
{
struct S s = {"zhangsan", 20};
FILE* pf = fopen("test.txt","w");
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//写
//fprintf(stdout, "%s %d", s.name, s.age);
fprintf(pf, "%s %d", s.name, s.age);//将结构体s里面的成员写入文件(文件指针pf)
fclose(pf);
pf = NULL;
return 0;
}
//sscanf/fscanf(pf, "%s %d", s.name, &(s.age)); --- 都是将前面的buf指针,写入或者读入后面的结构体(注意要取地址)
//sprintf/fprintf(pf, "%s %d", s.name, s.age); --- 都是讲后面的结构体成员,写入前面的文件指针
// write
fwrite (& s, the sizeof (struct S),. 1, pf);
// s content structure file is written to the point pf, s address to take. 1 represents the structure
struct S
{
char name[20];
int age;
};
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
int main()
{
struct S s = {"张三", 20};
FILE* pf = fopen("test.txt", "wb");
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//写操作
fwrite(&s, sizeof(struct S), 1, pf);//将s结构体的内容写到pf指向的文件中去,s要取地址。1代表1个结构体
fclose(pf);
pf = NULL;
return 0;
}
// read
fread (& s, the sizeof (struct S),. 1, pf);
// point pf the file to read the contents of s to the structure, s address to take
int main()
{
struct S s = {0};
FILE* pf = fopen("test.txt", "rb");
if(pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//读操作
fread(&s, sizeof(struct S), 1, pf);//将pf指向的文件s读到结构体的内容中去,s要取地址
printf("%s %d\n", s.name, s.age);
fclose(pf);
pf = NULL;
system("pause");
return 0;
}
//fwrite(&s, sizeof(struct S), 1, pf);//将s结构体的内容写到pf指向的文件中去,s要取地址。1代表1个结构体
//fread(&s, sizeof(struct S), 1, pf);//将pf指向的文件s读到结构体的内容中去,s要取地址
fseek: The file offset pointer to locate and position the file pointer
ftell: file pointer offset with respect to the starting position
rewind: Let file pointer position back to the starting position of the file
int main()
{
FILE* pf = fopen("example.txt", "r");
int ch = 0;
if(pf == NULL)
return 0;
//目前文件中总字节数为17
fseek(pf, -1, SEEK_END);//文件指针定位到 末尾 的 前面1个的位置(偏移量ftell为16)
printf("%d\n", ftell(pf));//偏移量为16
ch = fgetc(pf);//文件指针往后走了一个 (偏移量为17)
putchar(ch);//打印一个字符
printf("%d\n", ftell(pf));//偏移量为17
fseek(pf, 3, SEEK_CUR);//文件指针从当前位置 向后 走3个位置,偏移量 17 + 3 = 20
printf("%d\n", ftell(pf));//偏移量为20
ch = fgetc(pf);//文件指针往后走了一个 (偏移量为21)其实是20/////////////文件指针最大偏移量是20,不能再多了
putchar(ch);//打印一个字符
printf("%d\n", ftell(pf));//偏移量为21 其实是20
fseek(pf, -9,SEEK_CUR);//文件指针从当前位置 向前 走3个位置,偏移量 20 - 9 = 11
printf("%d\n", ftell(pf));//偏移量为11
rewind(pf);//文件指针回到最开始,偏移量ftell(pf) = 0
ch = fgetc(pf);//文件指针往前走了一个位置
putchar(ch);
printf("%d\n", ftell(pf));//偏移量为1
fclose(pf);
pf = NULL;
system("pause");
return 0;
}
He will cover the original data written fputs
int main()
{
FILE * pFile;
pFile = fopen("example.txt", "wb");
fputs("This is an apple.", pFile);
fseek(pFile, 9, SEEK_SET);
fputs(" sam", pFile);//This is a sample.
fclose(pFile);
return 0;
}