用c ++读取INI文件

时间:2012-05-22 15:18:46

标签: c++ ini

我不知道怎么做才能帮助我: -

之前我曾经从.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
}

3 个答案:

答案 0 :(得分:1)

请记住,当您需要做一些已经由数百万程序员完成的事情之前,通常有一种方法可以轻松完成,而不需要重新发明轮子。

在这种情况下,我会使用GetPrivateProfileString。实际上,我可能会忘记INI文件而只是使用注册表,但它就是它。

答案 1 :(得分:1)

你似乎对你要问的问题感到非常困惑。我从你的问题中得到的是你想要将IP地址字符串解析为IP地址,而不是从你已经完成的INI文件中读取它。

你不需要。操作系统为您完成。使用gethostbyname是一个简单的答案。

如果这是家庭作业或其他东西,你必须填写该签名,首先你被搞砸了因为它不会返回任何东西......你将不得不设置一些愚蠢的全局来返回价值或其他东西。但是,您可以使用sscanf完成工作。

答案 2 :(得分:1)

该代码看起来非常讨厌,而且在ini文件中添加越来越多的配置选项时,它不会变得更漂亮。您应该使用boost库查看此nice alternative