(solved) strange thing_one digit missing when using %d for standard output [modified]
-
I am recording to current time, and compare with the old time which stored in a file. but here t4 is always one digit less. for example, when I check file end.txt and start.txt, t4 is 22284, and t3 is 21975, no problem. but when I calculate and output differ, it shows 2228-21975=-19747. t4 is wrong! what is the problem here?
int differ=0; // int old_time=0; int t3=0; int t4=0; time_t t1,t2; FILE * pFile; (void) time(&t2); // record the current time t4=(int)t2; pFile = fopen ("/root/pclinq/end.txt","r+"); fprintf (pFile, "%d",t4); //save the current time to the file fclose (pFile); pFile = fopen ("/root/pclinq/start.txt","r+"); fscanf (pFile, "%d", &t3); //dig out the old time saved fclose (pFile); differ=t4-t3; printf("differ is %d-%d=%d\n",t4,t3,differ);
(solved)it turns out the last digit was a remaining one, for example,2228 is the real number recorded this time -- modified at 8:51 Tuesday 17th July, 2007 -
I am recording to current time, and compare with the old time which stored in a file. but here t4 is always one digit less. for example, when I check file end.txt and start.txt, t4 is 22284, and t3 is 21975, no problem. but when I calculate and output differ, it shows 2228-21975=-19747. t4 is wrong! what is the problem here?
int differ=0; // int old_time=0; int t3=0; int t4=0; time_t t1,t2; FILE * pFile; (void) time(&t2); // record the current time t4=(int)t2; pFile = fopen ("/root/pclinq/end.txt","r+"); fprintf (pFile, "%d",t4); //save the current time to the file fclose (pFile); pFile = fopen ("/root/pclinq/start.txt","r+"); fscanf (pFile, "%d", &t3); //dig out the old time saved fclose (pFile); differ=t4-t3; printf("differ is %d-%d=%d\n",t4,t3,differ);
(solved)it turns out the last digit was a remaining one, for example,2228 is the real number recorded this time -- modified at 8:51 Tuesday 17th July, 2007bloodwinner wrote:
(solved)it turns out the last digit was a remaining one, for example,2228 is the real number recorded this time
and I guess the more appropriate open mode for writing a file is "w+" ... :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }