當前位置:首頁 » 編程語言 » c語言ini文件讀寫

c語言ini文件讀寫

發布時間: 2024-04-20 18:34:30

⑴ ini、配置文件、讀取鍵值,源代碼(C/C++),謝謝

1,c++ builder 用法:
#include <vcl.h>

TIniFile *GameIni = new TIniFile ("d:\\murong.ini");
AnsiString value;
value = GameIni->ReadString("Section2 Name", "KeyName2","");
delete GameIni;

2,VC
char value[20];
GetPrivateProfileString("Section2 Name","KeyName2",NULL,value,20,"d:\\murong.ini");

⑵ INI的文件讀寫

讀取 (_In_LPCTSTRlpAppName,_In_LPCTSTRlpKeyName,_In_LPCTSTRlpDefault,_Out_LPTSTRlpReturnedString,_In_DWORDnSize,_In_LPCTSTRlpFileName);參數說明:
lpAppName (輸入參數)節名
lpKeyName (輸入參數)鍵名
lpDefault (輸入參數)默認值。INI讀取失敗時,得到這個值
lpReturnedString (輸出參數)讀取到的鍵值
nSize (輸入參數)lpReturnedString的緩沖區大小
lpFileName (輸入參數)INI文件的文件路徑
返回值:
讀取到的字元數,不包括末尾的''字元或L''字元。
寫入 (_In_LPCTSTRlpAppName,_In_LPCTSTRlpKeyName,_In_LPCTSTRlpString,_In_LPCTSTRlpFileName);參數說明:
lpAppName (輸入參數)節名
lpKeyName (輸入參數)鍵名
lpString (輸入參數)需要寫入的鍵值
lpFileName (輸入參數)INI文件的文件路徑
返回值:
寫入成功返回非零值,寫入失敗返回零值,可以用GetLastError函數得到錯誤原因。 #!/usr/bin/envpython#-*-coding:UTF-8-*-importConfigParseriniparser=ConfigParser.ConfigParser()#初始化一個INI讀寫類的對象iniparser.read('filename.ini')#讀取INI文件key_value=iniparser.get('section_name_1','key_name')#解析INI文件printkey_value#生成INI文件iniparser.add_section('section_name_2')iniparser.set('section_name_2','key_name','key_value')withopen('filename.ini','w')asinifile: iniparser.write(inifile)#寫入INI文件

⑶ 濡備綍鍦–錛冧腑璇誨啓INI鏂囦歡

銆銆INI鏂囦歡灝辨槸鎵╁睍鍚嶄負 ini 鐨勬枃浠 鍦╓indows緋葷粺涓 INI鏂囦歡鏄寰堝 鏈閲嶈佺殑灝辨槸 System ini System ini 鍜 Win ini 璇ユ枃浠朵富瑕佸瓨鏀劇敤鎴鋒墍鍋氱殑閫夋嫨浠ュ強緋葷粺鐨勫悇縐嶅弬鏁 鐢ㄦ埛鍙浠ラ氳繃淇鏀笽NI鏂囦歡 鏉ユ敼鍙樺簲鐢ㄧ▼搴忓拰緋葷粺鐨勫緢澶氶厤緗 浣嗚嚜浠嶹indows 鐨勯鍑 鍦╓indows緋葷粺涓寮曞叆浜嗘敞鍐岃〃鐨勬傚康 INI鏂囦歡鍦╓indows緋葷粺鐨勫湴浣嶅氨寮濮嬩笉鏂涓嬫粦 榪欐槸鍥犱負娉ㄥ唽琛ㄧ殑鐙鐗逛紭鐐 浣垮簲鐢ㄧ▼搴忓拰緋葷粺閮芥妸璁稿氬弬鏁板拰鍒濆嬪寲淇℃伅鏀捐繘浜嗘敞鍐岃〃涓 浣嗗湪鏌愪簺鍦哄悎 INI鏂囦歡榪樻嫢鏈夊叾涓嶅彲鏇誇唬鐨勫湴浣 鏈鏂囧氨鏉ユ帰璁ㄤ竴涓婥錛冩槸濡備綍瀵笽NI榪涜岃誨啓鎿嶄綔 INI鏂囦歡鐨勭粨鏋 INI鏂囦歡鏄涓縐嶆寜鐓х壒鐐規柟寮忔帓鍒楃殑鏂囨湰鏂囦歡 姣忎竴涓狪NI鏂囦歡鏋勬垚閮介潪甯哥被浼 鐢辮嫢騫叉佃惤錛坰ection錛夌粍鎴 鍦ㄦ瘡涓甯︽嫭鍙風殑鏍囬樹笅闈 鏄鑻ュ共涓浠ュ崟涓鍗曡瘝寮澶寸殑鍏抽敭璇嶏紙keyword錛夊拰涓涓絳夊彿 絳夊彿鍙寵竟鐨勫氨鏄鍏抽敭瀛楀瑰簲鐨勫礆紙value錛 鍏朵竴鑸褰㈠紡濡備笅

銆銆[Section ] KeyWord = Valuel KeyWord = Value 銆鈥︹ [Section ] KeyWord = Value KeyWord = Value

銆銆C錛冨拰Win API鍑芥暟 C錛冨苟涓嶅儚C錛嬶紜 鎷ユ湁灞炰簬鑷宸辯殑綾誨簱 C錛冧嬌鐢ㄧ殑綾誨簱鏄 Net妗嗘灦涓烘墍鏈 Net紼嬪簭寮鍙戞彁渚涚殑涓涓鍏辨湁鐨勭被搴撯斺 Net FrameWork SDK 鉶界劧 Net FrameWork SDK鍐呭瑰嶮鍒嗗簽澶 鍔熻兘涔熼潪甯稿己澶 浣嗚繕涓嶈兘闈㈤潰淇卞埌 鑷沖皯瀹冨苟娌℃湁鎻愪緵鐩存帴鎿嶄綔INI鏂囦歡鎵闇瑕佺殑鐩稿叧鐨勭被 鍦ㄦ湰鏂囦腑 C錛冩搷浣淚NI鏂囦歡浣跨敤鐨勬槸Windows緋葷粺鑷甯Win 鐨凙PI鍑芥暟鈥斺擶ritePrivateProfileString錛堬級鍜孏etPrivateProfileString錛堬級鍑芥暟 榪欎簩涓鍑芥暟閮戒綅浜 kernel dll 鏂囦歡涓 鎴戜滑鐭ラ亾鍦–錛冧腑浣跨敤鐨勭被搴撻兘鏄鎵樼′唬鐮侊紙Managed Code錛夋枃浠 鑰學in 鐨凙PI鍑芥暟鎵澶勭殑鏂囦歡 閮芥槸闈炴墭綆′唬鐮侊紙Unmanaged Code錛夋枃浠 榪欏氨瀵艱嚧浜嗗湪C錛冧腑涓嶅彲鑳界洿鎺ヤ嬌鐢ㄨ繖浜涢潪鎵樼′唬鐮佹枃浠朵腑鐨勫嚱鏁 濂藉湪 Net妗嗘灦涓轟簡淇濇寔瀵逛笅鐨勫吋瀹 涔熶負浜嗗厖鍒嗗埄鐢ㄤ互鍓嶇殑璧勬簮 鎻愬嚭浜嗕簰鎿嶄綔 閫氳繃浜掓搷浣滃彲浠ュ疄鐜板筗in 鐨凙PI鍑芥暟鐨勮皟鐢 浜掓搷浣滀笉浠呴傜敤浜嶹in 鐨凙PI鍑芥暟 榪樺彲浠ョ敤鏉ヨ塊棶鎵樼$殑瀵硅薄 C錛冧腑瀵筗in 鐨凙PI鍑芥暟鐨勪簰鎿嶄綔鏄閫氳繃鍛藉悕絀洪棿 System Runtime InteropServices 涓鐨 DllImport 鐗瑰緛綾繪潵瀹炵幇鐨 瀹冪殑涓昏佷綔鐢ㄦ槸鎸囩ず姝ゅ睘鎬у寲鏂規硶鏄浣滀負闈炴墭綆DLL鐨勮緭鍑哄疄鐜扮殑 涓嬮潰浠g爜灝辨槸鍦–錛冨埄鐢ㄥ懡鍚嶇┖闂 System Runtime InteropServices 涓鐨 DllImport 鐗瑰緛綾葷敵鏄庝笂闈浜屼釜Win 鐨凙PI鍑芥暟 C錛冪敵鏄嶪NI鏂囦歡鐨勫啓鎿嶄綔鍑芥暟WritePrivateProfileString錛堬級 [ DllImport ( kernel ) ] private static extern long WritePrivateProfileString ( string section string key string val string filePath ) ; 鍙傛暟璇存槑 section INI鏂囦歡涓鐨勬佃惤 key INI鏂囦歡涓鐨勫叧閿瀛 val INI鏂囦歡涓鍏抽敭瀛楃殑鏁板 filePath INI鏂囦歡鐨勫畬鏁寸殑璺寰勫拰鍚嶇О C錛冪敵鏄嶪NI鏂囦歡鐨勮繪搷浣滃嚱鏁癎etPrivateProfileString錛堬級 [ DllImport ( kernel ) ] private static extern int GetPrivateProfileString ( string section string key string def StringBuilder retVal int size string filePath ) ; 鍙傛暟璇存槑 section INI鏂囦歡涓鐨勬佃惤鍚嶇О key INI鏂囦歡涓鐨勫叧閿瀛 def 鏃犳硶璇誨彇鏃跺欐椂鍊欑殑緙虹渷鏁板 retVal 璇誨彇鏁板 size 鏁板肩殑澶у皬 filePath INI鏂囦歡鐨勫畬鏁磋礬寰勫拰鍚嶇О 涓嬮潰鏄涓涓璇誨啓INI鏂囦歡鐨勭被

銆銆public class INIClass { public string inipath; [DllImport( kernel )] private static extern long WritePrivateProfileString(string section string key string val string filePath); [DllImport( kernel )] private static extern int GetPrivateProfileString(string section string key string def StringBuilder retVal int size string filePath); ///

/// 鏋勯犳柟娉 /// ///

銆銆鏂囦歡璺寰

public INIClass(string INIPath) { inipath = INIPath; } /// /// 鍐欏叆INI鏂囦歡 /// ///

銆銆欏圭洰鍚嶇О(濡 [TypeName] )

///

銆銆閿

///

銆銆鍊

public void IniWriteValue(string Section string Key string Value) { WritePrivateProfileString(Section Key Value this inipath); } /// /// 璇誨嚭INI鏂囦歡 /// ///

銆銆欏圭洰鍚嶇О(濡 [TypeName] )

///

銆銆閿

public string IniReadValue(string Section string Key) { StringBuilder temp = new StringBuilder( ); int i = GetPrivateProfileString(Section Key temp this inipath); return temp ToString(); } /// /// 楠岃瘉鏂囦歡鏄鍚﹀瓨鍦 /// /// 甯冨皵鍊 public bool ExistINIFile() { return File Exists(inipath); } }

銆銆C錛冨笽NI鏂囦歡榪涜屽啓鎿嶄綔 銆銆瀵笽NI鏂囦歡榪涜屽啓鎿嶄綔 鏄閫氳繃緇勪歡button 鐨 Click 浜嬩歡鏉ュ疄鐜扮殑 榪欓噷鏈変竴鐐瑰簲璇ユ敞鎰 褰撳湪璋冪敤WritePrivateProfileString錛堬級瀵笽NI鏂囦歡榪涜屽啓鎿嶄綔鐨勬椂鍊 濡傛灉姝ゆ椂鍦↖NI鏂囦歡涓瀛樺湪鍜岃佸啓鍏ョ殑淇℃伅鐩稿悓鐨勬佃惤鍚嶇О鍜屽叧閿瀛 鍒欏皢瑕嗙洊姝INI淇℃伅 涓嬮潰鏄痓utton 緇勪歡鐨 Click 浜嬩歡瀵瑰簲鐨勪唬鐮佹竻鍗

銆銆private void button _Click ( object sender System EventArgs e )

銆銆{

銆銆string FileName = textBox Text ;

銆銆string section = textBox Text ;

銆銆string key = textBox Text ;

銆銆string keyValue = textBox Text ;

銆銆WritePrivateProfileString ( section key keyValue FileName ) ;

銆銆MessageBox Show ( 鎴愬姛鍐欏叆INI鏂囦歡錛 淇℃伅 ) ;

銆銆}

銆銆C錛冨笽NI鏂囦歡榪涜岃繪搷浣 銆銆姝g『璇誨彇INI鐨勫繀欏繪弧瓚充笁涓鍓嶆彁 INI鏂囦歡鐨勫叏璺寰 孌佃惤鍚嶇О鍜屽叧閿瀛楀悕縐 鍚﹀垯灝辨棤娉曟g『璇誨彇 浣犲彲浠ヨ懼畾璇誨彇涓嶆垚鍔熷悗鐨勭己鐪佹暟鍊 鍦ㄤ笅闈㈢殑紼嬪簭涓 涓轟簡鐩磋傝懼畾鐨勬槸 鏃犳硶璇誨彇瀵瑰簲鏁板礆紒 瀛楃︿覆 璇誨彇INI鏂囦歡鏄閫氳繃button 緇勪歡鐨 Click 浜嬩歡鏉ュ疄鐜扮殑 涓嬮潰鏄鍏跺瑰簲鐨勪唬鐮佹竻鍗

銆銆private void button _Click ( object sender System EventArgs e )

銆銆{

銆銆StringBuilder temp = new StringBuilder ( ) ;

銆銆string FileName = textBox Text ;

銆銆string section = textBox Text ;

銆銆string key = textBox Text ;

銆銆int i = GetPrivateProfileString ( section key 鏃犳硶璇誨彇瀵瑰簲鏁板礆紒

銆銆temp FileName ) ;

銆銆//鏄劇ず璇誨彇鐨勬暟鍊

銆銆textBox Text = temp ToString ( ) ;

銆銆}

銆銆

銆銆using System ;

銆銆using System Drawing ;

銆銆using System Collections ;

銆銆using System ComponentModel ;

銆銆using System Windows Forms ;

銆銆using System Data ;

銆銆using System Runtime InteropServices ;

銆銆using System Text ;

銆銆namespace C_鎿嶄綔INI鏂囦歡__鍐欐搷浣

銆銆{

銆銆public class Form : System Windows Forms Form

銆銆{

銆銆private System Windows Forms Button button ;

銆銆private System Windows Forms TextBox textBox ;

銆銆private System Windows Forms Button button ;

銆銆private System Windows Forms TextBox textBox ;

銆銆private System Windows Forms TextBox textBox ;

銆銆private System Windows Forms TextBox textBox ;

銆銆private System Windows Forms Label label ;

銆銆private System Windows Forms Label label ;

銆銆private System Windows Forms Label label ;

銆銆private System Windows Forms Button button ;

銆銆private System Windows Forms OpenFileDialog openFileDialog ;

銆銆private System ComponentModel Container ponents = null ;

銆銆public Form ( )

銆銆{

銆銆InitializeComponent ( ) ;

銆銆}

銆銆protected override void Dispose ( bool disposing )

銆銆{

銆銆if ( disposing )

銆銆{

銆銆if ( ponents != null )

銆銆{

銆銆ponents Dispose ( ) ;

銆銆}

銆銆}

銆銆base Dispose ( disposing ) ;

銆銆}

銆銆[ DllImport ( kernel ) ]

銆銆private static extern long WritePrivateProfileString ( string

銆銆section

銆銆string key string val string filePath ) ;

銆銆[ DllImport ( kernel ) ]

銆銆private static extern int GetPrivateProfileString ( string section

銆銆string key string def StringBuilder retVal

銆銆int size string filePath ) ;

銆銆private void InitializeComponent ( )

銆銆{

銆銆this button = new System Windows Forms Button ( ) ;

銆銆this textBox = new System Windows Forms TextBox ( ) ;

銆銆this button = new System Windows Forms Button ( ) ;

銆銆this textBox = new System Windows Forms TextBox ( ) ;

銆銆this textBox = new System Windows Forms TextBox ( ) ;

銆銆this textBox = new System Windows Forms TextBox ( ) ;

銆銆this label = new System Windows Forms Label ( ) ;

銆銆this label = new System Windows Forms Label ( ) ;

銆銆this label = new System Windows Forms Label ( ) ;

銆銆this button = new System Windows Forms Button ( ) ;

銆銆this openFileDialog = new

銆銆System Windows Forms OpenFileDialog ( ) ;

銆銆this SuspendLayout ( ) ;

銆銆this button FlatStyle = System Windows Forms FlatStyle Flat ;

銆銆this button Location = new System Drawing Point ( ) ;

銆銆this button Name = button ;

銆銆this button Size = new System Drawing Size ( ) ;

銆銆this button TabIndex = ;

銆銆this button Text = 閫夋嫨INI鏂囦歡 ;

銆銆this button Click += new System EventHandler ( this button _Click ) ;

銆銆this textBox Location = new System Drawing Point ( ) ;

銆銆this textBox Name = textBox ;

銆銆this textBox Size = new System Drawing Size ( ) ;

銆銆this textBox TabIndex = ;

銆銆this textBox Text = ;

銆銆this button FlatStyle = System Windows Forms FlatStyle Flat ;

銆銆this button Location = new System Drawing Point ( ) ;

銆銆this button Name = button ;

銆銆this button Size = new System Drawing Size ( ) ;

銆銆this button TabIndex = ;

銆銆this button Text = 鍐欏叆INI鏂囦歡 ;

銆銆this button Click += new System EventHandler ( this button _Click ) ;

銆銆this textBox Location = new System Drawing Point ( ) ;

銆銆this textBox Name = textBox ;

銆銆this textBox Size = new System Drawing Size ( ) ;

銆銆this textBox TabIndex = ;

銆銆this textBox Text = ;

銆銆this textBox Location = new System Drawing Point ( ) ;

銆銆this textBox Name = textBox ;

銆銆this textBox Size = new System Drawing Size ( ) ;

銆銆this textBox TabIndex = ;

銆銆this textBox Text = ;

銆銆this textBox Location = new System Drawing Point ( ) ;

銆銆this textBox Name = textBox ;

銆銆this textBox Size = new System Drawing Size ( ) ;

銆銆this textBox TabIndex = ;

銆銆this textBox Text = ;

銆銆this label Location = new System Drawing Point ( ) ;

銆銆this label Name = label ;

銆銆this label TabIndex = ;

銆銆this label Text = 孌佃惤鍚嶇О ;

銆銆this label Location = new System Drawing Point ( ) ;

銆銆this label Name = label ;

銆銆this label TabIndex = ;

銆銆this label Text = 鍏抽敭瀛 ;

銆銆this label Location = new System Drawing Point ( ) ;

銆銆this label Name = label ;

銆銆this label TabIndex = ;

銆銆this label Text = 鍏抽敭瀛楁暟鍊 ;

銆銆this button FlatStyle = System Windows Forms FlatStyle Flat ;

銆銆this button Location = new System Drawing Point ( ) ;

銆銆this button Name = button ;

銆銆this button Size = new System Drawing Size ( ) ;

銆銆this button TabIndex = ;

銆銆this button Text = 璇誨彇INI鏁板 ;

銆銆this button Click += new System EventHandler ( this button _Click ) ;

銆銆this openFileDialog Filter = INI 鏂囦歡|* ini ;

銆銆this AutoScaleBaseSize = new System Drawing Size ( ) ;

銆銆this ClientSize = new System Drawing Size ( ) ;

銆銆this Controls AddRange ( new System Windows Forms Control [ ] {

銆銆this button

銆銆this textBox

銆銆this textBox

銆銆this textBox

銆銆this button

銆銆this textBox

銆銆this button

銆銆this label

銆銆this label

銆銆this label } ) ;

銆銆this MaximizeBox = false ;

銆銆this Name = Form ;

銆銆this Text = C錛冩搷浣淚NI鏂囦歡 鍐欐搷浣 ;

銆銆this ResumeLayout ( false ) ;

銆銆}

銆銆[STAThread]

銆銆static void Main ( )

銆銆{

銆銆Application Run ( new Form ( ) ) ;

銆銆}

銆銆private void button _Click ( object sender System EventArgs e )

銆銆{

銆銆openFileDialog ShowDialog ( ) ;

銆銆textBox Text = openFileDialog FileName ;

銆銆}

銆銆//鍐欏叆INI鏂囦歡

銆銆private void button _Click ( object sender System EventArgs e )

銆銆{

銆銆string FileName = textBox Text ;

銆銆string section = textBox Text ;

銆銆string key = textBox Text ;

銆銆string keyValue = textBox Text ;

銆銆WritePrivateProfileString ( section key keyValue FileName ) ;

銆銆MessageBox Show ( 鎴愬姛鍐欏叆INI鏂囦歡錛 淇℃伅 ) ;

銆銆}

銆銆//璇誨彇鎸囧畾INI鏂囦歡鐨勭壒瀹氭佃惤涓鐨勫叧閿瀛楃殑鏁板

銆銆private void button _Click ( object sender System EventArgs e )

銆銆{

銆銆this Controls AddRange ( new System Windows Forms Control [ ] {

銆銆this button

銆銆this textBox

銆銆this textBox

銆銆this textBox

銆銆this button

銆銆this textBox

銆銆this button

銆銆this label

銆銆this label

銆銆this label } ) ;

銆銆this MaximizeBox = false ;

銆銆this Name = Form ;

銆銆this Text = C錛冩搷浣淚NI鏂囦歡 鍐欐搷浣 ;

銆銆this ResumeLayout ( false ) ;

銆銆}

銆銆[STAThread]

銆銆static void Main ( )

銆銆{

銆銆Application Run ( new Form ( ) ) ;

銆銆}

銆銆private void button _Click ( object sender System EventArgs e )

銆銆{

銆銆openFileDialog ShowDialog ( ) ;

銆銆textBox Text = openFileDialog FileName ;

銆銆}

銆銆//鍐欏叆INI鏂囦歡

銆銆private void button _Click ( object sender System EventArgs e )

銆銆{

銆銆string FileName = textBox Text ;

銆銆string section = textBox Text ;

銆銆string key = textBox Text ;

銆銆string keyValue = textBox Text ;

銆銆WritePrivateProfileString ( section key keyValue FileName ) ;

銆銆MessageBox Show ( 鎴愬姛鍐欏叆INI鏂囦歡錛 淇℃伅 ) ;

銆銆}

銆銆//璇誨彇鎸囧畾INI鏂囦歡鐨勭壒瀹氭佃惤涓鐨勫叧閿瀛楃殑鏁板

銆銆private void button _Click ( object sender System EventArgs e )

銆銆{

銆銆StringBuilder temp = new StringBuilder ( ) ;

銆銆string FileName = textBox Text ;

銆銆string section = textBox Text ;

銆銆string key = textBox Text ;

銆銆int i = GetPrivateProfileString ( section key

銆銆 鏃犳硶璇誨彇瀵瑰簲鏁板礆紒 emp FileName ) ;

銆銆//鏄劇ず璇誨彇鐨勬暟鍊

銆銆textBox Text = temp ToString ( ) ;

銆銆}

銆銆}

銆銆}

銆銆

銆銆鎬葷粨

閫氳繃涓婇潰鐨勮繖浜涗粙緇 鍙浠ョ湅鎴怌錛冩搷浣淚NI鏂囦歡鐨勮繃紼 鍏跺疄灝辨槸C錛冭皟鐢╓in 鐨凙PI鍑芥暟鐨勮繃紼 鎺屾彙浜嗗備綍鍦–錛冪敵鏄嶹in 鐨凙PI鍑芥暟 鍐嶆潵鎿嶄綔INI灝辨樉寰楅潪甯哥畝鍗

lishixin/Article/program/ASP/201311/21793

熱點內容
腳本找書 發布:2024-05-03 12:02:17 瀏覽:493
境外伺服器租用怎麼辦 發布:2024-05-03 11:45:34 瀏覽:943
我的世界伺服器怎麼設置到重生點 發布:2024-05-03 11:33:04 瀏覽:752
mysqllinux安裝包下載 發布:2024-05-03 11:32:07 瀏覽:934
以太演算法的幣 發布:2024-05-03 11:30:45 瀏覽:383
編譯nfs 發布:2024-05-03 11:30:40 瀏覽:432
srs搭建直播伺服器 發布:2024-05-03 11:29:53 瀏覽:133
南通哪裡有賣密碼鎖的 發布:2024-05-03 11:22:01 瀏覽:522
除法指演算法 發布:2024-05-03 11:21:23 瀏覽:844
h265壓縮 發布:2024-05-03 11:20:27 瀏覽:490