当前位置: 技术问答>linux和unix
求教,结构体中字符串的比较
来源: 互联网 发布时间:2016-05-20
本文导语: 有一个结构体 struct type {char disk[100]; int percent; }type1; char disk[100]里面是:/dev/cciss/c0d0p1 /dev/cciss/c0d0p2 none /dev/cciss...
有一个结构体
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
#include
#include
#include
#include
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,'', 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 %dn",type1.disk,type1.percent);
}
{printf("请输入要监控目录的目录名:");
scanf("%s",destney);
if(strcmp(destney,(*p).disk)==0)
printf("%d",type1.percent);
else
printf("该目录不存在");
}
return 0;
}
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
#include
#include
#include
#include
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,'', 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 %dn",type1.disk,type1.percent);
}
{printf("请输入要监控目录的目录名:");
scanf("%s",destney);
if(strcmp(destney,(*p).disk)==0)
printf("%d",type1.percent);
else
printf("该目录不存在");
}
return 0;
}
|
代码有问题,标识了一下,你帮我解释下。
#include
#include
#include
#include
#include
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,'', 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 %dn",type1.disk,type1.percent);
}
{printf("请输入要监控目录的目录名:"); //这里有个'{'符号?没有循环体来重复strcmp?
scanf("%s",destney);
if(strcmp(destney,(*p).disk)==0) //才比较一次?
printf("%d",type1.percent);
else
printf("该目录不存在");
}
return 0;
}