Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Going Mad Please Help!!!!

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
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( &quot;test.txt&quot;, &quot;rt&quot; )) == NULL )
printf( &quot;error opening file \n&quot; );
else
printf( &quot;file opened \n&quot; );
/* Read in characters and place them in &quot;buffer&quot;: */
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( &quot;%s\n&quot;,buffer);

fclose(fp);

}

Can some one pls help me, or put me in the right direction.

Thanks!!.


 

Here is the code that works. check test1.txt for the result.

#include <stdio.h>

int main()
{
FILE *fp, *fp1; // file pointer

int nTimeFlag=0; // time flag
float nTmpTime=0.00; // ref time.



/* Open file for reading (fail if data does not exist) */
if( (fp = fopen( &quot;test.txt&quot;, &quot;rt&quot; )) == NULL )
printf( &quot;error opening file \n&quot; );
else
printf( &quot;file opened \n&quot; );

fp1 = fopen(&quot;test1.txt&quot;,&quot;w&quot;);

while(!feof(fp)){
// store original time in array time
float nTime;
char szBuf[100];
int nRet = fscanf(fp,&quot;%f%s&quot;,&nTime,szBuf);
if (nRet == -1) break;
if (nTime < nTmpTime)
{
if (!nTimeFlag)nTimeFlag=1;
else nTimeFlag =0;
}

nTmpTime = nTime;

if (nTimeFlag){
nTime +=12.00;
if (nTime >= 24.00) nTime -=24.00;
}
fprintf(fp1,&quot;%5.2f %s\n&quot;,nTime,szBuf);
}

fclose(fp);
fclose(fp1);
return 0;
}

 
Thanks very much Jfhuang,

1) It sort of worked, its ok unitl 12.00 midnight, and there aftere it just keeps on print 00.00000 number non stop.
2) When converting it dose not stop, i have to terminate the program, and when i have a look at Test1.txt, its about 25MB big. and at time i am unable to open this file, keeps on poping error message, &quot;unable to register document, The document may already be open&quot;, this is beacuse i had to terminate the program.
3) i tried the program with anothe text file, with time and text with 90 characters with spaces and character like &quot; / . (1966) etc. IT DIDNT WORK, is there a way round this??
4) I had to remove &quot;5.2&quot; from fprintf statment it didn't work.

Is there any thing else that i should try???

Thanks again!!!!
 
It seems you are using the different complier. I have tested it in Visual C++ 5.0. It works fine. Check what compiler you are using.
 
i got it working are few test, had remove 5.2 and add .2 on the fprintf statment, works fine after 12pm aswell, then only problem i have now is read the text after time
ie
reads fine when: 2.00 Good
fails on when it says: 2.00 Good Bye to The man (1990) Bye

I tried incrementing buffer size, it did'nt work.

any suggestion regarding this...

Thanks.
 
To get this, simply change the statement:

int nRet = fscanf(fp,&quot;%f%s&quot;,&nTime,szBuf);
if (nRet == -1) break;

to:

int nRet = fscanf(fp,&quot;%f&quot;,&nTime);
if (nRet == -1) break;
fgets(szBuf,100,fp);


Hope this can make you feel great.
 

To get this, simply change the statement:

int nRet = fscanf(fp,&quot;%f%s&quot;,&nTime,szBuf);
if (nRet == -1) break;

to:

int nRet = fscanf(fp,&quot;%f&quot;,&nTime);
if (nRet == -1) break;
fgets(szBuf,100,fp);


Hope this can make you feel great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top