perl 讀寫配置檔案

2021-05-28 05:20:55 字數 3119 閱讀 9815

1.配置檔案格式形式如下

[newbin]

path = /data1/vshare/newbin

uid = root

gid = root

read only = no 

[col]

path = /data1/col

uid = root

gid = root

read only = no

2.讀配置函式

read_conf()

讀出為整個hash

$hash= 「/data1/vshare/newbin」

3.讀大分類函式

read_cat($cat)

讀出為$hash= 「/data1/vshare/newbin」

4.讀key函式

read_cat_key($cat,$key)

讀出為值

5.寫指定資料函式

write_cat_key($cat,$key,$value)

(1)讀出整個配置,替換此value,整個重寫回去

#! /usr/bin/perl

use rw_conf;

$cup = new rw_conf();

$cup->setfilename("my.conf");

# my %ret = $cup->read_conf();

# foreach my $cat( keys %ret)});

# print "[".$cat."]".$key."=".$value,"\n";

# }

# }

#my $cat="lady_high";

#my %ret = $cup->read_cat($cat);

# foreach my $key( keys %ret );

# print "[".$cat."]".$key."=".$value,"\n";

# }

#my $cat="lady_high";

#my $key="vcodec";

#my $value = $cup->read_cat_key($cat,$key);

#print "[".$cat."]".$key."=".$value,"\n";

my $cat="lady_high";

my $key="***xx";

$cup->write_cat_key($cat,$key,"yyyy");

my $cat="lady_high";

my $key="vfopts2";

$cup->write_cat_key($cat,$key,"yyyy");

package rw_conf;

use strict;

sub new ;

bless $this;

return $this;

}sub setfilename()

=shift;

}sub getfilename();}

#讀配置函式

#read_conf 返回 %hash

sub read_conf()

or die("can't not open ".$this->."\n");

my $cat="";

my $key="";

while (my $myline = )

elsif ($myline =~ /=/)

=$value;

#print "[".$cat."]|".$key."|".$value,"\n";}}

close fh;

return %ret;

}#讀大分類函式

#read_cat($cat)

#讀出為%hash

sub read_cat()

or die("can't not open ".$this->."\n");

my $cat="";

my $key="";

while (my $myline = )

elsif ($myline =~ /=/)

=$value;

#print "[".$cat."]|".$key."|".$value,"\n";}}

close fh;

return %};

}#讀key函式

#read_cat_key($cat,$key)

sub read_cat_key()

or die("can't not open ".$this->."\n");

my $cat="";

my $key="";

while (my $myline = )

elsif ($myline =~ /=/)

=$value;

#print "[".$cat."]|".$key."|".$value,"\n";}}

close fh;

return $ret;

}#寫指定資料函式

#write_cat_key($cat,$key)

#讀出整個配置,替換此value,整個重寫回去

sub write_cat_key()

or die("can't not open ".$this->."\n");

my $cat="";

my $key="";

while (my $myline = )

elsif ($myline =~ /=/)

=$value;

#print "[".$cat."]|".$key."|".$value,"\n";}}

close fh;

$ret=$write_value;

#重寫配置檔案

open det,">", $this-> || die ("can't not open ".$this->."\n");

foreach my $cat( keys %ret)});

print det $key."=".$value,"\n";

}

} close det; }

1;

讀寫配置檔案

windows作業系統專門為此提供了6個api函式來對配置設定檔案進行讀 寫 getprivateprofileint 從私有初始化檔案獲取整型數值 getprivateprofilestring 從私有初始化檔案獲取字串型值 getprofileint 從win.ini 獲取整數值 getprof...

讀寫配置檔案

procedure curstepchanged curstep tsetupstep varsetupname string inipath string begin case curstep of ssinstall 複製檔案前 begin end sspostinstall 完成複製 begi...

讀寫配置檔案

寫配置檔案 include include include include using std cout using std endl int main int nlen strlen d myinifile.ini 16 nlen strlen lppath 0 strcpy s lppath,s...