标签:space 读写文件 info 命令 结束 img fflush while const
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
using namespace std;
/*
文件结束标志: EOF --- feof()
读写文件:
fgetc: file get char 以字符形式获取
fputc: file put char 以字符形式写到文件
*/
int main()
{
//定义文件指针
FILE *fp;
//打开文件
fp = fopen("fopen_01.txt", "w+");
//判断文件打开是否成功
if (!fp) {
printf("打开文件失败\n");
exit(0);
}
//写的方式: 自己移动
fputc(‘a‘, fp);
fputc(‘b‘, fp);
fputc(‘c‘, fp);
//打开文件
fclose(fp);
//只读文件
FILE *read = fopen("fopen_01.txt", "r");
if (!read) {
printf("打开文件失败\n");
exit(0);
}
//读文件
//fgetc(): 放回int值, 以字符形式获取
int ch = fgetc(read);
while (ch != EOF)
{
putchar(ch); //打印字符到命令行
ch = fgetc(read);
}
printf("\n");
fclose(read);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <cstdlib>
#include <cstdio>
using namespace std;
/*
int fputs(char *str, FILE *fp);
char* fgets(char *str, int n, FILE *fp);
*/
int main()
{
FILE *fp = fopen("fgets_fputs_02.txt", "w+");
if (!fp) {
printf("打开文件失败");
exit(1);
}
//写的方式: 自己移动
int isok = fputs("Douzi is cute\n I love china!\n who are you?", fp);
//返回非负值,表示成功
printf("返回非负值表示成功: %d\n", isok);
fclose(fp);
FILE *read = fopen("fgets_fputs_02.txt", "r");
if (!read) {
printf("打开文件失败\n");
exit(1);
}
//读文件
char str[50] = "";
//设置读取的长度
while (fgets(str, 50, read))
{
puts(str);
}
//puts(str);
fclose(read);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
/*
//以二进制形式存储数据
int fwrite(void *buffer,unsigned size, unsigned count, FILE * fp)
//以什么方式写进去就以什么方式读出 !!
int fread(void *buffer,unsigned size, unsigned count, FILE * fp)
*/
struct student
{
char name[20];
int age;
double num;
}mystudent;
int main()
{
//写文件
FILE *fp = fopen("fread_fwrite_03.bat", "wb");
if (fp == NULL) {
printf("打开失败\n");
exit(1);
}
while (true)
{
printf("输入姓名-年龄-成绩:\n");
scanf("%s%d%lf", &mystudent.name, &mystudent.age, &mystudent.num);
//参数: 源起始位置, 写多少(字节), 写的次数, 目的地
// &mystudent, sizeof(struct student), 1, fp
fwrite(&mystudent, sizeof(struct student), 1, fp);
printf("是否继续?(Y/N)");
//清除缓冲区
//否则下面的字符无法输入,因为上面输入按下回车,"回车"会被下面的getchar()吃掉,而无法自己输入
fflush(stdin);
int ch = getchar();
if (ch == ‘N‘ || ch == ‘n‘) {
break;
}
}
fclose(fp);
//读取文件
FILE *read = fopen("fread_fwrite_03.bat", "rb");
if (!read) {
printf("文件打开失败\n");
exit(1);
}
//以二进制形式读取
while (fread(&mystudent, sizeof(struct student), 1, read) > 0) {
printf("姓名\t年龄\t成绩:\n");
printf("%s\t%d\t%.2f\n", mystudent.name, mystudent.age, mystudent.num);
}
fclose(read);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
/*
//以二进制形式存储数据
int fwrite(void *buffer,unsigned size, unsigned count, FILE * fp)
//以什么方式写进去就以什么方式读出 !!
int fread(void *buffer,unsigned size, unsigned count, FILE * fp):
count: 知道有多少数据可以读取
*/
const int maxn = 1024 + 10;
struct student
{
char name[20];
int age;
double num;
}stus[maxn];
int main()
{
////写文件
FILE *fp = fopen("fread_fwrite_03.bat", "wb");
if (fp == NULL) {
printf("打开失败\n");
exit(1);
}
int cnt;
scanf("%d", &cnt);
for (int i = 0; i < cnt; i++)
{
printf("输入姓名-年龄-成绩:\n");
scanf("%s%d%lf", &stus[i].name, &stus[i].age, &stus[i].num);
//清除缓冲区
//否则下面的字符无法输入,因为上面输入按下回车,"回车"会被下面的getchar()吃掉,而无法自己输入
fflush(stdin);
}
//参数: 源起始位置, 写多少(字节), 写的次数, 目的地
// stus, sizeof(struct student), cnt, fp
fwrite(stus, sizeof(struct student), cnt, fp);
fclose(fp);
//读取文件
FILE *read = fopen("fread_fwrite_03.bat", "rb");
if (!read) {
printf("文件打开失败\n");
exit(1);
}
student read_demo[maxn];
fread(read_demo, sizeof(struct student), cnt, read);
for (int i = 0; i < cnt; i++){
printf("姓名\t年龄\t成绩:\n");
printf("%s\t%d\t%.2f\n", read_demo[i].name, read_demo[i].age, read_demo[i].num);
}
return 0;
}
标签:space 读写文件 info 命令 结束 img fflush while const
原文地址:https://www.cnblogs.com/douzujun/p/10321833.html