案例需求:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#define FILENAME "score.txt"
//控制台输出位置
//gotoxy只对下面一行的输出语句有用
void gotoxy(int x, int y)
{
//设置输出位置
COORD pos = { x,y }; //x行 y列
//获取控制台输出句柄
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
//设置控制台光标位置
SetConsoleCursorPosition(output, pos);
}
//隐藏光标
VOID HideConsoleCursor(VOID)
{
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),
&cursor_info);
}
//关闭窗口无效化
VOID KillConsoleCloseButton(VOID)
{
DeleteMenu(GetSystemMenu(GetConsoleWindow(), FALSE),
SC_CLOSE, MF_DISABLED);
DrawMenuBar(GetConsoleWindow());
}
//改变窗口的标题
VOID SetTitle(LPCWSTR lpTitle)
{
SetConsoleTitle(lpTitle);
}
//开始界面
void Welcome()
{
gotoxy(45, 6);
system("color 7C");
printf("欢迎使用系统\n");
gotoxy(45, 8);
printf("1.记录比赛得分\n");
gotoxy(45, 10);
printf("2.查看往届记录\n");
gotoxy(45, 12);
printf("3.清空比赛记录\n");
gotoxy(45, 14);
printf("4.退出当前程序\n");
HideConsoleCursor();
KillConsoleCloseButton();
SetTitle(L"记录比赛分数");
}
//全局变量 记录标志 文件是否为空
int fileEmpty = 0;//1为真 0为空
//初始化文件为空的标志
void initFlag()
{
FILE* fp = fopen(FILENAME, "r");
if (fp == NULL)
{
perror("错误信息:");
system("pause");
system("cls");
return;
}
//从文件读取一个字符,如果返回值为EOF,则文件为空
char ch = fgetc(fp);
if (ch == -1)
{
fileEmpty = 1;
}
else
{
fileEmpty = 0;
}
}
//记录比赛得分
void getScore();
//查看往届记录
void checkScore();
//清空比赛记录
void clearScore();
int main()
{
initFlag();
while (1)
{
Welcome();
//选择
gotoxy(45, 16);
printf("请输入选项:\n");
gotoxy(56, 16);
int select = 0;
scanf("%d", &select);
switch (select)
{
case 1:
getScore();
break;
case 2:
checkScore();
break;
case 3:
clearScore();
break;
case 4:
system("cls");
printf("欢迎使用!");
exit(0);
break;
default:
system("cls");
break;
}
}
system("pause");
return 0;
}
//记录比赛得分
void getScore()
{
system("cls");
printf("请输入比赛得分:\n");
double score = 0;
scanf("%lf", &score);
//放入文件中--用追加的方式写
FILE* fp = fopen(FILENAME, "a");
if (!fp)
{
perror("出错信息");
system("pause");
system("cls");
return;
}
//按格式化的方式写入文件中
fprintf(fp, "%lf\n", score);
fclose(fp);
printf("分数写入成功\n");
//写入成功后,文件不为空
fileEmpty = 0;
system("pause");
system("cls");
}
//查看往届记录
void checkScore()
{
if (fileEmpty)
{
system("cls");
printf("文件为空\n");
system("pause");
//清屏
system("cls");
return;
}
system("cls");
FILE* fp = fopen(FILENAME, "r");
if (!fp)
{
perror("出错信息");
system("pause");
system("cls");
return;
}
//格式化方式读取
int index = 0;
double Score = 0;
while (!feof(fp))
{
//如果格式化读取时,不把\n读取,光标移至文件结尾有效数据的下一行开头
//造成无法读取到有效数据,只能追溯上一次的有效数据,再次录入,造成最后一次数据重复打印
//解决方案1:格式化读取时,加上\n
//fscanf(fp, "%lf\n", &Score);
//解决方案2:利用返回值判断是否读取到有效数据,没有读取到就结束读取
int ret=fscanf(fp, "%lf", &Score);
if (ret == EOF)
{
break;
}
printf("第%d届的分数为%.2lf\n", ++index, Score);
}
system("pause");
system("cls");
}
//清空比赛记录
void clearScore()
{
if (fileEmpty)
{
system("cls");
printf("文件为空\n");
system("pause");
//清屏
system("cls");
return;
}
system("cls");
//方法:以写的方式打开文件,如果文件存在则清空文件,文件不存在则创建文件
printf("确认是否清空?\n");
printf("1.确认清空\n2.返回\n");
int select = 0;
scanf("%d", &select);
if (select == 1)
{
//清空
FILE* fp = fopen(FILENAME, "w");
if (fp == NULL)
{
perror("出错信息");
system("pause");
system("cls");
return;
}
printf("清空文件成功!\n");
//清空完后,文件为空
fileEmpty = 1;
}
//输入其他值,则返回主界面
system("pause");
system("cls");
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有