在Linux环境下为什么用cat 命令输出到终端没有乱码。而我用vi打开则有
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
system("top -n 1 |grep Cpu | cut -d \",\" -f 1 | cut -d \":\" -f 2 >cpu.txt");
system("top -n 1 |grep Cpu | cut -d \",\" -f 2 >>cpu.txt");
system("top -n 1 |grep Mem | cut -d \",\" -f 1 | cut -d \":\" -f 2 >>cpu.txt");
system("top -n 1 |grep Mem | cut -d \",\" -f 2 >>cpu.txt");
return 0;
}
编译、运行:
[root@localhost study]# g++ test.cpp
[root@localhost study]# ./a.out
[root@localhost study]# cat cpu.txt
2.1%us
1.5%sy
2066240k total
1619784k used
但是用编辑器打开cpu.txt后出现的是乱码,而且将数据独到内存后输出的也是乱码!!!
我现在需要2.1%us这个字符串。求解。。。。。。
[解决办法]