C言語のstrstr

C言語で文字列内の文字列検索時に使用する。

 

【書式】
#include <string.h>
char *strstr(const char *s1, const char *s2);

 

戻り値は*ポインタなので、

今回はchar *lookで待ち構え。

 

#include <stdio.h>
#include <string.h>

#define BUF 256


int main(void){

FILE *rfp;
char *filename = "/proc/meminfo";
/* char filename[128] = "/proc/meminfo"; */
char *look;
char *total = "MemTotal";
char *used = "MemFree";
char *buffers = "Buffes";
char *cached = "Cached";
char buf[BUF];

if ( (rfp = fopen(filename,"r")) == NULL ) {

fprintf(stderr,"can not open file");
}

while( fgets(buf,BUF,rfp) ) {


if *1 != NULL ){


printf("%s",buf);
}
}

printf(" total used free shared buffers cached \n");
printf ("-/+ buffes/cache:\n");
printf("Swap:\n");
}
[root@gochamaze C]#

*1:look=strstr(buf,total