我有一个批处理文件如下所示
wmic cpu get CurrentClockSpeed > file.txt
program程序是以下命令的可执行文件
gcc -o program initial_scan.c -lpsapi下面是initial_scan.c文件
#include <stdio.h>
#include <windows.h>
#include <psapi.h>
#define DIV 1048576
#define WIDTH 7
int main(){
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
PERFORMANCE_INFORMATION statex2;
statex2.cb=sizeof(statex2);
GetPerformanceInfo(&statex2,statex2.cb);
FILE *f=fopen("file.txt","a");
if(f==NULL){
printf("error opening the file");
exit(1);
}
fprintf(f,"There is %*ld percent of memory in use.\n",WIDTH, statex.dwMemoryLoad);
fprintf(f,"There are %*I64d total MB of physical memory.\n",WIDTH, statex.ullTotalPhys/DIV);
fprintf(f,"There are %*I64d free MB of physical memory.\n",WIDTH, statex.ullAvailPhys/DIV);
fprintf(f,"There is %*d processes currently in the system. \n",WIDTH,statex2.ProcessCount);
fprintf(f,"There is %*d threads currently in the system. \n",WIDTH,statex2.ThreadCount);
return 0;
}当我运行批处理文件时,输出如下
CurrentClockSpeed
3101
桔牥獩†††㈠‵数捲湥⁴景洠浥牯⁹湩甠敳മ吊敨敲愠敲††〸㈷琠瑯污䴠⁂景瀠票楳慣敭潭祲മ吊敨敲愠敲††〶〲映敲䴠⁂景瀠票楳慣敭潭祲മ吊敨敲椠††〱‱牰捯獥敳畣牲湥汴⁹湩琠敨猠獹整ഠ吊敨敲椠†ㄠ㘳″桴敲摡畣牲湥汴⁹湩琠敨猠獹整ഠ当我在没有wmic命令的情况下编译和运行initial_scan.c文件时,我得到了正确的输出,这是我期望从initial_scan.c文件中得到的。批处理文件中有什么问题?
发布于 2014-01-08 06:45:12
不过,您正在使用ANSI编码来打开/编写您的文件,wmic输出UTF-16,下面是您需要采取的修复措施:
将此添加到您的文件打开部分:
FILE *f = fopen("file.txt", "a, ccs=UTF-16LE");然后像这样替换你的fprintf:
fwprintf(f,L"...\n", ...);发布于 2014-01-08 06:41:38
好的,我能够通过编辑批处理文件来解决这个问题,如下所示
program
wmic cpu get CurrentClockSpeed >> file.txt">>“将命令输出附加到文件的末尾,而不删除文件中已经包含的信息。
https://stackoverflow.com/questions/20987184
复制相似问题