首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

一个c++读取.ini文件有关问题

2012-03-31 
一个c++读取.ini文件问题#include iostream.h#include stdio.h#include windows.h#define N 20int m

一个c++读取.ini文件问题
#include "iostream.h" 
#include "stdio.h" 
#include "windows.h" 
#define N 20 

int main(int argc,char* argv[]) 

char UserName_Input[N]; 
char Password_Input[N]; 
char UserName[N]; 
char Password[N]; 

cout < <"请输入用户名:"; 
cin > >UserName_Input; 

cout < <"请输入密码:"; 
cin > >Password_Input; 

GetPrivateProfileString("UserInfo","UserName","",UserName,20,"F:\\UserInfo.ini"); 
GetPrivateProfileString("UserInfo","Password","",Password,20,"F:\\UserInfo.ini"); 


if(strcmp(UserName_Input,UserName)!=0 ¦ ¦strcmp(Password_Input,Password)!=0) 
cout < <"登录失败!请您重新登录。" < <endl; 
else 
cout < <"登录成功!欢迎您的登录。" < <endl; 

cout < <UserName < <" " < <Password < <endl; 
return 0; 



UserInfo.ini 
[UserInfo] 
UserName=f 
Password=f 

执行完后cout < <"登录成功!欢迎您的登录。" < <endl;什么也没有! 

______________________________________ 
请输入用户名:f 
请输入密码:f 
登录失败!请您重新登录。 

Press any key to continue 
______________________________________ 
查看GetPrivateProfileString()时,说了: 
头文件:winreg.h 连接库:advapi32.lib 
这里的:连接库:advapi32.lib是什么意思? 

如何才能正确读取ini?是不是我的UserInfo.ini有什么不对? 


[解决办法]
在使用GetPrivateProfileString之前先清空UserName和Password
使用之后立即把获得的信息打印出来
[解决办法]
直接使用fstream来读取ini文件也可以啊
[解决办法]
MSDN中解释:
The GetPrivateProfileString function retrieves a string from the specified section in an initialization file.

Note: This function is provided only for compatibility with 16-bit Windows-based applications. Applications should store initialization information in the registry

既GetPrivateProfileString函数只能读取已经注册的ini文件

注册方法:
Windows Server 2003, Windows XP/2000/NT: Calls to private profile functions may be mapped to the registry instead of to the specified initialization files. This mapping occurs when the initialization file and section are specified in the registry under the following keys:


HKEY_LOCAL_MACHINE\Software\Microsoft\
Windows NT\CurrentVersion\IniFileMapping

This mapping is likely if an application modifies system-component initialization files, such as Control.ini, System.ini, and Winfile.ini. In these cases, the GetPrivateProfileString function retrieves information from the registry, not from the initialization file; the change in the storage location has no effect on the function's behavior.

The profile functions use the following steps to locate initialization information:


Look in the registry for the name of the initialization file, say MyFile.ini, under IniFileMapping: 
HKEY_LOCAL_MACHINE\Software\Microsoft\
Windows NT\CurrentVersion\IniFileMapping\myfile.ini

Look for the section name specified by lpAppName. This will be a named value under myfile.ini, or a subkey of myfile.ini, or will not exist. 
If the section name specified by lpAppName is a named value under myfile.ini, then that value specifies where in the registry you will find the keys for the section. 
If the section name specified by lpAppName is a subkey of myfile.ini, then named values under that subkey specify where in the registry you will find the keys for the section. If the key you are looking for does not exist as a named value, then there will be an unnamed value (shown as <No Name>) that specifies the default location in the registry where you will find the key. 


If the section name specified by lpAppName does not exist as a named value or as a subkey under myfile.ini, then there will be an unnamed value (shown as <No Name>) under myfile.ini that specifies the default location in the registry where you will find the keys for the section. 
If there is no subkey for MyFile.ini, or if there is no entry for the section name, then look for the actual MyFile.ini on the disk and read its contents. 

[解决办法]
这个就要看你的.INI的文件结构了。再就是调用的参数有没有错。
[解决办法]
http://topic.csdn.net/t/20041104/11/3520769.html
[解决办法]
http://blog.csdn.net/yangjundeng/archive/2007/01/15/1483594.aspx
[解决办法]
楼主,我刚给你试过了,输出的是"登陆成功".

热点排行