日期:2014-05-16  浏览次数:20630 次

popen函数用法...急,在线等
请教个问题
我用popen打开一个执行命令
代码如下
C/C++ code

    fd = popen("mkfs.ext3 /dev/sda1", "r");
    if(!fd)
    {
        return ERROR;
    }
    while(fread(buf, 1, 1, fd))        
    {          
        printf("%s", buf);
    }


为何我打印出来的只是
mke2fs 1.38 (30-Jun-2005)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
2011296 inodes, 4016242 blocks
200812 blocks (5.00%) reserved for the super user
First data block=0
123 block groups
32768 blocks per group, 32768 fragments per group
16352 inodes per group
Superblock backups stored on blocks: 
  32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208

Writing inode tables: done  
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

而手动执行mkfs.ext3 /dev/sda1
显示如下
/dev $ mkfs.ext3 sda1 
mke2fs 1.38 (30-Jun-2005)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
2011296 inodes, 4016242 blocks
200812 blocks (5.00%) reserved for the super user
First data block=0
123 block groups
32768 blocks per group, 32768 fragments per group
16352 inodes per group
Superblock backups stored on blocks: 
  32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208

Writing inode tables: 61/123

我想通过popen读取格式化硬盘的进度。该如何做啊?

------解决方案--------------------
用fgets吧
------解决方案--------------------
用fgets + sscanf 函数,解析你的打印信息,但是解析的速度如果比格式化速度慢的话 你的进度信息将会是不准确的。还有就是看看mkfs的源代码,在里面将格式化进度的那个值取出来进行。类似的操作在cdrecord里就有。