如何将计算机时间导出到文本文件而不覆盖C ++ [英] how to export the computer time to text file without overwriting C++

查看:68
本文介绍了如何将计算机时间导出到文本文件而不覆盖C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个C ++程序员:

I want to write a C++ programmer to:

1. record the computer time when user click on the mouse
2. export the time to a file ( .txt)
3. update the file



对于第一部分,我尝试过:


For the first part, I have tried out:

#include <time.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{   //string record;

char key;

    ofstream fout;
    ifstream fin;

fin.open ("text.txt");
fout.open ("text1.txt");
    while(1){
        cin>>key;
             if (key=='a')
             {time_t currentTime = time(0);
               
               printf("The Current Time is %s", ctime(¤tTime));  
  
               fout << ctime(¤tTime);
                  
                 key=='0';

 
                       }
                       if (key =='b')
                       break;
                       }
                       fout.close();
system("PAUSE");
  return 0;
}



但每次重新启动.exe时,.txt文件都会被覆盖,

我需要保留旧版本数据并更新/插入新数据。



谢谢


But every time I restart the .exe, the .txt file is overwriten,
I need to keep the old data and update/insert the new data.

Thanks

推荐答案

您可以使用文件流类读/写文件。



使用输入/输出文件C ++ [ ^ ]



You can use the file stream classes to read/write files.

Input/Output files using C++[^]

#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}


这篇关于如何将计算机时间导出到文本文件而不覆盖C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆