Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 读取c+中的INI文件+;_C++_Ini - Fatal编程技术网

C++ 读取c+中的INI文件+;

C++ 读取c+中的INI文件+;,c++,ini,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&); i

我不知道该怎么做,请帮帮我:-

早些时候,我习惯于从.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文件,而只是使用注册表,但事实就是这样。

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


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

看来你已经把你要问的问题弄得很复杂了。我从你的问题中得到的是,你想把一个IP地址字符串解析成一个IP地址,而不是从INI文件中读取它,你已经这样做了

你不需要。操作系统为你做这件事。使用
gethostbyname
是一个简单的答案


如果这是家庭作业或什么的,你必须填写签名,首先你就完蛋了,因为它不会返回任何东西……你必须设置一些愚蠢的全局值来返回值或什么的。但是,您可以使用
sscanf

完成这项工作,因为您似乎已经将您想要问的问题严重地复杂化了。我从你的问题中得到的是,你想把一个IP地址字符串解析成一个IP地址,而不是从INI文件中读取它,你已经这样做了

你不需要。操作系统为你做这件事。使用
gethostbyname
是一个简单的答案


如果这是家庭作业或什么的,你必须填写签名,首先你就完蛋了,因为它不会返回任何东西……你必须设置一些愚蠢的全局值来返回值或什么的。但是,您可以使用
sscanf

完成这项工作。该代码看起来非常糟糕,而且当您在ini文件中添加越来越多的配置选项时,它不会变得更漂亮。您应该使用boost库来检查这一点。

这段代码看起来非常糟糕,当您在ini文件中添加越来越多的配置选项时,它不会变得更漂亮。您应该使用boost库来查看这一点

#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;   
}
void readServerConfig(string& serverIPAddr)
{
    //Please guide how to write this function
}