1.单字符读写
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main01()
{
//写文件
FILE* fp_write = NULL;
//写方式打开文件
fp_write = fopen("1.txt", "w+");
if (fp_write == NULL)
{
perror("fopen:");
}
char buf[] = "this is a test for pfutc!";
for (int i = 0; i < (int)strlen(buf); i++)
{
fputc(buf[i], fp_write);
}
fclose(fp_write);
fp_write = NULL;
//读文件
FILE* fp_read;
fp_read = fopen("1.txt", "r");
if (NULL == fp_read)
{
return -1;
}
//判断文件结尾 注意:多输出一个空格
while (!feof(fp_read))
{
printf("%c", fgetc(fp_read));
}
//char ch;
//while ((ch=fgetc(fp_read))!=EOF)
//{
// printf("%c", ch);
//}
system("pause");
return EXIT_SUCCESS;
}
2.行读写
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main02()
{
//写文件
FILE* fp_write = NULL;
fp_write = fopen("1.txt", "w+");
if (NULL == fp_write)
{
perror("fopen:");
return -1;
}
char* buf[] = {
"01 this is a test for pfutc!\n",
"02 this is a test for pfutc!\n",
"03 this is a test for pfutc!\n",
"04 this is a test for pfutc!\n",
};
for (int i = 0; i < 4; i++)
{
fputs(buf[i], fp_write);
}
fclose(fp_write);
fp_write = NULL;
//读文件
FILE* fp_read = NULL;
fp_read = fopen("1.txt", "r");
if (NULL == fp_read)
{
perror("fopen:");
return -1;
}
while (!feof(fp_read))
{
char temp[1024] = { 0 };
fgets(temp, 1024, fp_read);
printf("%s", temp);
}
fclose(fp_read);
fp_read = NULL;
system("pause");
return EXIT_SUCCESS;
}
3.块读写
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct _TEACHER{
char name[64];
int age;
}Teacher;
int main03()
{
FILE* fp_write= NULL;
fp_write = fopen("1.txt", "wb");
if (NULL == fp_write)
{
perror("fopen:");
return -1;
}
Teacher teachers[4] = {
{ "Obama", 33 },
{ "John", 28 },
{ "Edward", 45 },
{ "Smith", 35 }
};
for (int i = 0; i < 4; i++)
{
fwrite(&teachers[i], sizeof(Teacher), 1,fp_write);
}
fclose(fp_write);
fp_write = NULL;
//读文件
FILE* fp_read = NULL;
fp_read = fopen("1.txt", "rb");
if (fp_read == NULL)
{
perror("fopen:");
return -1;
}
Teacher temps[4];
fread(&temps, sizeof(Teacher), 4, fp_read);
for (int i = 0; i < 4; i++){
printf("Name:%s Age:%d\n", temps[i].name, temps[i].age);
}
fclose(fp_read);
system("pause");
return EXIT_SUCCESS;
}
4.格式化读写
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main04()
{
//写文件
FILE* fp_write = NULL;
//写方式打开文件
fp_write = fopen("1.txt", "w");
if (fp_write == NULL)
{
perror("fopen:");
return -1;
}
fprintf(fp_write, "hello world:%d!", 10);
//关闭文件
fclose(fp_write);
//读文件
FILE* fp_read = NULL;
fp_read = fopen("1.txt", "rb");
if (fp_read == NULL)
{
perror("fopen:");
return -1;
}
char temps[1024] = { 0 };
while (!feof(fp_read))
{
fscanf(fp_read, "%s", temps);
printf("%s", temps);
}
fclose(fp_read);
system("pause");
return EXIT_SUCCESS;
}
习题
//1. 某个数据文件包含了家庭成员的年龄。一个家庭各个成员的年龄位于同一行,
//由空格分割。例如:下面的数据
//----------------------
//45 42 22
//36 35 7 3 1
//22 30
//----------------------
//描述了三个家庭的所有成员的年龄,它们分别有3个、5个和2个成员。
//编写一个程序,计算这种用文件表示的每个家庭所有成员的平均年龄。
//程序应该用格式代码%5.2f,打印出平均年龄,家庭每个成员的年龄。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
//FILE* fp_write = NULL;
//fp_write = fopen("作业01.txt", "w+");
//if (NULL == fp_write)
//{
// perror("open:");
// return -1;
//}
//char* buf[] = {
// "----------------------\n",
// "45 42 22\n"
// "36 35 7 3 1\n",
// "22 30\n",
// "----------------------\n",
//};
//for (int i = 0; i < 5; i++)
//{
// fputs(buf[i], fp_write);
//}
//fclose(fp_write);
//fp_write = NULL;
FILE* fp_write = NULL;
fp_write = fopen("1.txt", "w+");
if (NULL == fp_write)
{
perror("fopen:");
return -1;
}
char* buf[] = {
"----------------------\n",
"45 42 22\n"
"36 35 7 3 1\n",
"22 30\n",
"----------------------\n",
};
for (int i = 0; i < 5; i++)
{
fputs(buf[i], fp_write);
}
fclose(fp_write);
fp_write = NULL;
getchar();
return EXIT_SUCCESS;
}