鱼C论坛

 找回密码
 立即注册

Windows中的ini文件读写

热度 23已有 2464 次阅读2013-10-11 11:33 |个人分类:复帖代码

今天看到一个提问:
===========================================
求编写一个记录自己进步与退步的小程序。

要求功能如下:
.程序开启后提问,您今天全神贯注读书时间为几小时?输入数字按回车,系统记录输入的小时数,给相应小时数的分,如输入“8”(表示今日读书8小时),回车后系统+8分。将其加分记录下来,并写入到某个文件夹下的记事本中。记事本中的分数每日叠加,每次打开本软件时,第一条显示叠加后的总分数。并进行下一次的计算,叠加后在写入某些文件夹下的记事本当中。
===========================================

想起了Windows中ini文件的读写,特此做了此小程序,参考交流。
============================================
效果:
============================================
代码:
// VS 2008 
#include "stdafx.h"

#include <stdlib.h> 
#include "windows.h"
#include <stdio.h>
#include <tchar.h>


int _tmain(int argc, _TCHAR* argv[])
{
const char* szcFile = "D:\\ReadRecord.ini";
int nLastDate = 0;
int nLastScore = 0;
int nSumScore = 0;
int nOperCnt = 0;
// 获取当前系统日期
SYSTEMTIME sysTime = { 0 };
GetSystemTime( &sysTime );
int nToday = sysTime.wYear*10000 + sysTime.wMonth*100 + sysTime.wDay;

// 获取上次阅读的日期,分数,次数
nLastDate  = GetPrivateProfileInt( "History", "LastDate",  nToday/*20131011*/, szcFile );
nLastScore = GetPrivateProfileInt( "History", "LastScore", 0,        szcFile );
nSumScore  = GetPrivateProfileInt( "History", "SumScore",  0,        szcFile );
nOperCnt   = GetPrivateProfileInt( "History", "OperCnt",   0,        szcFile );
// 两个日期比较
int nDateDiff = sysTime.wYear*10000 + sysTime.wMonth*100 + sysTime.wDay - nLastDate ;
if ( nDateDiff > 1 )
{
// 说明中间至少一天没有阅读
for( int i=1; i<nDateDiff; i++ ) // 1开始==不含今天的
{
nLastScore -= 3;
}
printf( "注意啦,小伙伴们, 学习如逆水行舟,不进则退啊。\n  你们已经 %d 天没有好好学习了,目前得分为 %d。\n",nDateDiff, nLastScore );
}
else
{
printf( "\t============学习情况统计============\t\n" );
printf( "\t\t累计得分:%d\t\n" ,nLastScore );
printf( "\t\t累计学时:%d\t\n" ,nSumScore );
printf( "\t\t阅读次数:%d\t\n" ,nOperCnt );
printf( "\t\t平均学时:%g\t\n" ,(nOperCnt==0?0:nSumScore*1.0/nOperCnt) );
printf( "\t\t上次阅读:%d\t\n" ,nLastDate );
printf( "\n\t\t贵有恒,何必三更眠五更起;\n\t\t最无益,只怕一日曝十日寒!\t\n" );
printf( "\t====================================\t\n" );
}
printf( "您今天[%4d-%02d-%02d]的阅读时间为:",
sysTime.wYear, sysTime.wMonth,sysTime.wDay );
int nTodayScore = -1;
if ( 1 != scanf( "%d", &nTodayScore ) )
{
scanf( "%*c" );
printf( "输入出错啦...\n" );
return 0;
}
if ( nTodayScore < 0)
{
scanf( "%*c" );
printf( "输入出错啦...\n" );
return 0;
}
else if ( nTodayScore == 0 )
{
nLastScore -= 3;
}
else 
{
if ( nTodayScore > 24 )
{
printf( " 要么是输错,要么吹大了,或者抱佛脚,不然就是...\n" );
return 0;
}
if ( nTodayScore > 12 )
{
printf( " 努力学习固然重要,但别忘了健康...\n" );
}
nLastScore += nTodayScore;
nSumScore += nTodayScore;

printf( "今天经验值 +%d...\n",nTodayScore );
}
// 写入文件保存
nOperCnt++;
char szcText[20] = { 0 }, szcText2[20] = { 0 };
sprintf( szcText, "%d", nLastScore );
WritePrivateProfileString( "History","LastScore", szcText, szcFile );

sprintf( szcText, "%d", nSumScore );
WritePrivateProfileString( "History","SumScore", szcText, szcFile );

sprintf( szcText, "%d", nToday );
WritePrivateProfileString( "History","LastDate", szcText, szcFile );

sprintf( szcText, "%d", nOperCnt );
WritePrivateProfileString( "History","OperCnt", szcText, szcFile );

sprintf( szcText2, "%d-%d-%d %02d:%02d", sysTime.wYear, sysTime.wMonth,sysTime.wDay, sysTime.wHour, sysTime.wMinute );
WritePrivateProfileString( "Date",szcText, szcText2, szcFile );

return 0;
}


3

路过

鸡蛋

鲜花
18

握手
1

雷人

刚表态过的朋友 (22 人)

发表评论 评论 (3 个评论)

回复 静待 2013-10-11 21:13
实现这样一个效果,居然要写这么长的代码!佩服,我是初学者,得向你学习!
回复 苹果沃珂 2013-10-12 09:02
静待: 实现这样一个效果,居然要写这么长的代码!佩服,我是初学者,得向你学习!
理顺思路了,写下来就这样了……
在根据调试几下,根据Debug,修修补补,就出来。。。
Come On!
回复 折心灬小轩 2014-7-10 14:42
去at小仙吧

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 05:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部