分類:
c++linux系統程式設計
c 2010-03-11 09:52 368人閱讀
收藏舉報
一.配置檔案樣本:
[c-sharp]view plain
copy
# user.conf
# username age *** country
tom 20 male us
chen 22 female cn
二.使用shell的awk語言進行解析讀取
例如:獲取 tom 的性別則可以使用這樣的語句:
[c-sharp]view plain
copy
cat user.conf | awk '/^tom[[:blank:]]+/ '
三.在c語言中,我們如何將使用awk得到的結果交給乙個變數呢?
一般就我而言採用兩種方式,一種則是將得到的結果寫入乙個temp檔案,然後直接讀取到某個buffer中;另一種則是通過管道的方式寫進去,再讀出來,如
講到的例子一樣。
[c-sharp]view plain
copy
#include
#include
#include
static int my_system(const char* pcmd, char* presult, int size);
int main(void)
...'", result, 1025);
printf("the result is: %s ", result);
return 0;
}
static int my_system(const char* pcmd, char* presult, int size)
...
if((pid = fork()) == 0)
...
close(1);
dup2(fd2[1],1);
close(fd[0]);
close(fd2[1]);
system(pcmd);
read(fd2[0], presult, maxlen);
presult[strlen(presult)-1] = 0;
write(fd[1], presult, strlen(presult));
close(fd2[0]);
exit(0);
} // parent process
close(fd[1]);
p = presult;
left = maxlen;
while((count = read(fd[0], p, left))) ...
close(fd[0]);
return 0;
}
執行結果:
[c-sharp]view plain
copy
[denny@localhost testforc]$ ./a.out
the result is: male
讀取配置檔案的方式
業務場景 有時候寫好一段 裡面有一些引數有可能在日後有變動的可能,如何不變動 而改變引數的內容呢?那就配置到配置檔案裡面吧 比如我的properties或者我的yml配置檔案中有個屬性是hi.pram aaa,想獲取hi.pram屬性的值 配置檔案 hi pram 引數值方式一 引用物件進行獲取en...
Java 讀取配置檔案的方式
通過乙個propertiesutil類,在tomcat初始化時,對properties物件props進行初始化 指向對應的配置檔案filename mmall public class propertiesutil catch ioexception e public static string g...
讀取配置檔案
類載入器 1 直接用class.forname 類的路徑名 class.forname com.hsf.classloader.classloader 2 用classloder 類載入器 inputstream in classloader.getsystemresourceasstream aa...