当前位置:首页 » 编程语言 » 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-11-28 06:29:52 浏览:870
oraclesql加密 发布:2024-11-28 06:29:15 浏览:900
图片缓存工具 发布:2024-11-28 06:29:15 浏览:962
有文件夹删不掉怎么办 发布:2024-11-28 06:22:50 浏览:526
python3怎么用 发布:2024-11-28 06:10:58 浏览:255
安卓盒子内存小怎么办 发布:2024-11-28 06:10:57 浏览:809
车企减配哪些配置好 发布:2024-11-28 05:55:18 浏览:674
传说对决上传照片 发布:2024-11-28 05:49:18 浏览:465
解压笔怎么画 发布:2024-11-28 05:49:17 浏览:518
预编译查询 发布:2024-11-28 05:32:35 浏览:313