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
bloodwinner
Posts
-
(solved) strange thing_one digit missing when using %d for standard output [modified] -
need a similar function as "getchar()" -
need a similar function as "getchar()"I had been used multithread, and it works strange. It works fine when I run the program at foreground, the first thread keeps running while the second thread waiting for input. But it does not work well when I run it at background. Without any inputs, both the first and second threads will stop. Only after I manually switching it back to run at foreground, then the first thread runs again. So I really want to find something similar and simpler than the mulithread way.
-
need a similar function as "getchar()"not necessarily a single function, but with the usage: keep program running, while accept the input if any
-
need a similar function as "getchar()"if I am using getchar(), then the execution of the program will stop here if no input is given. could someone help me figure out if there is another way? Aim: while any input is given, save it to a variable, if not, keep on executing program without stop. I have used multithread, but it did not work well when I put it running at background. I am using C
-
need a similar function as "getchar()"if I am using getchar(), then the execution of the program will stop here if no input is given. could someone help me figure out if there is another way? Aim: while any input is given, save it to a variable, if not, keep on executing program without stop. I have used multithread, but it did not work well when I put it running at background. I am using C
-
how to keep on this program executing when I put it at the backgound?errr.... I am only using C, so could you give me some information related to it?
-
how to keep on this program executing when I put it at the backgound?thank you for reply. 1.I guess it does not have much thing to do with the platform. 2.Regarding to the parameters of pthread, I took reference from here http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html#BASICS it looks working fine, at least as I said when executing it at the foreground, but because I can open only one terminal, probably have to do several things, so I can only put it running at the background, there problems occurs.
-
how to keep on this program executing when I put it at the backgound?the scenario is I am counting down 5, if no 'y' has been input during this time, then it ends at one way, else it ends at another way. Because I dont expect there must be a 'y' input, I write this as multi-thread, one for counting down, one for checking the 'y' input (could be unnecessary). this program works fine when I put it at the foreground(using linux), but when put it at background, error happens, it will stop and wait for getchar() input, and m doesnot count down. As long as I use shell command "fg" to bring it back to foreground, the count down resumes, but that is not what I want. what I want is just: put the program execution at background, while waiting for a 'y' input during 5 seconds count down time.
void *countdown( ); void *waitinput( ); static int m=5; static int k=1; int main(int argc, char **argv) { pthread_t thread1=0, thread2=0; int iret1=0, iret2=0; /* Create independent threads each of which will execute function */ iret1 = pthread_create( &thread1, NULL, countdown, NULL); iret2 = pthread_create( &thread2, NULL, waitinput, NULL); /* Wait till threads are complete before main continues. Unless we */ /* wait we run the risk of executing an exit which will terminate */ /* the process and all threads before the threads have completed. */ pthread_join( thread1, NULL); // pthread_join( thread2, NULL); pthread_cancel(thread1); pthread_cancel(thread2); printf("pthread been cancelled\n\n"); if(k=1){...} else {...} return 0; } void *countdown() { while(m>0) { printf("%d seconds left\n",m--); sleep(1); printf("m is %d\n",m); } } void *waitinput() { char c='n'; while(m) { if((c=getchar())=='y') { printf("congratulations. you entered y\n"); sleep(1); m=0;k=0; } else ; } }
-
how to keep on this program executing when I put it at the backgound?the scenario is I am counting down 5, if no 'y' has been input during this time, then it ends at one way, else it ends at another way. Because I dont expect there must be a 'y' input, I write this as multi-thread, one for counting down, one for checking the 'y' input (could be unnecessary). this program works fine when I put it at the foreground(using linux), but when put it at background, error happens, it will stop and wait for getchar() input, and m doesnot count down. As long as I use shell command "fg" to bring it back to foreground, the count down resumes, but that is not what I want. what I want is just: put the program execution at background, while waiting for a 'y' input during 5 seconds count down time.
void *countdown( ); void *waitinput( ); static int m=5; static int k=1; int main(int argc, char **argv) { pthread_t thread1=0, thread2=0; int iret1=0, iret2=0; /* Create independent threads each of which will execute function */ iret1 = pthread_create( &thread1, NULL, countdown, NULL); iret2 = pthread_create( &thread2, NULL, waitinput, NULL); /* Wait till threads are complete before main continues. Unless we */ /* wait we run the risk of executing an exit which will terminate */ /* the process and all threads before the threads have completed. */ pthread_join( thread1, NULL); // pthread_join( thread2, NULL); pthread_cancel(thread1); pthread_cancel(thread2); printf("pthread been cancelled\n\n"); if(k=1){...} else {...} return 0; } void *countdown() { while(m>0) { printf("%d seconds left\n",m--); sleep(1); printf("m is %d\n",m); } } void *waitinput() { char c='n'; while(m) { if((c=getchar())=='y') { printf("congratulations. you entered y\n"); sleep(1); m=0;k=0; } else ; } }
-
how to write string to MS access database through DAO?I know how to read string values from Access db, like: CString strFields[5]; COleVariant varstring for(field=0; field<5;field++) { recordset.GetFieldValue(field, varstring); strFields[field]=V_BSTRT(&varstring); } then the string value from db. goes into strFields But what if I know the strFields, and want write to update the db. Does anyone know the adverse procedure?
-
how to write string to MS access database through DAO?I know how to read string values from Access db, like: CString strFields[5]; COleVariant varstring for(field=0; field<5;field++) { recordset.GetFieldValue(field, varstring); strFields[field]=V_BSTRT(&varstring); } then the string value from db. goes into strFields But what if I know the strFields, and want write to update the db. Does anyone know the adverse procedure?
-
need help with MFC access to DAO database problemI am a new learner to the Dao database operation through MFC access.For now I am trying to write a program, saving two values in two edit boxes to a column of MS Access 2000 table. There are not many samples available. Could any one write little sample codes or guide me to a tutorial. The msdn explanation seems a bit difficult and abstract for me.
-
type convert problemstring strd1 = "9"; int m=atoi(strd); Got the error: error C2664: 'atoi' : cannot convert parameter 1 from 'class std::basic_string,class std::allocator >' to 'const char *' how to change the second line to make it work?
-
how to move to the second line in C++ .txt file reading?if I have three words in there lines in a .txt file, and I want to read them to three string, how to do this using c++ ifstream? how to read every line(word)?
-
how to save two int values into two columns of excel file? and how toread them again?I am using C++ in VC++ 2003 there are some examples like this http://www.codeproject.com/cpp/miniexcel.asp but I don't quite understand. I hope someone could help me or guide me to some related documents, bow!
-
Urgently Need help to beginner's questionI am using the MFC dialog application. There are 3 edit box, add1, add2 and sum and a button "add", when exectuing, I input add1, add2, and use the "add" function dlg.m_sum=dlg.m_add1+dlg.m_add2 how to make the sum appears in the "sum" edit box? Do I have to add a view class or how to modify the OnPaint? I now only have add.cpp and addDlg.cpp
-
Need help to beginner's questionI am using the MFC dialog application. There are 3 edit box, add1, add2 and sum and a button "add", when exectuing, I input add1, add2, and use the "add" function dlg.m_sum=dlg.m_add1+dlg.m_add2 how to make the sum appears in the "sum" edit box? Do I have to add a view class or how to modify the OnPaint? I now only have add.cpp and addDlg.cpp
-
what is the use of "&" heredo you mean both these two ways are ok? which would you prefer here?
-
what is the use of "pOldPen" here?sorry, what is that mean? could you explain it a little detailed? what is the difference if I had not use it?