Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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(&currentTime));  
  
               fout << ctime(&currentTime);
                  
                 key=='0';
 
 
                       }
                       if (key =='b')
                       break;
                       }
                       fout.close();
system("PAUSE");
  return 0;
}
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
Posted 16-Dec-12 2:46am
Edited 17-Dec-12 4:52am
v4

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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;
}
  Permalink  
v2
Comments
Quirkafleeg at 17-Dec-12 7:15am
   
+5. A rare occasion when a C++ question is answered using C++, rather than C!
jibesh at 17-Dec-12 14:50pm
   
Thanks. Quirkafleeg.
Member 9689332 at 17-Dec-12 8:41am
   
I can output the time but when I update the time, the old time is overwrote.
How can I update the new time into a new line?
jibesh at 17-Dec-12 14:55pm
   
Did you check the link i provided in my solution. you need to open a file with append flag on.
eg:
 
ofstream myfile;
myfile.open ("example.bin", ios::out | ios::app | ios::binary);
 
os::app All output operations are performed at the end of the file, appending the content to the current content of the file. This flag can only be used in streams open for output-only operations.
 
Read the link carefully you will get the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 853
1 OriginalGriff 410
2 CPallini 275
3 George Jonsson 226
4 Richard Deeming 145
0 OriginalGriff 5,450
1 CPallini 4,500
2 Sergey Alexandrovich Kryukov 4,482
3 George Jonsson 3,057
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 17 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100