Khiempossible
05-11-2006, 02:16 AM
So I'm a first year civil engineering student. Haha. K. Then I lucked into this programming job in Switzerland. My boss has every understanding I'm a very beginner programmer. Anyways, i'm trying to develop a program that searches log files by date and outputs only the pieces of the log file of interest. Logfiles are dated by timestamps (Unix based) which are numbers. These numbers represent the number of seconds that have passed since jan 1 1970. Anyways, I need my user to input the date in standard form. dd/mm/yyyy hh:mm:ss and I need it to convert to timestamp form so I can search my logfiles.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<time.h>
/* ----------------------------------------------------------------------------
** Static Function Prototypes - necessity for conversion
*/
static time_t convert (char *buff);
static int convertMonth (char *mon);
static void showFormats (void);
/* ----------------------------------------------------------------------------
** Constant Definitions - necessity for conversion
*/
#define DATE_FORMAT_SPEC "%d %s %d %d:%d:%d" /* 25 Aug 2004 13:30:00 */
#define DATE_FORMAT_SPEC2 "%s %d %d:%d:%d %d" /* Aug 25 13:30:00 2004 */
#define DATE_FORMAT_SPEC3 "%s %s %d %d:%d:%d %d" /* Wed Aug 25 13:30:00 2004 */
#define DATE_FORMAT_SPEC4 "%d/%d/%d %d:%d:%d" /* 25/08/2004 13:30:00 */
/* ----------------------------------------------------------------------------
** Convert month string to integer - necessity for conversion
** Returns : -1 -> no match found, other ->
*/
static int convertMonth (char *mon)
{
char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int i;
/* search for match */
for (i=0; i<12; i++) {
if (!strcasecmp (months[i], mon)) {
break;
}
}
if (i == 12) {
return (-1);
}
return (i);
}
//conversion function stolen from time2int
static time_t convert (char *buff)
{
struct tm tmval;
char mon[16], day[16];
time_t tval;
int c, monval;
/* initialisation */
memset(&tmval, 0, sizeof(struct tm));
strcpy (day, "");
strcpy (mon, "");
tval = -1;
/* scan date fields from buffer */
if ( ((c = sscanf (buff, DATE_FORMAT_SPEC,
&tmval.tm_mday, mon, &tmval.tm_year,
&tmval.tm_hour, &tmval.tm_min, &tmval.tm_sec)) == 6) ||
((c = sscanf (buff, DATE_FORMAT_SPEC2,
mon, &tmval.tm_mday, &tmval.tm_hour, &tmval.tm_min,
&tmval.tm_sec, &tmval.tm_year)) == 6) ||
((c = sscanf (buff, DATE_FORMAT_SPEC3,
day, mon, &tmval.tm_mday, &tmval.tm_hour, &tmval.tm_min,
&tmval.tm_sec, &tmval.tm_year)) == 7) ||
((c = sscanf (buff, DATE_FORMAT_SPEC4,
&tmval.tm_mday, &tmval.tm_mon, &tmval.tm_year,
&tmval.tm_hour, &tmval.tm_min, &tmval.tm_sec)) == 6) ) {
/* try to convert month string */
monval = convertMonth (mon);
if (monval != -1) {
tmval.tm_mon = monval;
} else {
/* month was supplied as integer in buffer */
tmval.tm_mon -= 1;
}
/* perform conversion */
tmval.tm_isdst = -1;
tmval.tm_year -= 1900;
if ((tval = mktime (&tmval)) == -1) {
printf ("failed to mktime()\n");
return (-1);
}
}
return (tval);
}
int main()
{
for(;;)
{
FILE *fp; //file pointer
int i=0, j=0, k=0, l=0;
char filename[30], date1[10], date2[10], time1[10], time2[20], temp1[20], temp2[20], *temp1p, *temp2p;
time_t t1, t2;
//retrieving data
scanf("%s %s %s %s %s", filename, date1, time1, date2, time2);
if(filename[0]==0)
{
//bring up help documentation
}
else
{//exit clause
if((filename[0]=='e')&& (filename[1]=='x') && (filename[2]=='i') && (filename[3]=='t'))
{
exit(1);
}
else
{
printf("data is: %s %s %s %s %s\n", filename, date1, time1, date2, time2);
strcpy(temp1, " ");
strcpy(temp2, " ");
for(;date1[i]!='\0';i++)
{
temp1[i] = date1[i];
printf("%s\n", temp1);
}
temp1[i] = ' ';
printf("%s\n", temp1);
j=i+1;
for(; time1[j-i-1]!='\0';j++)
{
temp1[j] = time1[j-i-1];
printf("%s\n", temp1);
}
for(;date2[k]!='\0';k++)
{
temp2[k] = date2[k];
printf("%s\n", temp2);
}
temp2[k] = ' ';
printf("%s\n", temp2);
l=k+1;
for(; time2[l-i-1]!='\0';l++)
{
temp2[l] = time2[l-i-1];
printf("%s\n", temp2);
}
printf("\n%s\n", temp1);
printf("%s\n", temp2);
*temp1p = &temp1;
*temp2p = &temp2;
t1 = convert(temp1p);
t2 = convert(temp2p);
printf ("%d %d", (int) t1, (int) t2);
printf("data is: %s %s %s %s %s\n", filename, date1, time1, date2, time2);
//opening file and checking errors
if((fp=fopen(filename,"r"))==NULL)
{
printf("Cannot open file\n");
exit(1);
}
}
}
}
return 0;
}
that's my program. The first two functions my boss gave me in a program that converts real dates to timestamps (because there are no functions like this in a C library. Clearly the program is unfinished.
anyways, on to my problem. I don't exactly understand how the first two functions work. I assume they do since, I copied them directly from the source code of the time2int program. I kinda get the gist of it. the convert function takes a string pointer and returns the time value in a new variable type called time_t. I think.
Anyways, the specific problem for the moment is:
*temp1p = &temp1;
*temp2p = &temp2;
i'm trying to create a pointer of for the strings containing my real dates so that I can feed them to my convert function.
I get the following warnings:
assignment makes integer from pointer without a cast 159
assignment makes integer from pointer without a cast 160
I'm not sure if my pointers are working properly, I debug, and I believe i'm getting an address for my pointers. But when I feed it to my function I get -1 for my t1 and t2 variables which are supposed to be integers or a long series of numbers representing my timestamps. I think the problem lies there. help s'il te plait. thanks.
here's the time2int program I was given.
/* ----------------------------------------------------------------------------
** Program : time2Int.c
** Description : Convert a time string into an integer.
** ------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------------
** Standard headers
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <time.h>
/* ----------------------------------------------------------------------------
** Static Function Prototypes
*/
static time_t convert (char *buff);
static int convertMonth (char *mon);
static void showFormats (void);
/* ----------------------------------------------------------------------------
** Constant Definitions
*/
#define DATE_FORMAT_SPEC "%d %s %d %d:%d:%d" /* 25 Aug 2004 13:30:00 */
#define DATE_FORMAT_SPEC2 "%s %d %d:%d:%d %d" /* Aug 25 13:30:00 2004 */
#define DATE_FORMAT_SPEC3 "%s %s %d %d:%d:%d %d" /* Wed Aug 25 13:30:00 2004 */
#define DATE_FORMAT_SPEC4 "%d/%d/%d %d:%d:%d" /* 25/08/2004 13:30:00 */
/* ----------------------------------------------------------------------------
** Convert month string to integer
** Returns : -1 -> no match found, other ->
*/
static int convertMonth (char *mon)
{
char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int i;
/* search for match */
for (i=0; i<12; i++) {
if (!strcasecmp (months[i], mon)) {
break;
}
}
if (i == 12) {
return (-1);
}
return (i);
}
/* ----------------------------------------------------------------------------
** Perform conversion
** Returns : -1 -> error, other -> time as time_t
*/
static time_t convert (char *buff)
{
struct tm tmval;
char mon[16], day[16];
time_t tval;
int c, monval;
/* initialisation */
memset(&tmval, 0, sizeof(struct tm));
strcpy (day, "");
strcpy (mon, "");
tval = -1;
/* scan date fields from buffer */
if ( ((c = sscanf (buff, DATE_FORMAT_SPEC,
&tmval.tm_mday, mon, &tmval.tm_year,
&tmval.tm_hour, &tmval.tm_min, &tmval.tm_sec)) == 6) ||
((c = sscanf (buff, DATE_FORMAT_SPEC2,
mon, &tmval.tm_mday, &tmval.tm_hour, &tmval.tm_min,
&tmval.tm_sec, &tmval.tm_year)) == 6) ||
((c = sscanf (buff, DATE_FORMAT_SPEC3,
day, mon, &tmval.tm_mday, &tmval.tm_hour, &tmval.tm_min,
&tmval.tm_sec, &tmval.tm_year)) == 7) ||
((c = sscanf (buff, DATE_FORMAT_SPEC4,
&tmval.tm_mday, &tmval.tm_mon, &tmval.tm_year,
&tmval.tm_hour, &tmval.tm_min, &tmval.tm_sec)) == 6) ) {
/* try to convert month string */
monval = convertMonth (mon);
if (monval != -1) {
tmval.tm_mon = monval;
} else {
/* month was supplied as integer in buffer */
tmval.tm_mon -= 1;
}
/* perform conversion */
tmval.tm_isdst = -1;
tmval.tm_year -= 1900;
if ((tval = mktime (&tmval)) == -1) {
printf ("failed to mktime()\n");
return (-1);
}
}
return (tval);
}
/* ----------------------------------------------------------------------------
** Show valid date & time formats
*/
static void showFormats (void)
{
printf ("Valid formats :-\n");
printf (" 25 Aug 2004 13:30:00,\n");
printf (" Aug 25 13:30:00 2004,\n");
printf (" Wed Aug 25 13:30:00 2004,\n");
printf (" 25/08/2004 13:30:00\n");
}
/* ----------------------------------------------------------------------------
** Main
*/
int main (int argc,
char *argv[])
{
char buff[64], *c_p;
time_t tval;
int i;
/* convert args if present */
if (argc > 1) {
strcpy (buff, "");
for (i=1; i<argc; i++) {
strcat (buff, argv[i]);
strcat (buff, " ");
}
if ((tval = convert (buff)) == -1) {
printf ("Error : failed to convert time '%s'\n", buff);
showFormats();
} else {
printf ("%d\n", (int) tval);
}
} else {
/* interactive mode */
while (1) {
printf ("time string : ");
c_p = gets(buff);
if (c_p) {
if ((tval = convert (c_p)) == -1) {
printf ("Error : failed to convert time '%s'\n", c_p);
showFormats();
} else {
printf ("time as int = %d\n", (int) tval);
}
}
printf ("\n");
fflush (stdout);
}
}
exit (0);
}
Lastly, I'm programming in Eclipse with the CDT plug-in on WinXP with the GCC compiler from cygwin.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<time.h>
/* ----------------------------------------------------------------------------
** Static Function Prototypes - necessity for conversion
*/
static time_t convert (char *buff);
static int convertMonth (char *mon);
static void showFormats (void);
/* ----------------------------------------------------------------------------
** Constant Definitions - necessity for conversion
*/
#define DATE_FORMAT_SPEC "%d %s %d %d:%d:%d" /* 25 Aug 2004 13:30:00 */
#define DATE_FORMAT_SPEC2 "%s %d %d:%d:%d %d" /* Aug 25 13:30:00 2004 */
#define DATE_FORMAT_SPEC3 "%s %s %d %d:%d:%d %d" /* Wed Aug 25 13:30:00 2004 */
#define DATE_FORMAT_SPEC4 "%d/%d/%d %d:%d:%d" /* 25/08/2004 13:30:00 */
/* ----------------------------------------------------------------------------
** Convert month string to integer - necessity for conversion
** Returns : -1 -> no match found, other ->
*/
static int convertMonth (char *mon)
{
char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int i;
/* search for match */
for (i=0; i<12; i++) {
if (!strcasecmp (months[i], mon)) {
break;
}
}
if (i == 12) {
return (-1);
}
return (i);
}
//conversion function stolen from time2int
static time_t convert (char *buff)
{
struct tm tmval;
char mon[16], day[16];
time_t tval;
int c, monval;
/* initialisation */
memset(&tmval, 0, sizeof(struct tm));
strcpy (day, "");
strcpy (mon, "");
tval = -1;
/* scan date fields from buffer */
if ( ((c = sscanf (buff, DATE_FORMAT_SPEC,
&tmval.tm_mday, mon, &tmval.tm_year,
&tmval.tm_hour, &tmval.tm_min, &tmval.tm_sec)) == 6) ||
((c = sscanf (buff, DATE_FORMAT_SPEC2,
mon, &tmval.tm_mday, &tmval.tm_hour, &tmval.tm_min,
&tmval.tm_sec, &tmval.tm_year)) == 6) ||
((c = sscanf (buff, DATE_FORMAT_SPEC3,
day, mon, &tmval.tm_mday, &tmval.tm_hour, &tmval.tm_min,
&tmval.tm_sec, &tmval.tm_year)) == 7) ||
((c = sscanf (buff, DATE_FORMAT_SPEC4,
&tmval.tm_mday, &tmval.tm_mon, &tmval.tm_year,
&tmval.tm_hour, &tmval.tm_min, &tmval.tm_sec)) == 6) ) {
/* try to convert month string */
monval = convertMonth (mon);
if (monval != -1) {
tmval.tm_mon = monval;
} else {
/* month was supplied as integer in buffer */
tmval.tm_mon -= 1;
}
/* perform conversion */
tmval.tm_isdst = -1;
tmval.tm_year -= 1900;
if ((tval = mktime (&tmval)) == -1) {
printf ("failed to mktime()\n");
return (-1);
}
}
return (tval);
}
int main()
{
for(;;)
{
FILE *fp; //file pointer
int i=0, j=0, k=0, l=0;
char filename[30], date1[10], date2[10], time1[10], time2[20], temp1[20], temp2[20], *temp1p, *temp2p;
time_t t1, t2;
//retrieving data
scanf("%s %s %s %s %s", filename, date1, time1, date2, time2);
if(filename[0]==0)
{
//bring up help documentation
}
else
{//exit clause
if((filename[0]=='e')&& (filename[1]=='x') && (filename[2]=='i') && (filename[3]=='t'))
{
exit(1);
}
else
{
printf("data is: %s %s %s %s %s\n", filename, date1, time1, date2, time2);
strcpy(temp1, " ");
strcpy(temp2, " ");
for(;date1[i]!='\0';i++)
{
temp1[i] = date1[i];
printf("%s\n", temp1);
}
temp1[i] = ' ';
printf("%s\n", temp1);
j=i+1;
for(; time1[j-i-1]!='\0';j++)
{
temp1[j] = time1[j-i-1];
printf("%s\n", temp1);
}
for(;date2[k]!='\0';k++)
{
temp2[k] = date2[k];
printf("%s\n", temp2);
}
temp2[k] = ' ';
printf("%s\n", temp2);
l=k+1;
for(; time2[l-i-1]!='\0';l++)
{
temp2[l] = time2[l-i-1];
printf("%s\n", temp2);
}
printf("\n%s\n", temp1);
printf("%s\n", temp2);
*temp1p = &temp1;
*temp2p = &temp2;
t1 = convert(temp1p);
t2 = convert(temp2p);
printf ("%d %d", (int) t1, (int) t2);
printf("data is: %s %s %s %s %s\n", filename, date1, time1, date2, time2);
//opening file and checking errors
if((fp=fopen(filename,"r"))==NULL)
{
printf("Cannot open file\n");
exit(1);
}
}
}
}
return 0;
}
that's my program. The first two functions my boss gave me in a program that converts real dates to timestamps (because there are no functions like this in a C library. Clearly the program is unfinished.
anyways, on to my problem. I don't exactly understand how the first two functions work. I assume they do since, I copied them directly from the source code of the time2int program. I kinda get the gist of it. the convert function takes a string pointer and returns the time value in a new variable type called time_t. I think.
Anyways, the specific problem for the moment is:
*temp1p = &temp1;
*temp2p = &temp2;
i'm trying to create a pointer of for the strings containing my real dates so that I can feed them to my convert function.
I get the following warnings:
assignment makes integer from pointer without a cast 159
assignment makes integer from pointer without a cast 160
I'm not sure if my pointers are working properly, I debug, and I believe i'm getting an address for my pointers. But when I feed it to my function I get -1 for my t1 and t2 variables which are supposed to be integers or a long series of numbers representing my timestamps. I think the problem lies there. help s'il te plait. thanks.
here's the time2int program I was given.
/* ----------------------------------------------------------------------------
** Program : time2Int.c
** Description : Convert a time string into an integer.
** ------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------------
** Standard headers
*/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <time.h>
/* ----------------------------------------------------------------------------
** Static Function Prototypes
*/
static time_t convert (char *buff);
static int convertMonth (char *mon);
static void showFormats (void);
/* ----------------------------------------------------------------------------
** Constant Definitions
*/
#define DATE_FORMAT_SPEC "%d %s %d %d:%d:%d" /* 25 Aug 2004 13:30:00 */
#define DATE_FORMAT_SPEC2 "%s %d %d:%d:%d %d" /* Aug 25 13:30:00 2004 */
#define DATE_FORMAT_SPEC3 "%s %s %d %d:%d:%d %d" /* Wed Aug 25 13:30:00 2004 */
#define DATE_FORMAT_SPEC4 "%d/%d/%d %d:%d:%d" /* 25/08/2004 13:30:00 */
/* ----------------------------------------------------------------------------
** Convert month string to integer
** Returns : -1 -> no match found, other ->
*/
static int convertMonth (char *mon)
{
char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int i;
/* search for match */
for (i=0; i<12; i++) {
if (!strcasecmp (months[i], mon)) {
break;
}
}
if (i == 12) {
return (-1);
}
return (i);
}
/* ----------------------------------------------------------------------------
** Perform conversion
** Returns : -1 -> error, other -> time as time_t
*/
static time_t convert (char *buff)
{
struct tm tmval;
char mon[16], day[16];
time_t tval;
int c, monval;
/* initialisation */
memset(&tmval, 0, sizeof(struct tm));
strcpy (day, "");
strcpy (mon, "");
tval = -1;
/* scan date fields from buffer */
if ( ((c = sscanf (buff, DATE_FORMAT_SPEC,
&tmval.tm_mday, mon, &tmval.tm_year,
&tmval.tm_hour, &tmval.tm_min, &tmval.tm_sec)) == 6) ||
((c = sscanf (buff, DATE_FORMAT_SPEC2,
mon, &tmval.tm_mday, &tmval.tm_hour, &tmval.tm_min,
&tmval.tm_sec, &tmval.tm_year)) == 6) ||
((c = sscanf (buff, DATE_FORMAT_SPEC3,
day, mon, &tmval.tm_mday, &tmval.tm_hour, &tmval.tm_min,
&tmval.tm_sec, &tmval.tm_year)) == 7) ||
((c = sscanf (buff, DATE_FORMAT_SPEC4,
&tmval.tm_mday, &tmval.tm_mon, &tmval.tm_year,
&tmval.tm_hour, &tmval.tm_min, &tmval.tm_sec)) == 6) ) {
/* try to convert month string */
monval = convertMonth (mon);
if (monval != -1) {
tmval.tm_mon = monval;
} else {
/* month was supplied as integer in buffer */
tmval.tm_mon -= 1;
}
/* perform conversion */
tmval.tm_isdst = -1;
tmval.tm_year -= 1900;
if ((tval = mktime (&tmval)) == -1) {
printf ("failed to mktime()\n");
return (-1);
}
}
return (tval);
}
/* ----------------------------------------------------------------------------
** Show valid date & time formats
*/
static void showFormats (void)
{
printf ("Valid formats :-\n");
printf (" 25 Aug 2004 13:30:00,\n");
printf (" Aug 25 13:30:00 2004,\n");
printf (" Wed Aug 25 13:30:00 2004,\n");
printf (" 25/08/2004 13:30:00\n");
}
/* ----------------------------------------------------------------------------
** Main
*/
int main (int argc,
char *argv[])
{
char buff[64], *c_p;
time_t tval;
int i;
/* convert args if present */
if (argc > 1) {
strcpy (buff, "");
for (i=1; i<argc; i++) {
strcat (buff, argv[i]);
strcat (buff, " ");
}
if ((tval = convert (buff)) == -1) {
printf ("Error : failed to convert time '%s'\n", buff);
showFormats();
} else {
printf ("%d\n", (int) tval);
}
} else {
/* interactive mode */
while (1) {
printf ("time string : ");
c_p = gets(buff);
if (c_p) {
if ((tval = convert (c_p)) == -1) {
printf ("Error : failed to convert time '%s'\n", c_p);
showFormats();
} else {
printf ("time as int = %d\n", (int) tval);
}
}
printf ("\n");
fflush (stdout);
}
}
exit (0);
}
Lastly, I'm programming in Eclipse with the CDT plug-in on WinXP with the GCC compiler from cygwin.