读书人

在Linux环境上为什么用cat 命令输出到

发布时间: 2012-08-10 12:19:33 作者: rapoo

在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这个字符串。求解。。。。。。

[解决办法]

探讨

什么乱七八糟的!

那不是乱码,那些是终端控制字符,控制显示格式的。top 输出用于重定向要加 -b 选项。

读书人网 >C语言

热点推荐