Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   30 minutes interval (http://www.go4expert.com/forums/30-minutes-interval-t6245/)

gaurang2984 7Sep2007 15:26

30 minutes interval
 
how do i code for reading timestamp after every 30 minutes. this is required for
creating a txt. file with a timestamp label. pls advice
tks

shabbir 7Sep2007 18:10

Re: 30 minutes interval
 
You need a timer which triggers after every 30 minutes.

DaWei 7Sep2007 20:35

Re: 30 minutes interval
 
You received some good responses to this question on that other forum. For each response, you merely repeated your question. If you conduct this thread in that way, I will close it.

gaurang2984 8Sep2007 11:19

Re: 30 minutes interval
 
i do not expect any system details since u mentioned that u r old
tks

DaWei 8Sep2007 17:40

Re: 30 minutes interval
 
Your question was throughly answered on that other forum, including example code. I am not going to provide a link to that competing forum -- you know where you asked the question. I'm going to quote a couple of the posts for OTHERS on this forum that may have similar problems. I'm sure they'll make better use of the answers. Then I going to close this thread.
Quote:

OK, let's start with the basics:

You can get the timestamp from one of several functions in the <time.h> standard library; which one you should use depends on just what sort of timestamp you need. There are several kinds: for example, the time() function returns a time_t, which is a count of the seconds since 00:00 1 Jan 1970; the gmtime() and locatime() functions return a struct tm containing the year, month, day, hour, minute, and second; the asctime() and ctime() functions return a char array in the locally defined format; and stftime() (the one you probably want) fills a string buffer based on a formatting string which you pass it.

Note that under Linux, files are automatically given a system-dependent timestamp on creation, though I'm not sure how you can access it. If all you need is the file creation time, that might do better. OTOH, if the goal is to give the files a unique name, then there are other ways to do it without a timestamp at all. You would know better than I would what it is you actually need this for; I am assuming that you actually need the timestamp for the filename, as you state.

As for opening files, fopen(), the standard file opening function in <stdio.h> , will create a file if given a "w" argument on the name of a file that doesn't already exist. You would need to close it with fclose() before the end of the program, or the file handle may not get set correctly (unlikely, but you should close it anyway).

However, if this is going to be Linux specific, you may want to use creat() instead, which gives more control over the file creation and opening, and IIUC will allow you to create an empty file without opening it.
Quote:

If you need to have the program stop running for a time and then wake up to create one of these files, then you will want to use the sleep(1) system call declared in the <unistd.h> library header. You would need to loop on the sleep() call something like this:
Code:

#include <unistd.h>
int main()
{
    while (! exit_condition_of_daemon)
    {
        sleep(3600);  /* sleep for 30 minutes */
        do_something();
        write_timestamped_file();
      }
 }

I wrote this in a rather vague way, since I really don't know enough about what you are doing at this point to show anything more than this.

OTOH, if you the program to continue running, then at the 30 minute mark stop and write a file, then you would need to either a) put the whole program in a large master loop and poll the time (using one of the time functions I previously mentioned), or b) create a separate thread which will sleep until the time comes, then stop the main thread and handle the case which you need to write the timestamped files for, then restart the main thread. The latter is more efficient, but more work; given the wherewithal you've shown to date . I would recommend the polling approach.

It would help tremendously if you could tell us what you need this for - not what you are doing, that is, but why you are doing it. What is the purpose of the timestamped files in this program? What is the program supposed to do? Is it a daemon that is running in the background, or an app or utility that has to stop periodically to do something (e.g., logging)? Does it actually need to be a full program or can it be done with a cron script? Without knowing at least some of that. any help we give you is limited and general at best.

Finally, I want to re-iterate the point: show us that you are putting at least SOME effort into this yourself. We cannot and will not do this for you, especially since you've given us bupkus for details so far. If you need or want someone else to do the work, go to the Hire a Programmer forum, or a contracting service like Scriptlance or Rent-A-Coder. If your goal is to get free labor from us, or to trick us into helping you cheat on your schoolwork, then close this thread and begone: non serviam.

We do want to help, believe me. However, it is a two-way street. The purpose of this group is to help each other learn how to solve these problems, and you've shown no inclination to learn a damn thing so far. As Aesop said, 'The Gods help those who help themselves'. Expecting more than that from those of us who are of less divine nature is simply foolish and insulting.


All times are GMT +5.5. The time now is 12:45.