473,394 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

to get time and write it to a file in c

i have to write the current time to a file ?how to do it
Jul 19 '13 #1
2 5177
I have never extracted the system time in c++, but I recently worked on my own io class. As a result, I can help you with the part that deals with writing to a file. First, become familiar with the ofstream class (http://www.cplusplus.com/reference/fstream/ofstream/). The class is not hard to implement as you basically need to specify the file to open. If no file with the specified name is found, ofstream will create a new file with that name. Then, you can pass a string by using the << operator, such that outputFile << timeString;. Finally, close the file so you don't leak file handles. I was a bit vague because I want you to try it and come back with clarification questions. :D
Jul 19 '13 #2
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include <sys/time.h>
  7.  
  8. void getTime()
  9. {
  10.     FILE* fp = fopen("Time.txt","w+");
  11.     struct timeval usec_time;
  12.     time_t now = time(0);
  13.     gettimeofday(&usec_time,NULL);
  14.     // format time
  15.     struct tm *current = localtime(&now);
  16.     printf("%d:%d:%d\n",current->tm_hour, current->tm_min, current->tm_sec);
  17.     fprintf(fp,"%d:%d:%d\n",current->tm_hour, current->tm_min, current->tm_sec);
  18. }
  19.  
  20.  
  21.  
  22. int main(int argc,char* argv[])
  23. {
  24.     getTime();
  25. }
  26.  
Jul 23 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Lauren Quantrell | last post by:
I'd like to use my Access 2K application to write some text in the file properties of a Windows file. The properties I'm talking about are the properties you see whn you right click on a Windows...
16
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
0
by: shefali | last post by:
Hi, I need to use an HTTPHandler to create and write files. I wanted to create the file in the directory that has this handler, and other handlers. For me, this directory is:...
1
by: Tim | last post by:
Hi, What I am trying to accomplish is to initiate remote desktop session from within my C# application to XP Pro machine NOT terminal services to a server. I have not found any way to do this...
5
by: MichaelK | last post by:
I need to write a log file on server side. I'm using the code below. By some reason it just hanging on and not doing anything. Just the progress bar is moving very very slow and maximum gets to...
1
by: Gregory Gadow | last post by:
I have a folder on \\webserver\wwwroot, called staticContent. The folder has several message of the day text files, such as RepMotd.text. I have an admin page, AdminMotd.aspx, that allows a user to...
4
by: childtobe | last post by:
I want to write the time to a file. (to implement this in an other piece of code that i've writen) For some reason he gives me a C2064 error when i try to put the time into a string. As you can...
1
by: COHENMARVIN | last post by:
Hi, I need to find the Windows C++ functions that get the time a file was written to, read from, and last accessed. Is there a way to do that? Thanks, Marvin
0
by: Swan | last post by:
Can anyone plz tell me,Can I write file on server using binary access,or it is only to write on local?(Actually I am creating OCX for Http File Upload Control in VB using API's.So for that I need to...
4
by: Bruno | last post by:
Hi! I have big .txt file which i want to read, process and write to another .txt file. I have done script for that, but im having problem with croatian characters (Š,Đ,Ž,Č,Ć). How can I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.