-
File I/O convert
Going mad (Visitor) Oct 3, 2001
I have Text file as below on the left.
6.00 Peter 6.00 Peter
10.30 Joa 10.30 Joa
12.45 Mac 12.44 Mac
1.00 Jack 13.00 Jack
5.00 J9 17.00 J9
5.45 Liz ..
10.55 Mark ..
11.45 Peter 23.45 Peter
12.05 Mac 00.00 Mac
1.25 Joa 1.25 Joa
5.55 J9 5.55 J9
I am tring to read the above file from the start to end and convert the time to 24 hour time as shown on the right and save the text file with the converted time.
Heres what i have coded so far.
// Program to open .txt file covert and save to new name.
#include <stdio.h>
void main( void )
{
FILE *fp; // file pointer
float *time; // store original time
int nTimeFlag=0; // time flag
float nTmpTime=0.00; // ref time.
char buffer[900]; // store in read data
int i, ch;
/* Open file for reading (fail if data does not exist) */
if( (fp = fopen( "test.txt", "rt" )) == NULL )
printf( "error opening file \n" );
else
printf( "file opened \n" );
/* Read in characters and place them in "buffer": */
ch = fgetc( fp);
for( i=0; (i < 899 ) && ( feof(fp ) == 0 ); i++ )
{
// store original time in array time
buffer = (char)ch;
*time = buffer;
if (*time < nTmpTime)
{
if (!nTimeFlag)nTimeFlag=1;
else nTimeFlag =0;
}
buffer=*time;
if (nTimeFlag)*time +=12.00;
buffer =*time;
ch = fgetc( fp );
}
printf( "%s\n",buffer);
fclose(fp);
}
Can some one pls help me, or put me in the right direction, i can not get the time cinversion side working.
Thanks!!.
-
You implemented pretty weird functionality ;).
I cannot descipher, how are you obtaining the data needed for conversion...
You should scan each new line until first space, or whatever your delimiter char is, and convert the so obtained data.
I assume from following line:
>> 11.45 Peter 23.45 Peter <<
that the string format is
log_in_time/delimiter_char/ User_Name/delimiter_char/logout_time
It' would be much more professional if you'd use <fprintf> for writing to, and <fscanf> for reading from the file!
For a concrete example see fscanf example in the MSDN library (I can paste it here, if you don't have it).
Have fun!