LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-25-2011, 10:49 AM   #1
anon02
Member
 
Registered: Aug 2011
Posts: 223

Rep: Reputation: Disabled
Getting text between two points in C.


With Christmas in about 2 months, I have decided to modify my game, The Crimson Swarm, for a more christmassy version. Every day, the player will be given a new starter item, however I need to find the day. I have got it to print the time like;

Code:
Tue Oct 25 16:34:10 2011
However I want it to get 'Oct' and '25' from that char. I have searched the web for something which works like:
Code:
strcpy(c,function_name(time_string,4,7));
Which would set c to be Oct, and so on.
Are there any functions like that?


Also, is there a way to convert a char * to a int, for easier comparison.

Also, can anyone give me any pointers on how to save things to files, I know how to output them, but how could I make a folder in the home/user directory named '.christmasswarm' which has the files in?

Last edited by anon02; 10-25-2011 at 11:11 AM.
 
Old 10-25-2011, 11:13 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
If you do something along these lines
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void    main    (void)
{
        time_t  now;
        struct  tm      *tm;

        (void) time (&now);
        tm = localtime (&now);
        /*      show a two-digit year           */
        (void) fprintf (stdout, "Today is %02d/%02d/%02d\n",                                        
            tm->tm_mon+1, tm->tm_mday, tm->tm_year % 100);                                          
        /*      show a four-digit year          */                                                  
        (void) fprintf (stdout, "Today is %02d/%02d/%d\n",                                          
            tm->tm_mon+1, tm->tm_mday, tm->tm_year + 1900);                                         
        exit (EXIT_SUCCESS);
}
See how the tm structure elements are used? And, of course, you can use them in a if.

time.h has these structure elements:
Code:
struct tm
{
  int tm_sec;                   /* Seconds.     [0-60] (1 leap second) */
  int tm_min;                   /* Minutes.     [0-59] */
  int tm_hour;                  /* Hours.       [0-23] */
  int tm_mday;                  /* Day.         [1-31] */
  int tm_mon;                   /* Month.       [0-11] */
  int tm_year;                  /* Year - 1900.  */
  int tm_wday;                  /* Day of week. [0-6] */
  int tm_yday;                  /* Days in year.[0-365] */
  int tm_isdst;                 /* DST.         [-1/0/1]*/
};
So, if you follow the above example, you are going to get numeric values you can use for your purposes.

Hope this helps some.
 
Old 10-25-2011, 11:20 AM   #3
anon02
Member
 
Registered: Aug 2011
Posts: 223

Original Poster
Rep: Reputation: Disabled
Perfect. I am going to add that to the code once I have made a few tests with it, but it should be fine. Thanks!
 
Old 10-25-2011, 12:09 PM   #4
anon02
Member
 
Registered: Aug 2011
Posts: 223

Original Poster
Rep: Reputation: Disabled
Hmm... I tried setting two variables to be the date, however it errors:

Code:
void twelvedays(void)
{
        time_t  now;
        struct  tm      *tm;





        time (&now);
        tm = localtime (&now);
	month = tm->tm_mon+1;
	day = tm->tm_mday;
Is the code, and lines 161, 162, and 163:

Code:
     tm = localtime (&now);
	month = tm->tm_mon+1;
	day = tm->tm_mday
error with:

Code:
main.c:161:12: warning: assignment makes pointer from integer without a cast [enabled by default]
main.c:162:12: error: dereferencing pointer to incomplete type
main.c:163:10: error: dereferencing pointer to incomplete type
Which probably means I need something like 'int' or 'char', yet the example you gave did not have that, yet I can see no other reasons for it to fail. Any ideas?
 
Old 10-25-2011, 12:38 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
#include <time.h>

for a start
 
Old 10-25-2011, 12:48 PM   #6
anon02
Member
 
Registered: Aug 2011
Posts: 223

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
#include <time.h>

for a start
Ah, even though I had not put any of the other code, you still worked out that one. Works fine as rain now! Thanks!
 
Old 10-25-2011, 01:50 PM   #7
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
You might be going at it kind of sorta not quite what you want.

The tm structure elements are filled in by the call to localtime (&now) where now is the number of seconds from the epoch (00:00:00 01 Jan 1970). You only need to invoke tm = time (&now); once in your program (you're working with days, not seconds). So,
Code:
time_t  now;
struct  tm      *tm;

/*     get the system time in seconds from the epoch     */
(void) time (&now);
/*     convert that to local time                        */
tm = localtime (&now);
You'd only do that one time in a running program.

So, I think what you're trying to do is add a day (then another and another and so on) and use the tm structure?

OK, so here's a trick. A day is 86,400 seconds. Tomorrow is the value of now + 86,400. So
Code:
/*     what's tomorrow?                                 */
now += (time_t) 86400;
/*     fill the tm structure with tomorrow       */
localtime (&now);
and then use those values as you see fit (cute, eh?). The localtime() utility takes care of all the calendar changes (so, if somebody was running on 31 October and we add one day the tm structure will contain values for 01 November. Similarly, if you added five days (86400*5), you'd get a correct rollover at the end of a month, irrespective of what month it may be.

I didn't answer part of your question about writing to a file in the user's home directory.

All you'd need to do is a quick-and-dirty write of the value of now in a file, say a "dot" file so it's not readily visible to the user, something like .timestamp, something like this
Code:
time_t  now, oldtime;
struct  tm      *tm;
FILE    *out;

/*      get the current system time                             */
(void) time (&now);
/*      fill the tm structure elements                          */
tm = localtime (&now);
/*      initialize old timestamp                                */
oldtime = (time_t) 0;
/*      try to read an existing file                            */
if ((out = fopen (".timestamp", "r")) != (FILE *) NULL) {
        /*      it exists, read the value in it                 */
        (void) fscanf (out, "%ld\n", &oldtime);
        (void) fclose (out);
}
/*
        now, simply open it for writing (which clears the
        existing content) and put the current time stamp into it
*/
if ((out = fopen (".timestamp", "w")) == (FILE *) NULL) {
        /*      just in case there's a problem                  */
        (void) fprintf (stderr,
            "%s:\tcan't create %s\n",
            argv [0], ".timestamp");
        exit (EXIT_FAILURE);
}
/*      write the current time to the file              */
(void) fprintf (out, "%ld\n", now);
(void) fclsoe (out);
/*
 *      you can do whatever arithmetic you need to on the
 *      values of oldtime and now, just keep in mind that
 *      the difference between the two is what you need to
 *      work with and that oldtime will possibly be a few
 *      minutes ago, yesterday, a week ago or whatever and
 *      code appropriately to deal with that
*/
Hope this helps some.

Last edited by tronayne; 10-25-2011 at 01:53 PM.
 
Old 10-25-2011, 01:58 PM   #8
anon02
Member
 
Registered: Aug 2011
Posts: 223

Original Poster
Rep: Reputation: Disabled
Thanks for the tip. Now I have got it working (Or I think so, it counts 25 days and I have only tested it today. I am going to have November as testing month so I have 5 days now to add stuff, and 5 days after before release).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Bash command to 'cut' text into another text file & modifying text. velgasius Programming 4 10-17-2011 04:55 AM
cp adds exclamation points when copying very large text files 7565 Linux - Newbie 7 08-13-2009 06:04 PM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
play free (World of WarCraft , xBox Live, Live Points, Wii Points, Free Habbo) laraaj Linux - Games 1 02-09-2007 05:50 PM

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

All times are GMT -5. The time now is 08:13 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