2017-02-09 66 views
0

我需要说的编译,我是新手,在C和我只写了约100-150行代码在C属性文件读取C(没有C#或C++)使用MinGW

我需要阅读用的.properties文件中的条目类似如下:

Value1 = Hello 
Value2 = Bye 

我希望得到这样的价值观:

bla.getValue("Value1"); 

这样就可以与它的工作是这样的:

foo = bla.getValue("Value1"); 
bar = bla.getValue("Value2"); 
printf("%s - %s",foo,bar); 

我不需要他们做任何事情,而不是将它们打印到屏幕上。

我发现了两个问题在这里,它走进了正确的方向,但他们无法帮助我在我的任务:
How to read configuration/properties file in C?

Properties file library for C (or C++)
我尝试了上述线程的答案多,但无论哪种方式,我的编译器(MinGW的)不喜欢这些行之一:

using foo::bar; 
or 
using namespace foo; 

当我尝试编译我的代码,我得到一个错误说:
错误:

#include <windows.h> 
#include <stdio.h> 
#include <string.h> 
using platformstl::properties_file; 

int WINAPI WinMain(HINSTANCE a,HINSTANCE b,LPSTR c,int d) 
{ 
char *tPath, *tWindow; 
char *search = " "; 

tWindow = strtok(c, search); 
tPath = strtok(NULL, search); 
properties_file properties("%s",tPath); 

properties::value_type value1 = properties["Value1"]; 
properties::value_type value2 = properties["Value2"]; 

printf("Window: %s; Path: %s; %s %s",tWindow,tPath,value0,value1); 
} 

我使用的WinMain,因为PROGRAMM就是找到一个:“使用”

这是我试图执行的线程的上述给定的溶液中的代码未知类型名打开窗户。我没有包含代码的这些部分,因为它们与我的问题无关,并且工作得很好。 strtok();部件也适用于我。我需要他们,因为窗口的标题查找和属性文件的路径都给出命令行参数:

programm.exe windowtitle path/to/properties/file

当我和其他的答案,这让我加载某些库试过了,我得到了到某个地方,所需的库不包含所需的头文件。有些库甚至用于C++,我有一个限制,所以我不能使用它。

我希望能让事情更清楚一些,因为您可能知道我不习惯在这里提问。 :)

+2

这个问题要么是脱离主题,要么是广泛的。请花些时间阅读[帮助页面](http://stackoverflow.com/help),尤其是名为[“我可以问些什么话题?”]的章节(http://stackoverflow.com/help/)讨论话题)和[“我应该避免问什么类型的问题?”](http://stackoverflow.com/help/dont-ask)。另请[请阅读如何提出好问题](http://stackoverflow.com/help/how-to-ask)以及如何创建[最小,完整和可验证示例](http://stackoverflow.com /帮助/ MCVE)。 –

+0

我不明白你的第一个链接不能工作吗? – KevinDTimm

+0

欢迎来到Stack Overflow!请[编辑]你的问题,以显示[你迄今为止所尝试的](http://whathaveyoutried.com)。您应该包含您遇到问题的代码[mcve],然后我们可以尝试帮助解决特定问题。你还应该阅读[问]。 –

回答

0

我用一个很大的解决方法解决了我的问题。

这是我的最终代码:

if(vn != NULL){ 
for(i = 0; i < 1; i++){ 
    if(fgets(temp, BUF, vn) == NULL){ 
    printf("Line is empty"); 
    return 2; 
    } 
} 
if(fgets(puffer, BUF, vn) == NULL){ 
    printf("Line is empty"); 
    return 2; 
} 
tVariable = strtok(puffer, find); 
tValue = strtok(NULL, find); 
}else { 
printf("Unable to read File"); 
return 2; 
} 

我刚才读给定文件的第二行和削减它在=迹象。

我知道,我需要阅读第二行,因为我需要的属性总是在.properties文件的第二行中找到。

我现在有我想要的价值tValue,所以我可以用它来打印出printf("%s", tValue)