#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//static char Msg[256][256];
class FILESTORAGE
{
private:
struct TETRIS
{
int CurrentScore;
int Max_HistoricalScore;
}Tetris;
public:
FILESTORAGE()
{Tetris.CurrentScore = 0;
Tetris.Max_HistoricalScore = 0;
}
FILE* CreateFile_M(); //生成并打开一个文本,返回文件指针
FILE* ReadFile_M(); //读取文件,若该文件存在,以读的形式返回该文件指针;若该文件不存在,返回空指针
void WriteFile_M(FILE* File, char str[][256], int row); //将内容写入文件中
int GetSmark( const char str[256]); //使用方法:历史最高分:67; str[256]输入历史最高分:,返回int型分数
bool SetSmark(const char str[256], const unsigned int smark); //使用方法:历史最高分:67; str[256]输入历史最高分:,设置int型分数
char Msg[256][256];
};
FILE* FILESTORAGE::CreateFile_M()
{
FILE *fp_write = fopen("D:\\huanlewu.txt", "wb");
return fp_write;
}
FILE* FILESTORAGE::ReadFile_M()
{
FILE *fp_read = fopen("D:\\huanlewu.txt", "rb+");
if (fp_read == nullptr)
return nullptr;
else
return fp_read;
}
void FILESTORAGE::WriteFile_M(FILE* File, char str[][256], int row)
{
for (int i = 0; i < row; ++i)
{
fputs(str[i], File);
//fputc('\n', File);
}
}
int FILESTORAGE::GetSmark( const char str[256])
{
char *ptr = nullptr;
unsigned int num = 0;
for (int i = 0; i < 256; ++i)
{
ptr = strstr(Msg[i], str);
if (ptr != nullptr)
{
//MessageBox(NULL, ptr + strlen(str), "GETmark:", NULL);
num = atoi(ptr + strlen(str));
break;
}
}
return num;
}
bool FILESTORAGE::SetSmark(const char str[256], const unsigned int smark)
{
char *ptr = nullptr;
static char temptbuff[256] = { 0 };
char buff[256] = { 0 };
for (int i = 0; i < 256; ++i)
{
ptr = strstr(Msg[i], str);
if (ptr != nullptr)
{
strncpy(temptbuff, Msg[i], strlen(str));
memset(Msg[i], 0, sizeof(Msg[i]));
sprintf_s(Msg[i], "%s%d", temptbuff, smark);
return true;
}
}
return false;
}
文件操作(源码)
最新推荐文章于 2024-05-19 12:01:11 发布