c语言连续存储文件中断
来源:互联网 发布:蒙文翻译在线翻译软件 编辑:程序博客网 时间:2024/05/18 00:03
在计算数值模拟的时候,经常需要将上一步计算得出值来作为下一步的输入,进行迭代计算,如果数值过多,需要分析每一步的中间值,将每一步的值保存下来,存储到硬盘上。于是问题就产生了。
环境:1.编程语言:C
2.编译环境:VS2010
3.系统环境:XP,Win7
程序主体如下:
#include "stdafx.h"#include <stdio.h>#include <Windows.h>int _tmain(int argc, _TCHAR* argv[]){double calValue = 0;//the initial value int calTimes = 10;//the calculate times int iCount = 0;while (calTimes--){//this place, just for show, in the actual, the calculate express may be complex and a lot of values should be saved.calValue = calValue + 1 ;char savePath[128] = {0};sprintf(savePath,"D:\\test\\%0.4d.txt", ++iCount);FILE *pFlie = fopen(savePath, "w");if (pFlie){fprintf(pFlie,"%f\n", calValue);fclose(pFlie); pFile = NULL; }}return 0;}
问题1:不小心点了关闭程序按钮,怎样让程序启动时接着原来的步骤进行。
解决方案:记录存储的文件的序号,从上次文件中读取数据开始余下的计算。代码如下:
int _tmain(int argc, _TCHAR* argv[]){double calValue = 0;//the initial valueint calTimes = 10;//the calculate timesint iCount = 0;//add another file to record the num of current processFILE *pRecord = NULL;pRecord = fopen("D:\\test\\record.txt","r");if (pRecord){fscanf(pRecord, "%d", &iCount);fclose(pRecord);pRecord = NULL;}char savePath[128] = {0};sprintf(savePath,"D:\\test\\%0.4d.txt", iCount);FILE *pFile = fopen(savePath, "r");if (pFile){fscanf(pFile, "%lf", &calValue);fclose(pFile);pFile == NULL;}//this time, calTimes will subtract the iCountcalTimes = calTimes - iCount;printf("now we begin from iCount = %d\n", iCount);while (calTimes--){//this place, just for show, in the actual, the calculate express may be complex and a lot of values should be saved.calValue = calValue + 1 ;char savePath[128] = {0};sprintf(savePath,"D:\\test\\%0.4d.txt", ++iCount);printf("now we begin save file %s", savePath);FILE *pFlie = fopen(savePath, "w");if (pFlie){fprintf(pFlie,"%f\n", calValue);fclose(pFlie);pFlie = NULL;}printf("end save\n");//because i want to see the effect, so sleep 1 secondSleep(1000);printf("now we begin record\n");FILE *pRecord = NULL;pRecord = fopen("D:\\test\\record.txt","w");if (pRecord){fprintf(pRecord, "%d", iCount);fclose(pRecord);pRecord = NULL;}printf("end record\n");}return 0;}问题2:现在改变条件,假如需要更新某一个文件,当更新到一半时,程序退出,如何进行处理。
解决方案:备份原文件,修改当前文件,更新完成,则删除备份文件;更新未完成,则删除当前文件,将备份拷贝为当前文件。
通过SetConsoleCtrlHandler()设置消息回调函数,捕获程序退出响应,判断文件是否更新完成,做相应的处理。
问题3:接着问题2的情况,突然断电了,文件未更新完毕。
解决方案:在启动程序时,进行检测,如果存在备份文件,则删除当前文件,采用备份文件。
- c语言连续存储文件中断
- C语言连续存储实现队列机制
- C语言实现线性存储之连续存储
- C语言文件的存储
- c语言fwrite和fread连续读写文件流
- C 语言文件存储 文本 2进制
- 线性结构 -- 连续存储(数组), 1个简单的c语言代码实现.
- C语言动态连续数组
- C语言中断程序解读
- C语言中断原理简介
- C语言中断处理机制
- C语言的中断信号
- C语言实现文件按行倒序存储
- 用C语言实现通讯录文件存储的版本
- 【C语言】文件存储写入信息(文本方式)
- 通讯录c语言实现(动态内存开辟&文件存储)
- C的中断和反码数据存储
- c语言变量存储
- 试探何为RSS之我见
- 单网卡多IP
- 堆排序
- 赌神--斯杜·恩戈
- 补码的实质
- c语言连续存储文件中断
- POSIX线程
- 数据挖掘学习顺序
- miui恢复联系人之我的方法
- 创建Windows服务
- ios忽略2个字符串的大小写,再判断他们是否相等的
- android里面EditTex多行输入及输入置顶问题
- CBQ的简单性能调优
- linux 自动执行 crontab学习笔记