免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1613 | 回复: 2
打印 上一主题 下一主题

结构体中字符串的比较 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-27 15:18 |只看该作者 |倒序浏览
有一个结构体
struct type
{char disk[100];
int  percent;
}type1;

char disk[100]里面是:/dev/cciss/c0d0p1
                      /dev/cciss/c0d0p2
                      none
                      /dev/cciss/c0d0p3
                      /dev/cciss/c0d0p5
int percent里面是: 53
                    6
                    0
                    24
                    9
现在我想让用户手动输入磁盘的目录名,例如:/dev/cciss/c0d0p1。如果输入的目录名在char disk[100]里面存在和输入进去的东西相同的时候,则输入对应的percent。
例如,刚刚我键入/dev/cciss/c0d0p1的时候,则应该在屏幕上打印53.可是显示没有该目录。

以下是我写的代码,达人帮我看看;

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct type
{char disk[100];
int  percent;
}type1;
char destney[100];
int main(int argc, char* argv[])
{
  FILE *useshell;
  FILE *creatfile;
  FILE *read_in;
  char buf[1024];
  struct type *p;
  p=&type1;
    memset(buf,'\0', sizeof(buf));
    useshell=popen("df -k | awk 'NR>1{print $1,$5+0}'","r");
    creatfile=fopen("used_percent.txt","w+");
    fread(buf,sizeof(char),sizeof(buf),useshell);
    fwrite(buf,1,sizeof(buf),creatfile);
    pclose(useshell);
    fclose(creatfile);
    read_in=fopen("used_percent.txt","r" );
    while (!feof(read_in)){
                          fscanf(read_in,"%s %d",&(type1.disk),&(type1.percent));
                          printf("%s %d\n",type1.disk,type1.percent);
                          }
  {printf("请输入要监控目录的目录名:");
    scanf("%s",destney);
    if(strcmp(destney,(*p).disk)==0)
  printf("%d",type1.percent);
  else
  printf("该目录不存在");
  }
    return 0;
   
}

论坛徽章:
0
2 [报告]
发表于 2009-02-27 16:00 |只看该作者
你把两个字符串都打印出来看看吧

论坛徽章:
0
3 [报告]
发表于 2009-02-27 23:16 |只看该作者
楼主把你的代码对齐先搞好吧
我已经看到里面有莫名其妙的大括号了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP