#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PATH "/home/zlj/zlj/z.conf"
#define OBJ "age"
int main(int argc, char *argv[])
{
FILE *fp = NULL;
int flag = 0;
char buf[32], str[8], date[8];
memset(buf, 0, 32);
memset(str, 0, 8);
memset(date, 0, 8);
// if(2 != argc){
// printf("usage : io xxx\n");
// exit -1;
// }
if(NULL == (fp = fopen(PATH, "w+")))
printf("fopen error\n");
if(fprintf(fp, "%s=%s\n%s=%d\n%s=%d\n", "name", "zlj", "age", 24, "class", 1002) < 0)
printf("fprintf error\n");
// system("cat z.conf");
// fflush(fp);
if(0 != fseek(fp, 0, SEEK_SET))
printf("fseek error\n");
while(NULL != fgets(buf, 32, fp)){
sscanf(buf, "%[^=]=%s", str, date);
// printf("%s\n%s\n", str, date);
if(0 == strcmp(str, OBJ)){
printf("The value of %s is : %s\n", str, date);
flag = 1;
}
}
if(1 != flag)
printf("Can't find the value of %s!\n", OBJ);
return 0;
#include <stdlib.h>
#include <string.h>
#define PATH "/home/zlj/zlj/z.conf"
#define OBJ "age"
int main(int argc, char *argv[])
{
FILE *fp = NULL;
int flag = 0;
char buf[32], str[8], date[8];
memset(buf, 0, 32);
memset(str, 0, 8);
memset(date, 0, 8);
// if(2 != argc){
// printf("usage : io xxx\n");
// exit -1;
// }
if(NULL == (fp = fopen(PATH, "w+")))
printf("fopen error\n");
if(fprintf(fp, "%s=%s\n%s=%d\n%s=%d\n", "name", "zlj", "age", 24, "class", 1002) < 0)
printf("fprintf error\n");
// system("cat z.conf");
// fflush(fp);
if(0 != fseek(fp, 0, SEEK_SET))
printf("fseek error\n");
while(NULL != fgets(buf, 32, fp)){
sscanf(buf, "%[^=]=%s", str, date);
// printf("%s\n%s\n", str, date);
if(0 == strcmp(str, OBJ)){
printf("The value of %s is : %s\n", str, date);
flag = 1;
}
}
if(1 != flag)
printf("Can't find the value of %s!\n", OBJ);
return 0;
}
PS:一个自己写的linux下读写配置文件的程序,主要练习一下fprintf()、fscanf()、sprintf()、sscanf()、fgets()等函数的使用