首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

在Linux环境上为什么用cat 命令输出到终端没有乱码。而小弟我用vi打开则有

2012-08-10 
在Linux环境下为什么用cat 命令输出到终端没有乱码。而我用vi打开则有#include stdio.h#include unistd.

在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 选项。

热点排行