ファイル操作関連の機能
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include<stdlib.h>
基本操作
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ライト・ファイル
//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関数は、ファイルを読みます
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(「こんにちは」、PF)は 、
文字列の束を書きます
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) 、
データ又は印刷INT戻り値または%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
//変数形式のデータ構造が格納された文字列の形に変換される
のsprintf(BUF、 "%のSの%のD"、s.name、s.age);
//はだろうs.name、s.ageを書きますBUFに
構造[bufが配列名である]に//ストリング
fprintfの/のsprintf(BUF、 "%のSの%のD"、s.name >(すべての後のパラメータ(構造体) - ;、s.age)パラメータの前にアドレスを書き込み(ファイルポインタ))、位置パラメータは、アドレス[キー]取り戻す必要はありません
のsscanf(bufは、 "%のS%のD"、tmp.name、&(tmp.age));
//対応するBUFをTMP(構造)を書き込むと、アドレスを取得する必要があり、配列の名前は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;
}
//読み取り理解する[メモリ]重要
関数fscanf(PF、 "%sの%がD"、s.name、&(s.ageを));
//読み取りPS(ファイルポインタ)構造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;
}
//書き込み
関数fprintf(PF、 "%sの%のD"、s.name、s.age);
//ファイル(ファイルポインタ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); --- 都是讲后面的结构体成员,写入前面的文件指针
//書き込み
fwriteの(&S、はsizeof(構造体S),. 1、PF);
//のコンテンツ構造ファイルを取る点PF、Sアドレスに書き込まれます。図1は、構造を表します
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;
}
//リード
関数fread(&S、はsizeof(構造体S),. 1、PF);
取る//点は、構造に対するSの内容を読み取るためにファイルをPF、Sアドレス
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関数:ファイルが見つけ、ファイルポインタを配置するポインタをオフセット
のftell:ファイルポインタが開始位置に対してオフセット
巻き戻し:ファイルの開始位置に戻ってファイルポインタの位置をしてみましょう
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;
}
彼は、元のデータ書か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;
}