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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分