MCyouwan 2022-12-07 16:20 采纳率: 100%
浏览 41
已结题

[C++]求助,一个未知问题

代码:

#include <iostream>
#include <windows.h>
#include <ctime>
#include <cstdlib>
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <bits/stdc++.h>
#include <set> 
#include <map>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <conio.h>
using namespace std ;
void xinghao()
{
    cout << "*****************" << endl ;
}
static string time()
{
    time_t t = time(NULL) ;
    char ch[64] = {0} ;
    strftime(ch , sizeof(ch) -1 , "%Y-%m-%d %H:%M:%S" , localtime(&t)) ;
    return ch ;
}
int main()
{
    ofstream log("CHJ.log" , std::ios::out | std::ios::app) ;
    log << "[" << time() << "]" << "opened" << endl ;
    log << "[" << time() << "]" << "loading" << endl ;
    
    int a = 0 ;
    int times ;
    ifstream dat("times.dat" , ios::in) ;
    dat >> times ;
    dat.close() ;
    ofstream data("times.dat" , ios::trunc | ios::out) ;
    if(times >= 1 && times < 4000000)
    {
        times++ ;
        data << times ;
    }
    else
    {
        data << "1" ;
    }
    data.close() ;
    
    log << "[" << time() << "]" << "the load was complete" << endl ;
    log << "[" << time() << "]" << "this procedure is from MCyouwan" << endl ;
    log << "[" << time() << "]" << "this procedure's version is rd-1,open times:" << times << endl ;
    xinghao() ;
    cout << "吃鸡游戏" << endl << "作者:MCyouwan" << endl ;
    xinghao() ;
    cout << "打开次数:" << times << endl ;

其中,输出的“打开次数”为4633604(初次),并且times.dat显示也为4633604。并且往后打开会不断+1(在这个数字的基础上)。
理想状态下应为:
初次打开:“打开次数”为1,times.dat为1
2次打开:“打开次数”为2,times.dat为2
以此类推
不知道为什么,它总会自动生成一个不知道是什么的数字,但程序无报错
求解答

  • 写回答

1条回答 默认 最新

  • 赵4老师 2022-12-07 18:08
    关注

    乍看起来c++的cin、cout、ifstream、ofstream、istringstream、ostringstream在输入、输出上比c的scanf、printf、fscanf、fprintf、fread、fwrite、sscanf、sprintf简单,不用格式控制符!
    但是不用格式控制符,输入输出恰好是你期望的格式的时候好说;等到输入输出不是你期望的格式的时候,你就会觉得还是用格式控制符更方便、更靠谱。
    摒弃cin、cout、ifstream、ofstream、istringstream、ostringstream!
    使用scanf、printf、fscanf、fprintf、fread、fwrite、sscanf、sprintf。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月26日
  • 已采纳回答 12月18日
  • 创建了问题 12月7日

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单