Author: Zhu Jincan
Source: http://blog.csdn.net/clever101
Recently, I want to debug a network program, which is generally a program that receives socket strings. The network is somewhat unstable. In many cases, the program can normally receive data, but occasionally fails. I found that the better method is to write a log file for analysis.
What content should be written into the log file? I thought about three aspects:
1. Time when data is received
2. Length of the received data (ensure that the content is correct only when the length is correct)
3. Content of received data
The following code retrieves the local time and writes the file:
- # Include <stdio. h>
- # Include <windows. h>
- Int main (INT argc, char * argv [])
- {
- File * fp = fopen ("log.txt", "A + ");
- If (null = FP)
- Return 1;
- Systemtime now;
- Getlocaltime (& now); // obtain the local time
- Char sztimebuf [25];
- Memset (sztimebuf, '', 25 );
- // Format the time
- Sprintf (sztimebuf, "/n % 02d/% 02d/% 02d % 02d: % 02d: % 02d", now. wyear, now. wmonth, now. wday, now. whour, now. wminute, now. wsecond );
- Fwrite (sztimebuf, FP); // write the file
- Fclose (FP );
- Return 0;
- }