鱼C论坛

 找回密码
 立即注册
查看: 2024|回复: 6

文件写入问题

[复制链接]
发表于 2018-1-1 11:19:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我想设计一个就是把用户输入的值保存进文本,并且能增加其相对应的值,我的思路是用字典来,不保存文件的情况下我已经实现了,现在先保存进文件却发现只能一次一次的写入而不能增加其值。
我的代码:
  1. def display():
  2.     print('*'*9+'|'+'-'*22+'|'+'*'*9)
  3.     print('*'*9+'|'+'时间统计计算记录小程序'+'|'+'*'*9)
  4.     print('*'*9+'|'+'-'*22+'|'+'*'*9)
  5.     print('*'*9+'|'+'版本:1.0             '+'|'+'*'*9)
  6.     print('*'*9+'|'+'最后更新:2018.1.1    '+'|'+'*'*9)
  7.     print('*'*9+'|'+'制作人:          '+'|'+'*'*9)
  8.     print('*'*9+'|'+'-'*22+'|'+'*'*9)
  9.     print('*'*10+'*'*22+'*'*10)
  10.     print('*'*9+'|'+'-'*22+'|'+'*'*9)
  11.     print('*'*9+'|请您选择要执行的代码  |'+'*'*9)
  12.     print('*'*9+'|1.时间值转换          |'+'*'*9)
  13.     print('*'*9+'|2.更新统计时间        |'+'*'*9)
  14.     print('*'*9+'|3.查看统计时间        |'+'*'*9)
  15.     print('*'*9+'|4.退出程序            |'+'*'*9)
  16.     print('*'*9+'|'+'-'*22+'|'+'*'*9)
  17.    
  18. def hours():
  19.     hours=float(input('请您输入小时:'))
  20.     minutes=hours*60
  21.     seconds=hours*3600
  22.     print('%d小时--->%d分钟--->%d秒'%(hours,minutes,seconds))
  23. def minutes():
  24.     minutes=int(input('请您输入分钟:'))
  25.     hours=minutes/60
  26.     print('%d分钟--->%.2f小时'%(minutes,hours))
  27. def txt():
  28.     for key in timedict.keys():
  29.         f.write(key+':'+str(timedict[key])+'小时'+'\n')
  30.     f.close()
  31.    
  32.    
  33. #f=open('F:\Timestatistics.txt','a+')
  34. display()
  35. flag=0
  36. timedict={}
  37. while flag==0:
  38.     choice_one=int(input('请您输入要执行的代码:'))
  39.     if choice_one ==1:
  40.         print('|--------------------|')
  41.         print('|1:小时--->分钟--->秒|')
  42.         print('|2:分钟--->小时      |')
  43.         print('|--------------------|')
  44.         choice_two=int(input('输入要执行的代码:'))
  45.         if choice_two==1:
  46.             hours()
  47.         elif choice_two==2:
  48.             minutes()
  49.     elif choice_one==2:
  50.         f=open('F:/Timestatistics.txt','a+')
  51.         print('|------------------|')
  52.         print('|选择更新时间的项目|')
  53.         print('|------------------|')
  54.         print('|1.睡觉            |')
  55.         print('|2.学习            |')
  56.         print('|3.讲课            |')
  57.         print('|4.锻炼            |')
  58.         print('|5.玩游戏          |')
  59.         print('|6.玩手机          |')
  60.         print('|------------------|')
  61.         choice_three=int(input('输入要执行的代码:'))
  62.         if choice_three ==1:
  63.             times=float(input('请您输入增加的小时:'))
  64.             timedict['睡觉']+=times
  65.             txt()
  66.         elif choice_three==2:
  67.             times=float(input('请您输入增加的小时:'))
  68.             timedict['学习']+=times
  69.             txt()
  70.         elif choice_three==3:
  71.             times=float(input('请您输入增加的小时:'))
  72.             timedict['讲课']+=times
  73.             txt()
  74.         elif choice_three==4:
  75.             times=float(input('请您输入增加的小时:'))
  76.             timedict['锻炼']+=times
  77.             txt()
  78.         elif choice_three==5:
  79.             times=float(input('请您输入增加的小时:'))
  80.             timedict['玩游戏']+=times
  81.             txt()
  82.         elif choice_three==6:
  83.             times=float(input('请您输入增加的小时:'))
  84.             timedict['玩手机']+=times
  85.             txt()
  86.         else:
  87.             print('您输入的代码有误!')
  88.         
  89.     elif choice_one==3:
  90.         f=open('F:/Timestatistics.txt','r')
  91.         for line in f:
  92.             print(line)
  93.         f.close()
  94.     elif choice_one==4:
  95.         print('退出系统')
  96.         flag=1
  97.         f.close()
  98.     else:
  99.         print('您输入的代码有误!')

复制代码


写入是这样的:
睡觉:8.0小时
学习:0小时
讲课:0小时
锻炼:0小时
玩游戏:0小时
玩手机:0小时
如果再次写入就会继续在后面打印:
睡觉:8.0小时
学习:0小时
讲课:0小时
锻炼:0小时
玩游戏:0小时
玩手机:0小时
睡觉:8.0小时
学习:0小时
讲课:1小时
锻炼:2小时
玩游戏:0小时
玩手机:0小时

而我想的是只更新相对应的数值,大神们能不能给我一点思路。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-1-1 11:34:07 From FishC Mobile | 显示全部楼层
用字典,首先从文件中读取,然后每次更新替换对应键的值,再存储即可
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-1 12:50:01 | 显示全部楼层
BngThea 发表于 2018-1-1 11:34
用字典,首先从文件中读取,然后每次更新替换对应键的值,再存储即可

在文件中建好字典吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-1 13:23:42 | 显示全部楼层
本帖最后由 yjsx86 于 2018-1-1 13:27 编辑

我想问 你的txt是准备给人看的?
如果给人看没意义啊 都写程序了 就在程序里显示啊
如果不给人看 那为什么不保存为sqlit或者其他的关系数据库
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-1 13:34:06 From FishC Mobile | 显示全部楼层
像番茄加两个蛋 发表于 2018-1-1 12:50
在文件中建好字典吗?

你可以根据键值对存放数据,这样读取的时候就可以准确对位了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-1 18:11:39 | 显示全部楼层
//试试这个
//调用modifyContentInFile(char *fileName,int lineNum,char *content)
//fileName是文件名,lineNum是第几行,content是更改后的内容
//函数modifyContentInFile将第lineNum行的内容改为content
#include <fstream>
#include <string>
using namespace std;

string charToStr(char * contentChar)
{
    string tempStr;
    for (int i=0;contentChar[i]!='\0';i++){
        tempStr+=contentChar[i];
    }
    return tempStr;
}
void modifyContentInFile(char *fileName,int lineNum,char *content)
{
    ifstream in;
    char line[1024]={'\0'};
    in.open(fileName);
    int i=0;
    string tempStr;
    while(in.getline(line,sizeof(line))){
        i++;
        if(lineNum!=i){
            tempStr+=charToStr(line);
        }else{
           tempStr+=charToStr(content);
        }
        tempStr+='\n';
    }
    in.close();
    ofstream out;
    out.open(fileName);
    out.flush();
    out<<tempStr;
    out.close();
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-1-1 18:14:06 | 显示全部楼层
OllyDbg123 发表于 2018-1-1 18:11
//试试这个
//调用modifyContentInFile(char *fileName,int lineNum,char *content)
//fileName是文件名 ...

你的不是Python呀
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-3-22 21:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表