LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-08-2003, 09:33 AM   #1
wincrk
Member
 
Registered: Feb 2003
Distribution: Redhat 9.0
Posts: 104

Rep: Reputation: 15
time as the value of a variable in C


hi, i am a beginner in programming with C. for this moment, i am using borland turbo C++ 3.0 as my compiler. i wonder about certain thing:

1. how to set a variable to hold value of current time ? for example, i want to set variable 'the_time' to hold the current time of a particular machine and it will keep that value even the time on that machine is running.

2. usually, we define a value for a variable or the user will be asked to key in the value. the value that defined by programmer will remain unchanged when we close it and rerun the program, while the value that inserted by user will be lost when the program is close.
my question is how how to make the value that had been inserted by user will remained for a variable so the user could used the the same value again when he reopen the program?

if there any solution, please let me know, and what is the header file should included for both of my question.

please help me.
 
Old 04-08-2003, 10:52 AM   #2
Palin
Member
 
Registered: Feb 2003
Location: A Meatlocker, well feels like one
Distribution: Gentoo
Posts: 292

Rep: Reputation: 30
For time functions look for references on time.h

as for remembering the value of something you need to send it to permanant storage before you close the program ( ie write it to a file )
you would want either iostream.h for those types of functions. This is a small tutorial on file io
http://www.codenewbie.com/forum/show...=&threadid=755
 
Old 04-09-2003, 08:54 PM   #3
wincrk
Member
 
Registered: Feb 2003
Distribution: Redhat 9.0
Posts: 104

Original Poster
Rep: Reputation: 15
thanks, but actually.... it is hard for me to understand the tutorial. i think i need an example along the tutorial. a really clear example
 
Old 04-09-2003, 10:16 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Code:
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>

void main (void)
{
  time_t myTime;
  time(&myTime); // get current time
  string sMyTime = ctime( &myTime ); //convert time to "human readable"
  const char cFileName[]="saveTime";
  ofstream File;
  File.open( cFileName , ios::out );
  File.write( sMyTime.c_str(), sMyTime.size() );
}
This is C++, poorly (&quickly) whipped together,
no error handling of any kind, but you might get
the idea :}

Cheers,
Tink

P.S.: A nice, Unix specific introduction to
C can be found here

Last edited by Tinkster; 04-09-2003 at 10:18 PM.
 
Old 04-09-2003, 11:57 PM   #5
wincrk
Member
 
Registered: Feb 2003
Distribution: Redhat 9.0
Posts: 104

Original Poster
Rep: Reputation: 15
thanks Tinkster, this is better for me .
 
Old 04-10-2003, 02:34 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Pleasure mate, glad I could help :}

Cheers,
Tink
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 04:38 PM
Setting environment variable to current date/time nafan Programming 1 08-23-2004 09:04 PM
no signal when starting xorg for the 1st time (but the second time works fine) bungalowbill Linux - Software 0 06-04-2004 09:56 AM
calling a variable within a variable sdandeker Programming 9 04-28-2004 03:55 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration