Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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++ 读取ini文件C++;错误_C++_Ini - Fatal编程技术网

C++ 读取ini文件C++;错误

C++ 读取ini文件C++;错误,c++,ini,C++,Ini,我有一个ini文件,其中包含以下信息: 这是我的代码: #include "stdafx.h" #include "iostream" #include <stdio.h> #include "IniReader.h" #include "INIReader.h" using namespace std; int main(int argc, char * argv[]) { INIReader reader("C:\SampleFile.ini"); if (r

我有一个ini文件,其中包含以下信息: 这是我的代码:

#include "stdafx.h"
#include "iostream"
#include <stdio.h>
#include "IniReader.h"
#include "INIReader.h"

using namespace std;
int main(int argc, char * argv[])
{
    INIReader reader("C:\SampleFile.ini");

    if (reader.ParseError() < 0) {
   cout << "Can't load 'test.ini'\n";
        return 1;
    }
    std::cout << "Config loaded from 'test.ini': version="
              << reader.GetInteger("info", "CaptureDuration", -1) << "CaptureDuration"<<"\n"
              << reader.Get("info", "DayStart", `enter code here`"UNKNOWN") << ", email="; 


cin.get();
 return 0;   
}
#包括“stdafx.h”
#包括“iostream”
#包括
#包括“IniReader.h”
#包括“INIReader.h”
使用名称空间std;
int main(int argc,char*argv[])
{
InReader阅读器(“C:\SampleFile.ini”);
if(reader.ParseError()<0){

不能使用Windows API,如
GetPrivateProfileString
WritePrivateProfileString
来读取.ini文件。它工作正常,不需要任何库。我使用了几十年。

LNK2019通常有以下原因之一:

  • 您忘记为您的库链接正确的lib文件(本例中为ini读取器)
  • 您为库链接了错误的库文件(例如静态链接与动态链接)
  • 您忘记将库的cpp文件添加到项目中
  • 您混合了适用于不同平台(如x86和x64)的预编译代码
编辑: 在这个特定的实例中,您必须将文件IniReader.cpp添加到项目中,因为它提供了缺少的函数。

最简单的方法是:

  • inih
    的所有文件复制到项目目录中(我认为它是C:\Users\Owner1\Documents\Visual Studio 2008\Projects\InReader)
  • inih
    的所有文件拖动到Visual Studio 2008中的项目中
  • 然后按build按钮
该错误的原因如@Mario所说。您只编译了您的cpp文件,但没有编译项目所需的其他cpp文件。因此,您还需要编译inih
中的所有.cpp文件


注意:我认为你应该阅读以了解更多有关C的编译和链接的信息。

不过,在另一个平台上尝试一下,祝你好运。:)但是是的,对于一些仅限Windows的程序,我想这是最好的方法。最初的帖子包含
stdafx.h
…这看起来是一个Windows项目。我使用了GetPrivateProfileString,但它打印了我在函数中写入的数字不是文件中的数字。它打印1057和143!使用名称空间std;int main(int argc,_TCHAR*argv[]){char DayStart[1000];int CaptureDuration;GetPrivateProfileString(“info”,“DayStart”,“1057”,DayStart,255,“C:\SampleFile.ini”);CaptureDuration=GetPrivateProfileInt(“info”,“CaptureDuration”,143,“C:\SampleFile.ini”);//注意:WritePrivateProfileInt()不存在,因为我看不到代码中的问题。它应该可以工作。请检查调试器中返回的数据。您的问题是“它打印了数字”对于那些<代码> >包含“< /Cord>”语句,使用“角括号<代码> <代码>。很难摆脱25年前重要的文件格式。读取一个INI文件非常昂贵,在一个设置中20到50毫秒之间。让我们都把C++编译器归咎于瘦。k那
\S
是一个有效的字符,也很难去掉。@HansPassant:20ms是任何合理的硬盘上的平均seek+延迟。-或者你是说Windows API很慢?但是,如果性能和手动调整的易用性很重要,有什么更好的选择呢?(如果你说XML或注册表,我可能会笑…)谢谢。但是我在哪里可以找到lib文件。通常我在debug中找到它们并将它们附加到项目中,但我在这里看不到它们。我想使用CaptureDuration=GetPrivateProfileInt(“info”,“CaptureDuration”,143,“C:\SampleFile.ini”);但它在此处打印的是defaullt编号,而不是ini文件中的编号。我能做什么?您必须将IniReader.cpp添加到您的项目中。@user2748701,一种方法是将.cpp添加到您的项目中。如果您不想在项目中包含源代码,则应将IniReader类添加到一个空项目中,并编译该项目以获得dll和libs。
Errors:
,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?Get@INIReader@@QAE?AV?$

1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall INIReader::ParseError(void)" (?ParseError@INIReader@@QAEHXZ) referenced in function _main
@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main

1>C:\Users\Owner1\Documents\Visual Studio 2008\Projects\inireader\Debug\inireader.exe : fatal error LNK1120: 4 unresolved externals