[英]Reading INI file in c++
我不知道該怎么做,請幫幫我:-
之前我曾經從 .txt 文件中獲取服務器 ip 地址,因此很容易從 txt 文件中讀取 ip 地址
#include ".mySocket.h"
#include "myLog.h"
#include "myException.h"
#include "myHostInfo.h"
myLog winLog;
void readServerConfig(string&);
void checkFileExistence(const string&);
int main()
{
// Initialize the winsock library
myTcpSocket::initialize();
// get client's information (assume neither the name nor the address is given)
winLog << endl;
winLog << "Retrieve the localHost [CLIENT] name and address:" << endl;
myHostInfo clientInfo;
string clientName = clientInfo.getHostName();
string clientIPAddress = clientInfo.getHostIPAddress();
cout << "Name: " << clientName << endl;
cout << "Address: " << clientIPAddress << endl;
winLog << " ==> Name: " << clientName << endl;
winLog << " ==> Address: " << clientIPAddress << endl;
// get server's IP address and name
string serverIPAddress = "";
readServerConfig(serverIPAddress);
winLog << endl;
winLog << "Retrieve the remoteHost [SERVER] name and address:" << endl;
winLog << " ==> the given address is " << serverIPAddress << endl;
myHostInfo serverInfo(serverIPAddress,ADDRESS);
string serverName = serverInfo.getHostName();
cout << "Name: " << serverName << endl;
cout << "Address: " << serverIPAddress << endl;
winLog << " ==> Name: " << serverName << endl;
winLog << " ==> Address: " << serverIPAddress << endl;
// create the socket for client
myTcpSocket myClient(PORTNUM);
cout << myClient;
winLog << "client configuation: " << endl;
winLog << myClient;
// connect to the server.
cout << "connecting to the server [" << serverName << "] ... " << endl;
winLog << "connecting to the server [" << serverName << "] ... " << endl;
myClient.connectToServer(serverIPAddress,ADDRESS);
int recvBytes = 0;
while (1)
{
// send message to server
char messageToServer[MAX_MSG_LEN+1];
memset(messageToServer,0,sizeof(messageToServer));
cout << "[SEND] ";
cin.getline(messageToServer,MAX_MSG_LEN);
winLog << "[SEND] " << messageToServer << endl;
myClient.sendMessage(string(messageToServer));
if ( !string(messageToServer).compare("Quit") || !string(messageToServer).compare("quit") )
break;
// receive message from server
string messageFromServer = "";
recvBytes = myClient.recieveMessage(messageFromServer);
if ( recvBytes == -99 )
break;
cout << "[RECV:" << serverName << "]: " << messageFromServer << endl;
winLog << "[RECV:" << serverName << "]: " << messageFromServer << endl;
}
return 1;
}
//from the following code i read the ip address from the txt file but now how to read from INI file
**void readServerConfig(string& serverIPAddr)
{
string serverConfigFile = "serverConfig.txt";
checkFileExistence(serverConfigFile);
ifstream serverConfig(serverConfigFile.c_str());
// read server's IP address
getline(serverConfig,serverIPAddr);
serverConfig.close();
}**
void checkFileExistence(const string& fileName)
{
ifstream file(fileName.c_str());
if (!file)
{
cout << "Cannot continue:" << fileName << " does NOT exist!" << endl;
exit(1);
}
file.close();
}
但是現在我必須從 INI 文件中讀取服務器 ip 地址,現在的問題是如何讀取 INI 文件以獲取 ip 地址
我的 INI 文件讀寫器
#include "iostream"
#include "IniWriter.h"
#include "IniReader.h"
int main(int argc, char * argv[])
{
CIniWriter iniWriter(".\\Logger.ini");
iniWriter.WriteString("Setting", "ServerIp", "192.168.15.168");
iniWriter.WriteString("Setting", "MultiCastIp", "239.255.42.42");
iniWriter.WriteString("Setting", "Name", "jianxx");
iniWriter.WriteInteger("Setting", "Age", 27);
iniWriter.WriteFloat("Setting", "Height", 1.82f);
iniWriter.WriteBoolean("Setting", "Marriage", false);
CIniReader iniReader(".\\Logger.ini");
char *szName = iniReader.ReadString("Setting", "Name", "");
int iAge = iniReader.ReadInteger("Setting", "Age", 25);
float fltHieght = iniReader.ReadFloat("Setting", "Height", 1.80f);
bool bMarriage = iniReader.ReadBoolean("Setting", "Marriage", true);
char *szName1 = iniReader.ReadString("Setting", "MultiCastIp", "");
std::cout<<"Name:"<<szName<<std::endl
<<"Age:"<<iAge<<std::endl
<<"Height:"<<fltHieght<<std::endl
<<"Marriage:"<<bMarriage<<std::endl
**<<"MultiCastIp:"<<szName1<<std::endl;//i want to read this ip**
while(1);
delete szName;
return 1;
}
請幫幫我,我不知道如何編寫以下函數來獲取ip地址
void readServerConfig(string& serverIPAddr)
{
//Please guide how to write this function
}
請記住,當您需要做一些在您之前數百萬程序員已經做過的事情時,通常有一種方法可以輕松完成,而無需重新發明輪子。
在這種情況下,我會使用GetPrivateProfileString 。 實際上,我可能會忘記 INI 文件而只使用注冊表,但事實就是如此。
看來你已經把你要問的問題搞得很復雜了。 我從您的問題中得到的是,您想將 IP 地址字符串解析為 IP 地址,而不是從您已經完成的 INI 文件中讀取它。
你不需要。 操作系統為您完成。 使用gethostbyname
是一個簡單的答案。
如果這是家庭作業或其他東西,並且您必須填寫該簽名,首先您會被搞砸,因為它不會返回任何內容……您必須設置一些愚蠢的全局來返回值或其他東西。 但是,您可以使用sscanf
完成工作。
這段代碼看起來真的很糟糕,而且當您在 ini 文件中添加越來越多的配置選項時,它不會變得更漂亮。 您應該使用 boost 庫查看這個不錯的替代方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.