我有一个批处理文件如下
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文件输出的正确输出。批处理文件中的问题是什么?
答案 0 :(得分:2)
您正在使用ANSI编码打开/写入文件,但是wmic输出UTF-16,这是您需要采取的修复:
将此添加到您的文件开头:
FILE *f = fopen("file.txt", "a, ccs=UTF-16LE");
并将此fprintf
替换为:
fwprintf(f,L"...\n", ...);
答案 1 :(得分:0)
好的我能够通过编辑批处理文件来解决问题,如下所示
program
wmic cpu get CurrentClockSpeed >> file.txt
“&GT;&gt;” 中将命令输出附加到文件末尾,而不删除文件中已有的信息