⑴ c語言讀寫配置文件
#include <stdio.h>
#include <string.h>
#define MAX_BUF 20
#define SERVER "localhost"
#define CONFIG_FILE "1.conf"
bool SetAuthServer(char* strServerAdd)
{
char buf[MAX_BUF], tempBuf[MAX_BUF];
memset(buf, 0, MAX_BUF);
memset(tempBuf, 0, MAX_BUF);
FILE *pF = fopen(CONFIG_FILE, "r");
if(!pF)
{
printf("打開文件失敗!\n");
return false;
}
fread(buf, MAX_BUF, 1, pF);
if(!feof(pF))
{
printf("讀取不完整,請把MAX_BUF設置為大一點, 當前大小為: %d\n", MAX_BUF);
fclose(pF);
return false;
}
fclose(pF);
char *lpPos = buf;
char *lpNewPos = buf;
while(lpNewPos = strstr(lpPos, SERVER))
{
strncpy(tempBuf+strlen(tempBuf), lpPos, lpNewPos-lpPos);
strcat(tempBuf, strServerAdd);
lpPos = lpNewPos + strlen(SERVER);
}
strcat(tempBuf, lpPos);
pF = fopen(CONFIG_FILE, "w");
if(!pF)
{
printf("打開文件失敗!\n");
return false;
}
fwrite(tempBuf, strlen(tempBuf), 1, pF);
fclose(pF);
return true;
}
void main()
{
char buf[20];
printf("請輸入一個字元串來修改伺服器配置: ");
scanf("%s", buf);
if(SetAuthServer(buf) == true)
printf("修改成功!\n");
else
printf("修改失敗!\n");
}
⑵ C怎樣讀取配置文件並設成環境變數
右鍵單機「我的電腦」選擇「屬性」,在出來的新界面中選擇左邊的菜單「高級系統設置」,會彈出一個框框,框框裡面的最下面就有環境變數的編輯,你只要將你的環境變數丟裡面去就行了。
⑶ C#如何讀取config配置文件數據
代碼如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Text.RegularExpressions;
usingSystem.Configuration;
usingSystem.ServiceModel;
usingSystem.ServiceModel.Configuration;
namespaceNetUtilityLib
{
publicstaticclassConfigHelper
{
//依據連接串名字connectionName返回數據連接字元串
(stringconnectionName)
{
//指定config文件讀取
stringfile=System.Windows.Forms.Application.ExecutablePath;
System.Configuration.Configurationconfig=ConfigurationManager.OpenExeConfiguration(file);
stringconnectionString=
config.ConnectionStrings.ConnectionStrings[connectionName].ConnectionString.ToString();
returnconnectionString;
}
///<summary>
///更新連接字元串
///</summary>
///<paramname="newName">連接字元串名稱</param>
///<paramname="newConString">連接字元串內容</param>
///<paramname="newProviderName">數據提供程序名稱</param>
(stringnewName,stringnewConString,stringnewProviderName)
{
//指定config文件讀取
stringfile=System.Windows.Forms.Application.ExecutablePath;
Configurationconfig=ConfigurationManager.OpenExeConfiguration(file);
boolexist=false;//記錄該連接串是否已經存在
//如果要更改的連接串已經存在
if(config.ConnectionStrings.ConnectionStrings[newName]!=null)
{
exist=true;
}
//如果連接串已存在,首先刪除它
if(exist)
{
config.ConnectionStrings.ConnectionStrings.Remove(newName);
}
//新建一個連接字元串實例
=
newConnectionStringSettings(newName,newConString,newProviderName);
//將新的連接串添加到配置文件中.
config.ConnectionStrings.ConnectionStrings.Add(mySettings);
//保存對配置文件所作的更改
config.Save(ConfigurationSaveMode.Modified);
//強制重新載入配置文件的ConnectionStrings配置節
ConfigurationManager.RefreshSection("ConnectionStrings");
}
///<summary>
///返回*.exe.config文件中appSettings配置節的value項
///</summary>
///<paramname="strKey"></param>
///<returns></returns>
(stringstrKey)
{
stringfile=System.Windows.Forms.Application.ExecutablePath;
Configurationconfig=ConfigurationManager.OpenExeConfiguration(file);
foreach(stringkeyinconfig.AppSettings.Settings.AllKeys)
{
if(key==strKey)
{
returnconfig.AppSettings.Settings[strKey].Value.ToString();
}
}
returnnull;
}
///<summary>
///在*.exe.config文件中appSettings配置節增加一對鍵值對
///</summary>
///<paramname="newKey"></param>
///<paramname="newValue"></param>
(stringnewKey,stringnewValue)
{
stringfile=System.Windows.Forms.Application.ExecutablePath;
Configurationconfig=ConfigurationManager.OpenExeConfiguration(file);
boolexist=false;
foreach(stringkeyinconfig.AppSettings.Settings.AllKeys)
{
if(key==newKey)
{
exist=true;
}
}
if(exist)
{
config.AppSettings.Settings.Remove(newKey);
}
config.AppSettings.Settings.Add(newKey,newValue);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
//修改system.serviceModel下所有服務終結點的IP地址
(stringconfigPath,stringserverIP)
{
Configurationconfig=ConfigurationManager.OpenExeConfiguration(configPath);
ConfigurationSectionGroupsec=config.SectionGroups["system.serviceModel"];
=secasServiceModelSectionGroup;
ClientSectionclientSection=serviceModelSectionGroup.Client;
foreach(.Endpoints)
{
stringpattern=@"d{1,3}.d{1,3}.d{1,3}.d{1,3}";
stringaddress=item.Address.ToString();
stringreplacement=string.Format("{0}",serverIP);
address=Regex.Replace(address,pattern,replacement);
item.Address=newUri(address);
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("system.serviceModel");
}
//修改applicationSettings中App.Properties.Settings中服務的IP地址
publicstaticvoidUpdateConfig(stringconfigPath,stringserverIP)
{
Configurationconfig=ConfigurationManager.OpenExeConfiguration(configPath);
ConfigurationSectionGroupsec=config.SectionGroups["applicationSettings"];
=sec.Sections["DataService.Properties.Settings"];
=;
if(clientSettingsSection!=null)
{
SettingElementelement1=clientSettingsSection.Settings.Get("DataService_SystemManagerWS_SystemManagerWS");
if(element1!=null)
{
clientSettingsSection.Settings.Remove(element1);
stringoldValue=element1.Value.ValueXml.InnerXml;
element1.Value.ValueXml.InnerXml=GetNewIP(oldValue,serverIP);
clientSettingsSection.Settings.Add(element1);
}
SettingElementelement2=clientSettingsSection.Settings.Get("DataService_EquipManagerWS_EquipManagerWS");
if(element2!=null)
{
clientSettingsSection.Settings.Remove(element2);
stringoldValue=element2.Value.ValueXml.InnerXml;
element2.Value.ValueXml.InnerXml=GetNewIP(oldValue,serverIP);
clientSettingsSection.Settings.Add(element2);
}
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("applicationSettings");
}
privatestaticstringGetNewIP(stringoldValue,stringserverIP)
{
stringpattern=@"d{1,3}.d{1,3}.d{1,3}.d{1,3}";
stringreplacement=string.Format("{0}",serverIP);
stringnewvalue=Regex.Replace(oldValue,pattern,replacement);
returnnewvalue;
}
}
}
⑷ C語言如何讀取文件中指定的某一段
使用fread函數讀取指定長度的字元串,即使包含\n也會被讀取,可以首先使用fseek定位到文件結尾,然後ftell函數返回的值就是文件的大小,這樣就可以用循環多次讀取文件,直到讀取所有內容
FILE *file = NULL;
char szFile[1025] = {0};
int nHadRead = 0;
file = fopen( "file.txt", "r+");
if ( file == NULL )
return;
fseek( file, 0, SEEK_END ); //定位到文件尾
int nLen = ftell( file ); //獲取當前位置,即文件長度
fseek( file 0, SEEK_SET ); //重新定位到文件開頭,准備開始讀
while ( nHadRead < nLen )
{
int nRead = nLen - nHadRead >1024 ? 1024 : nLen - nHadRead;//如果剩餘小於1024位元組,則讀剩餘位元組,否則每次讀取1024位元組。
int nTmp = fread( szFile, 1, nRead , file );
nHadRead += nTmp;
printf( "%s", szFile );
memset( szFile, 0x0, sizeof(szFile) );
}
fclose(file);
大致過程就是這樣,純手打,沒有調試過,可能有錯
⑸ 用C#如何讀寫配置文件
INI文件就是擴展名為"ini"的文件。x0dx0a其一般形式如下:x0dx0a[section1] // 配置節x0dx0a//鍵名 //鍵值x0dx0akeyword1 = valuelx0dx0akeyword2 = value2x0dx0a??x0dx0a[section2]x0dx0akeyword3 = value3x0dx0akeyword4 = value4x0dx0a在Windows系統中,INI文件是很多,最重要的就是"System.ini"、"System32.ini"和"Win.ini"。該文件主要存放用戶所做的選擇以及系統的各種參數。用戶可以通過修改INI文件,來改變應用程序和系統的很多配置。但自從Windows 95的退出,在Windows系統中引入了注冊表的概念,INI文件在Windows系統的地位就開始不斷下滑,這是因為注冊表的獨特優點,使應用程序和系統都把許多參數和初始化信息放進了注冊表中。以及XML文件的國際標准化給INI文件又一次打擊。x0dx0a但在某些場合,INI文件還擁有其不可替代的地位。比如綠色軟體的規定就是不向注冊表和系統中填入新東西。對於軟體需要儲存的信息就需要存入到文件中了。XML雖然兼容性比較好,但對於僅僅保存幾個自定義參數而言就顯得大材小用了。這是就可以選擇使用快速簡單的儲存方式:INI文件。x0dx0a本文就來探討一下C#是如何對INI進行讀寫操作。x0dx0a主要思路是調用Win32 API。x0dx0a1.引入命名空間x0dx0ausingSystem.Runtime.InteropServices;x0dx0a2.聲明(把一個Win32 API函數轉成C#函數)x0dx0a//聲明INI文件的寫操作函數 WritePrivateProfileString()x0dx0a[DllImport("kernel32")]x0dx0aprivate static extern longWritePrivateProfileString(string section, string key, string val, stringfilePath);x0dx0a//聲明INI文件的讀操作函數 GetPrivateProfileString()x0dx0a[DllImport("kernel32")]x0dx0aprivate static extern intGetPrivateProfileString(string section, string key, string def, StringBuilderretVal, int size, string filePath);x0dx0a3.函數x0dx0apublic void Writue(string section,string key, string value)x0dx0a{x0dx0a// section=配置節,key=鍵名,value=鍵值,path=路徑(section,key, value, sPath);x0dx0a}x0dx0apublic string ReadValue(stringsection, string key)x0dx0a{x0dx0a// 每次從ini中讀取多少位元組x0dx0aSystem.Text.StringBuilder temp =new System.Text.StringBuilder(255);x0dx0a// section=配置節,key=鍵名,temp=上面,path=路徑x0dx0aGetPrivateProfileString(section,key, "", temp, 255, sPath);x0dx0areturntemp.ToString(); //注意類型的轉換x0dx0a}x0dx0a到此基本功能已經實現了。下面我們將所有的代碼重新整合一下:x0dx0anamespace Library.Filex0dx0a{x0dx0apublic class Inix0dx0a{x0dx0a// 聲明INI文件的寫操作函數 WritePrivateProfileString()x0dx0a[System.Runtime.InteropServices.DllImport("kernel32")]x0dx0aprivate static extern longWritePrivateProfileString(string section, string key, string val, stringfilePath);x0dx0a// 聲明INI文件的讀操作函數 GetPrivateProfileString()x0dx0a[System.Runtime.InteropServices.DllImport("kernel32")]x0dx0aprivate static extern intGetPrivateProfileString(string section, string key, string def,System.Text.StringBuilder retVal, int size, string filePath);x0dx0aprivate string sPath = null;x0dx0apublic Ini(string path)x0dx0a{x0dx0athis.sPath = path;x0dx0a}x0dx0apublic void Writue(string section,string key, string value)x0dx0a{x0dx0a// section=配置節,key=鍵名,value=鍵值,path=路徑(section,key, value, sPath);x0dx0a}x0dx0apublic string ReadValue(stringsection, string key)x0dx0a{x0dx0a// 每次從ini中讀取多少位元組x0dx0aSystem.Text.StringBuilder temp =new System.Text.StringBuilder(255);x0dx0a// section=配置節,key=鍵名,temp=上面,path=路徑x0dx0aGetPrivateProfileString(section,key, "", temp, 255, sPath);x0dx0areturn temp.ToString();x0dx0a}x0dx0a}x0dx0a}x0dx0a開始調用函數。x0dx0a// 寫入inix0dx0aIni ini = newIni("C:/config.ini");x0dx0aini.Writue("Setting","key1", "HELLO WORLD!");x0dx0aini.Writue("Setting","key2", "HELLO CHINA!");x0dx0a// 讀取inix0dx0aIni ini = newIni("C:/config.ini");x0dx0astring str1 =ini.ReadValue("Setting", "key1");x0dx0aMessageBox.Show(str1);x0dx0a二,在一些小的應用中,有時候不需要使用數據困這樣大規模的數據管理工具,也很少進行數據的查詢、修改等操作,而僅用文件來存儲數據。這時就需要使用。net中的文件操作對象,如file、streamReader、streamWriter等。x0dx0a1,使用File對象操作文件x0dx0aSystem.IO.File類提供了一系類的靜態辦法,完成對晚間的常用操作,如新建、刪除、拷貝、移動等x0dx0a2,使用StreamWriter寫入文件x0dx0a在System.IO空間中定義了一個文件寫入器對象StreamWriter,使用它可以以一種特定的編碼向輸出流中(Stream)寫入字元。x0dx0a3,使用SteamReader讀取文件x0dx0a與streamWrite對應
⑹ 用C語言讀取一個文件中的內容,如何對不同的行進行解析,比如是配置文件
很簡單的
配置文件 微軟有抓們的一套解析函數
INI文件是Windows系統中一類比較重要的文件,通常用來存放系統或者應用程序的配置信息,以方便系統或者應用 程序在初始化時再次讀入。比如Windows系統中的配置文件win.ini和system.ini,它們就主要存放系統啟動或用戶登陸時的系統信息。這 項功能在方便了系統配置的同時,也為非法程序的自動運行提供了可乘之機。顯然,這類文件的重要性應該引起我們的重視。但是對於這樣的ini文件的讀寫操作 卻與普通文本文件有著種種的不同,尤其體現在編程實現上。筆者曾經嘗試用手動更改的方法在文件中加入一些項,使得自己的程序能夠在初始化時自動運行,但是 卻沒有成功,最後還是藉由編程的方法來實現了。這里主要涉及到一些API函數,而這些函數又往往不被人們所熟知,本文的任務就是在介紹這些函數的同時,用 簡單的程序作了示例,下面我們言歸正傳。
先來看幾個往配置文件中寫入信息的函數:
(1)WritePrivateProfileSection()用來在ini文件中直接向指定區域寫入鍵和值的信息,其原型如下:
BOOL WritePrivateProfileSection(
LPCTSTR lpAppName, // 指向指定欄位的字元串
LPCTSTR lpString, // 指向要寫入的鍵與值字元串
LPCTSTR lpFileName // 指向文件名稱字元串,如果不包含完整路徑,則在windows目錄下創建
);
用法示例:
WritePrivateProfileSection(_T(「windows」),_T(「load=c:\\winnt\\notepad.exe」),_T(「c:\\winnt\\win.ini」));
(2)WritePrivateProfileString()與上一個函數的不同點在於其將鍵和值分開了,原型如下:
BOOL WritePrivateProfileString(
LPCTSTR lpAppName, // 指向指定欄位的字元串
LPCTSTR lpKeyName, // 指向指定鍵的字元串
LPCTSTR lpString, // 指向指定值的字元串
LPCTSTR lpFileName // 指向文件名稱字元串
);
用法示例:
WritePrivateProfileString(_T(「windows」),_T(load」)_T(「c:\\winnt\\notepad.exe」),_T(「c:\\winnt\\win.ini」));
(3)WritePrivateProfileStruct()與前面兩個的不同在於文件尾有校驗和,原型如下:
BOOL WritePrivateProfileStruct(
LPCTSTR lpszSection, //指向指定欄位的字元串
LPCTSTR lpszKey, //指向指定鍵的字元串
LPVOID lpStruct, //指向存放要加入的數據的緩沖區,如果為NULL,則刪除鍵
UINT uSizeStruct, //緩沖區大小,以位元組為單位
LPCTSTR szFile //以零結尾的文件名稱字元串,如果為空,則向win.ini寫入
);
用法示例:
WritePrivateProfileStruct(_T(「windows」),_T(「load」),pBuffer,sizeof(pBuffer),_T(「c:\\winnt\\win.ini」));
(4)還有兩個函數,是專門用來向win.ini文件寫入的,函數原型如下:
BOOL WriteProfileSection(
LPCTSTR lpAppName, //指向指定欄位的字元串
LPCTSTR lpString //指向指定值的字元串
);
BOOL WriteProfileString(
LPCTSTR lpAppName, //指向指定欄位的字元串
LPCTSTR lpKeyName, //指向指定鍵的字元串
LPCTSTR lpString //指向指定值的字元串
);
下面來看幾個對應的從ini文件獲取信息的API函數,上面已經說得很詳細了,這里只說其中兩個:
DWORD GetPrivateProfileString(
LPCTSTR lpAppName, //指向指定欄位的字元串
LPCTSTR lpKeyName, //指向鍵的字元串
LPCTSTR lpDefault, //如果INI文件中沒有前兩個參數指定的欄位名或鍵名,則將此值賦給變數
LPTSTR lpReturnedString, //存放INI文件中值的目的緩存區
DWORD nSize, //目的緩沖區的大小,以位元組為單位
LPCTSTR lpFileName //指向INI文件名稱的字元串
);
UINT GetPrivateProfileInt(
LPCTSTR lpAppName, //指向指定欄位的字元串
LPCTSTR lpKeyName, //指向鍵的字元串
INT nDefault, //如果INI文件中沒有前兩個參數指定的欄位名或鍵名,則將此值賦給變數
LPCTSTR lpFileName //指向INI文件名稱的字元串
);
程序示例1: 我們在這里建立了一個應用程序「App Name」,並且使用了一個INI文件「appname.ini」,在此INI文件中,我們寫入如下內容:
[Section1]
FirstKey = It all worked out okay.
SecondKey = By golly, it works.
ThirdKey = Another test.
代碼分析如下:
#include <stdio.h>
#include <windows.h>
//主函數
main()
{
//定義局部
CHAR inBuf[80];
HKEY hKey1, hKey2;
DWORD dwDisposition;
LONG lRetCode;
// 試圖創建INI文件的鍵值
lRetCode = RegCreateKeyEx ( HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT
\\CurrentVersion\\IniFileMapping\\appname.ini",
0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE,
NULL, &hKey1,
&dwDisposition);
//判斷是否出錯
if (lRetCode != ERROR_SUCCESS){
printf ("Error in creating appname.ini key\n");
return (0) ;
}
//試圖設置一個節區的值
lRetCode = RegSetValueEx ( hKey1,
"Section1",
0,
REG_SZ,
"USR:App Name\\Section1",
20);
//判斷是否出錯
if (lRetCode != ERROR_SUCCESS) {
printf ( "Error in setting Section1 value\n");
return (0) ;
}
//試圖創建一個應用名稱鍵值
lRetCode = RegCreateKeyEx ( HKEY_CURRENT_USER,
"App Name",
0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE,
NULL, &hKey2,
&dwDisposition);
//判斷是否出錯
if (lRetCode != ERROR_SUCCESS) {
printf ("Error in creating App Name key\n");
return (0) ;
}
//強制系統重新讀取映射區的內容到共享內存中,以便於將來對應用程序的調用可//以找到它,而不需要重新啟動系統
WritePrivateProfileStringW( NULL, NULL, NULL, L"appname.ini" );
//向INI文件中添加一些鍵值
WritePrivateProfileString ("Section1", "FirstKey",
"It all worked out okay.", "appname.ini");
WritePrivateProfileString ("Section1", "SecondKey",
"By golly, it works.", "appname.ini");
WritePrivateProfileSection ("Section1", "ThirdKey = Another Test.",
"appname.ini");
//測試一下添加的正確性
GetPrivateProfileString ("Section1", "FirstKey",
"Bogus Value: Get didn't work", inBuf, 80,
"appname.ini");
printf ("%s", inBuf);
return(0);
}
程序示例2:通過修改win.ini中的欄位[windows]中的鍵load或run,或者是為system.ini中的欄位[boot]中的鍵 shell增加值,可以達到設置程序自動運行的目的。假設我們要自動運行notepad.exe,修改後的win.ini或system.ini文件象這 樣就可以:
win.ini
[windows]
load=c:\winnt\notepad.exe
run=c:\winnt\notepad.exe
system.ini
[boot]
shell=c:\winnt\explorer.exe c:\winnt\notepad.exe
注意:system.ini文件的修改要特別注意,如果你單純改成shell=c:\winnt\notepad.exe,則不能首先運行 explorer.exe,很明顯你將看不到桌面和任務欄,呵呵,筆者在做實驗時就曾因為粗心造成了這樣的後果,不過不用害怕,只要你用我們下面提供的程 序,將它修改過來就可以了,默認時,系統在system.ini中的[boot]下是shell=c:\winnt\explorer.exe。很多非法 程序就是通過修改這兩個文件來達到自啟動的目的的。
下面這個程序可以在附書光碟中找到,名稱為「AutoPlay」,使用VC++6.0寫成,核心程序源代碼如下:
void CAutoRunDlg::OnBrowse()
{
//只瀏覽exe文件
CfileDialog fileDlg(TRUE,_T("EXE"),_T("*.exe"),OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,(_T("Executable Files (*.exe) |*.exe ||")));//顯示打開文件的對話框
//當操作者選擇OK時,程序取得選擇文件的全路徑名(包括文件的路徑及文件名稱),並將相應的數值傳輸給相關的控制項變數。
if(fileDlg.DoModal()==IDOK)
{
m_strFileName=fileDlg.GetPathName();
//向將變數中的數值傳輸給控制項顯示出來。
UpdateData(FALSE);
}
}
void CAutoRunDlg::OnApply()
{
//更新數據
UpdateData(TRUE);
//寫入ini文件
LPCTSTR filename;
filename=m_strFileName;
WritePrivateProfileString(_T("windows"),_T("load"),filename,_T("c:\\winnt\\win.ini"));
}
您如果要更改system.ini,可以將WritePrivateProfileString(_T("windows"),_T("load"),filename,_T("c:\\winnt\\win.ini"));
改為 WritePrivateProfileString(_T("boot"),_T("shell"),filename,_T("c:\\winnt \\system.ini"));並且在輸入文件名時輸入c:\winnt\explorer.exe c:\winnt\notepad.exe。
寫到這里,本文的意圖基本達到,如果您可以把某些代碼親自實現,相信讀者會有比較大的收獲。
⑺ c中讀取配置文件一般是怎麼寫的
我是這么做的,比如 ini 里以 #注釋,以=表示賦值
#
# Note
#
aaa=bbb
我從ini里一行一行讀出來,如果第一回個字母是答#,就忽略
否則就從 line_of_file 里查找 「=」字元,(去掉行末'\n'換行符)
=之前的就是參數名,=之後的就是參數值(去掉空格,tab)(用strncpy)
⑻ C語言如何讀取指定路徑下的所有指定格式的文件
用C語言讀取目錄中的文件名的方法:
1、如果是在window環境下,可以用一下方法:
使用stdlib.h頭文件聲明的system()函數
_CRTIMP int __cdecl system (const char*);
system("dir c:\ /a:h /b > c:\dir.txt");
調用系統命令dir,把c:目錄下文件列表寫入文件dir.txt中
2、使用dirent.h頭文件中聲明的opendir(),readdir()函數;
intmain(intargc,char*argv[])
{
DIR*directory_pointer;
structdirent*entry;
if((directory_pointer=opendir("d:\XL"))==NULL)
printf("Erroropening ");
else
{
while((entry=readdir(directory_pointer))!=NULL)
{
printf("%s ",entry->d_name);
}
closedir(directory_pointer);
}
system("PAUSE");
return0;
}
3、如果沒有dirent.h,可以使用io.h頭文件中聲明的_findfirst(),_findnext()函數;
示例代碼:
intmain(intargc,char*argv[])
{
longfile;
struct_finddata_tfind;
_chdir("d:\");
if((file=_findfirst("*.*",&find))==-1L)
{
printf("空白! ");
exit(0);
}
printf("%s ",find.name);
while(_findnext(file,&find)==0)
{
printf("%s ",find.name);
}
_findclose(file);
system("PAUSE");
return0;
}
⑼ 如何在VC中實現配置文件(ini)的讀寫
配置文件在重要性不言而喻,在我們常用的軟體中經常可以看到它的身影,它提供了程序初始化過程中一些常用的參數,並且可以手動的修改這些參數,因此使用起來非常的方便。常見的配置文件為*.ini文件。 [小節名]關鍵字=值關鍵字=值……MFC為用戶讀取ini文件提供了幾個函數,其中常用的幾個函數分別如下: 讀取信息:GetPrivateProfileString和GetPrivateProfileInt 寫入信息:WritePrivateProfileString 運用這幾個函數就可以滿足常用的對字元串和整數的讀寫操作了。為了體現MFC的封裝性以及方便使用,我們可以定義一個介面,即一個純虛類。所有的方法都由這個介面繼承而來。我們將這個純虛類命名為CCfgFile,之後我們從這個純虛類中繼承一個類(CIniFile)用來實現對ini文件的讀取。以後若是需要一些更高級的方法可以再從CCfgFile繼承出其他的類來實現。 這樣我們就可以利用CIniFile類中定義的函數來操縱ini文件了。在程序中我們需要操作ini文件中一些常用的配置參數讀寫,我們可以定義一個參數類來實現,如CParam 這里需要注意的是在程序中我們可能在很多地方都要實現配置參數的讀寫,我們不能在每個要使用的地方都通過new關鍵字來創建一個CParam對象。原因你懂的,呵呵!那麼我們可以通過定義CParam的一個靜態成員來實現,這個靜態成員通過一個靜態的成員函數來獲取。
⑽ C語言,如何讀入一個10M左右的配置文件到程序中 配置文件的內容是程序中定義的某個結構體的結構內容。
要讀入的文件內容為:
TERMSTRU ermstru[]=
{
{ "111111", "EA5U" },
{ "222222", "EA5U" },
{ "333333", "EA5U" },
{ "444444", "DHTD" },
{ "155555", "DHLO" },
}
大概上萬行
本來這個內容是在程序體裡面的,但是需要經常修改程序內容,所以我想把它變成一個配置文件,每次執行程序的時候自動讀入到程序中。
懇請各位高手援助,最好寫出程序,萬分感謝!!!