云计算百科
云计算领域专业知识百科平台

C语言-文件

1,文件的读写

2,读写一行数据

3,格式化输入输出

4,二进制的输入输出

5,格式化数据与字符串的相互转化

6,文件的随机读写

7,文本和二进制

———————————————————————————————————————————

1,文件的读写

#include<stdio.h>
#include<string.h>
#include<errno.h>

//int main() {
//FILE* pf = fopen("test.txt", "r");
//if (pf==NULL)
//{
//printf("%s\\n", strerror(errno));
//return 1;
//}
////读文件
//
////关闭文件
//fclose(pf);
//pf = NULL;
//return 0;
//}

//文件的读写 ===================================================================
// 写字符
//int main() {
//FILE* pf = fopen("test.txt", "w");
//if (pf==NULL)
//{
//printf("%s\\n", strerror(errno));
//return 1;
//}
////写文件
//char i = 0;
//for ( i = 'a'; i <= 'z'; i++)
//{
//fputc(i, pf);
//}
////关闭文件
//fclose(pf);
//pf = NULL;
//return 0;
//}

//读字符
int main() {
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\\n", strerror(errno));
return 1;
}
//读文件
//int ch = fgetc(pf);
//printf("%c\\n", ch);
//ch = fgetc(pf);
//printf("%c\\n", ch);
//ch = fgetc(pf);
//printf("%c\\n", ch);
//ch = fgetc(pf);
//printf("%c\\n", ch);
int ch = 0;
while ((ch=fgetc(pf))!=EOF) {
printf("%c ", ch);
}

//关闭文件
fclose(pf);
pf = NULL;
return 0;
}

2,读写一行数据

#include<stdio.h>
#include<string.h>
#include<errno.h>
//写一行数据
//int main() {
//FILE* pf = fopen("test.txt", "w");
//if (pf == NULL)
//{
//printf("%s\\n", strerror(errno));
//return 1;
//}
////写一行数据
//fputs("hello bit\\n", pf);
//fputs("hello bit\\n", pf);
//
////关闭文件
//fclose(pf);
//pf = NULL;
//return 0;
//}

//读一行数据
int main() {
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
printf("%s\\n", strerror(errno));
//perror("fopen");//等价于上面语句
return 1;
}
//读一行数据
char arr[20];
fgets(arr, 5, pf);//最多读10个字符,且包含\\0
printf("%s\\n", arr);

//关闭文件
fclose(pf);
pf = NULL;
return 0;
}

3,格式化输入输出

#include<stdio.h>
#include<string.h>
#include<errno.h>
//格式化输入输出——————————–
//格式化写
//struct S
//{
//char arr[10];
//int age;
//float score;
//};
//int main() {
//struct S s = { "zhangsan",25,50.5f };
//FILE* pf = fopen("test.txt", "w");
//if (pf == NULL)
//{
////printf("%s\\n", strerror(errno));
//perror("fopen");//等价于上面语句
//return 1;
//}
//
//fprintf(pf, "%s %d %f", s.arr, s.age, s.score);
//
////关闭文件
//fclose(pf);
//pf = NULL;
//return 0;
//}

//格式读
struct S
{
char arr[10];
int age;
float score;
};
int main() {
struct S s = {0};
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
//printf("%s\\n", strerror(errno));
perror("fopen");//等价于上面语句
return 1;
}

fscanf(pf, "%s %d %f", s.arr, &(s.age), &(s.score));
//printf("%s %d %f", s.arr, s.age, s.score);
fprintf(stdout, "%s %d %f", s.arr, s.age, s.score);//等价于上面的一行代码
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}

//补充知识–流
//FILE*
//printf
//scanf
//任何一个c程序,只要运行起来就会默认打开三个流
//FILE* stdin 标准输入流(键盘)
//FILE* stdout 标准输出流(屏幕)
//FILE* stderr 标准错误流(屏幕)

4,二进制的输入输出

#include<stdio.h>
#include<string.h>
#include<errno.h>
//二进制的输入输出
//二进制的方式写
//struct S
//{
//char arr[10];
//int age;
//float score;
//};
//int main() {
//struct S s = {"zhangsan",25,50.5f};
////以二进制的形式写到文件中
//FILE* pf = fopen("test.txt", "wb");
//if (pf == NULL)
//{
////printf("%s\\n", strerror(errno));
//perror("fopen");//等价于上面语句
//return 1;
//}
////二进制的方式写
//fwrite(&s, sizeof(struct S), 1, pf);
////关闭文件
//fclose(pf);
//pf = NULL;
//return 0;
//}

//二进制的方式读
struct S
{
char arr[10];
int age;
float score;
};
int main() {
struct S s = {0};
//以二进制的形式写到文件中
FILE* pf = fopen("test.txt", "rb");
if (pf == NULL)
{
//printf("%s\\n", strerror(errno));
perror("fopen");//等价于上面语句
return 1;
}
//二进制的方式写
fread(&s, sizeof(struct S), 1, pf);//1个结构体大小的数据
printf("%s %d % f\\n", s.arr, s.age, s.score);
//关闭文件
fclose(pf);
pf = NULL;
return 0;
}

5,格式化数据与字符串的相互转化

#include<stdio.h>
#include<string.h>
#include<errno.h>
//格式化数据与字符串的相互转化
struct S
{
char arr[10];
int age;
float score;
};
int main() {
struct S s = { "zhangsan",20,55.5f };
struct S tmp = { 0 };
char buf[100] = { 0 };
//把s中的格式化数据转化成字符串放到buf中
sprintf(buf, "%s %d %f", s.arr, s.age, s.score);
printf("字符串:%s\\n", buf);

//从字符串buf中获取一个格式化的数据到tmp中
sscanf(buf, "%s %d %f", tmp.arr, &(tmp.age), &tmp.score);
printf("格式化:%s %d %f\\n", tmp.arr, tmp.age, tmp.score);
return 0;
}

6,文件的随机读写

#include<stdio.h>
#include<string.h>
#include<errno.h>
//文件的随机读写
struct S
{
char arr[10];
int age;
float score;
};
int main() {
FILE* pf = fopen("test.txt", "r");
if (pf == NULL)
{
//printf("%s\\n", strerror(errno));
perror("fopen");//等价于上面语句
return 1;
}
//读文件
//定位文件指针
fseek(pf, 2, SEEK_SET);
int ch = fgetc(pf);//c
printf("%c\\n", ch);
//求相对于起始位置的偏移量
printf("%d\\n", ftell(pf));//3

//fseek(pf, 2, SEEK_CUR);
fseek(pf, -1, SEEK_END);
ch = fgetc(pf);//f
printf("%c\\n", ch);
printf("%d\\n", ftell(pf));//6

rewind(pf);//文件指针回到起始位置
ch = fgetc(pf);//a
printf("%c\\n", ch);

//关闭文件
fclose(pf);
pf = NULL;
return 0;
}

7,文本和二进制

#include<stdio.h>
#include<string.h>
#include<errno.h>
//文本和二进制
int main() {
int a = 10000;
FILE* pf = fopen("text.txt", "wb");
fwrite(&a, 4, 1, pf);//二进制的形式写到文件中
fclose(pf);
pf = NULL;
return 0;
}

赞(0)
未经允许不得转载:网硕互联帮助中心 » C语言-文件
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!