我用hFocus取得当前活动窗口句柄,判断如果跟上次的活动窗口句柄不一样就读入,并用szTitle保存下来,可是为什么fprintf(stream,"%C%S%C%C%S",10,time1+time2,32,32,szTitle);后logfile.txt中有时间没有szTitle记录的标题呢,代码在下边
     FILE *stream=fopen("c:\\logfile.txt","a+t");
 //char szTitle[256];//当前窗口名称
hFocus=::GetActiveWindow();//取得当前活动窗口句柄
if(g_hLastFocus!=hFocus)
{
::GetWindowText(hFocus,szTitle,256);
g_hLastFocus=hFocus;
CTime pt;
CString time1,time2;
pt=CTime::GetCurrentTime();
time1=pt.Format(_T("%A,  %B   %d,   %Y  "));
time2=pt.Format(_T("%H : %M : %S "));
fprintf(stream,"%C%S%C%C%S",10,time1+time2,32,32,szTitle);
fprintf(stream,"%C%C",32,32);
fprintf(stream,"\r\n",32);
}
这是怎么回事啊?

解决方案 »

  1.   

    fprintf(stream,"%c%s%c%c%s",10,time1+time2,32,32,szTitle); 
    fprintf(stream,"%c%c",32,32); %C改成%c
    %S改成%s
      

  2.   

    那你的工程是采用unicode编译还是非unicode?
      

  3.   

    如果代码没问题,那么记录的是乱码的话,对应你的unicode项目的话,你需要把szTitle的类型转过来,可能你记录存的不是unicode.看你存szTitle是怎么存的
      

  4.   


    我代码里是用数组存放的wchar_t  szTitle[256];
    怎么转啊?我初学VC,不会啊,请楼上的大侠帮帮忙啊!!!!
      

  5.   

    看这样输出是什么fprintf(stream,"%c%s%c%c%s",10,time1+time2,32,32, CString(szTitle)); 
      

  6.   

    我刚试了,还是没有输出标题来....写入文件后的效果:
    Saturday,  March   22,   2008  16 : 56 : 55     
    kstar
      

  7.   


    我刚试了,还是没有输出标题来.... 写入文件后的效果: 
    Saturday,  March   22,   2008  16 : 56 : 55      
    kstar
      

  8.   

    单步调试,看CString(szTitle)里是什么
      

  9.   


    我调试了,CString(szTitle)里面的内容是正确的..
      

  10.   

    你看看fprintf 的用法吧
    #include  <stdio.h>  
    #include  <process.h>  FILE *stream;  void main( void )  
    {  
    int i = 10;  
    double fp = 1.5;  
    char s[] = "this is a string";  
    char c = '\n';  stream = fopen( "fprintf.out", "w" );  
    fprintf( stream, "%s%c", s, c );  
    fprintf( stream, "%d\n", i );  
    fprintf( stream, "%f\n", fp );  
    fclose( stream );  
    system( "type fprintf.out" );  
    }  屏幕输出:  this is a string  
    10  
    1.500000