?
Solved

time

Posted on 2003-03-01
6
Medium Priority
?
223 Views
Last Modified: 2010-04-15
Hi,
I am write a program about the difference of two given times.
First, I need to retrieve the time(year, month , day, hour ,min, sec) from the file. And then I need to find the time different in the file.
However, those data in the file is string. So I need to convert them to int in order to assign in the time structure. This is tedious!

Do you guys have any good idea so I can get rid of this tediou method?
Thanks
0
Comment
Question by:kahou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Expert Comment

by:echard
ID: 8047829
if unerstand correctly.. can you just typecast the value you are reading in.. i.e.

say the value you read in form month is stored in char *month, which is month = "12"... depending on exactly what youy want could you do something like...

char *month = "THE VALUE YOU READ IN";
int imonth = (int) smonth;

etc..

does this help at all or am i way off base here?

echard
0
 

Expert Comment

by:echard
ID: 8047860
above should read:

char *smonth = "THE VALUE YOU READ IN";
int imonth = (int) smonth;

ech
0
 

Expert Comment

by:echard
ID: 8047897
Please shoot me for being an absolute moron... I am incorrect in what I previously told you... you would have to get the ASCII value of character you read in and convert it to int.


sorry

i am being stupid today.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Exceter
ID: 8048823
>> ...in the time structure.

Why don't you simply read and write entire structures to disk using fread() and fwrite()?

Exceter
0
 

Author Comment

by:kahou
ID: 8048974
If I use fread and fwrite, it still read the string. However, how to assign it into integer.
for instance:
if the file is written as,

2003 FEB 21 12 23 32

where 12 is hour,23 is minutes, 32 is second and 21 is date.

and this time is generated by localtime(). So if the time is generated in the morning. the time will only have 1 - 9 but not 01 - 09. This will make it tedious when I try to convert this string into int.
0
 
LVL 8

Accepted Solution

by:
Exceter earned 80 total points
ID: 8049049
If you used fread() and frwrite() these times would be written to disk in binary, not text, and would be read back into the integer in the structure. However, if you want to read these values back in a string and convert them to integer you can simply use the atoi function. For example,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>

int main()
{
     FILE* f;
     char month[25],temp[25];
     int year, hour, minute, second, date;

     f = fopen( "test.txt", "r" );

     fscanf( f, "%s", temp );
     year = atoi( temp );
     fscanf( f, "%s", temp );
     strcpy( month, temp );
     fscanf( f, "%s", temp );
     date = atoi( temp );
     fscanf( f, "%s", temp );
     hour = atoi( temp );
     fscanf( f, "%s", temp );
     minute = atoi( temp );
     fscanf( f, "%s", temp );
     second = atoi( temp );

     fclose(f);

     printf("%i %s %i %i %i %i\n", year, month, date, hour, minute, second);

     return 0;
}

-- Test.txt --

2003 FEB 21 12 23 32

-- end of file --

You can place this code into a loop to read more than one line. Alternativly you could read in the entire line and use strtok() to parse out the individual values. You can use fgets() to read in an entire line. Or, if you are interested, you could use fread() and fwrite() to read/write the values to/from disk.

Exceter
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question