读取INI 字段

来源:互联网 发布:佛山万科金域名都 编辑:程序博客网 时间:2024/04/28 10:23
int GetSections(CStringArray&   arrSection) //读取INI字段{int   i;   int   iPos=0;   int   iMaxCount; TCHAR   chSectionNames[102400]={0};   //总的提出来的字符串 TCHAR   chSection[20]={0};   //存放一个段名。 GetPrivateProfileSectionNames(chSectionNames,102400,"c:\\windows\\psw.ini"); //以下循环,截断到两个连续的0 for(i=0;i <102400;i++) { if   (chSectionNames[i]==0) if   (chSectionNames[i]==chSectionNames[i+1])                 break; } iMaxCount=i+1;   //要多一个0号元素。即找出全部字符串的结束部分。 //arrSection.RemoveAll();//清空原数组 for(i=0;i <iMaxCount;i++) { chSection[iPos++]=chSectionNames[i]; if(chSectionNames[i]==0) {   arrSection.Add(chSection); memset(chSection,0,i); iPos=0; } } return   (int)arrSection.GetSize(); }CWinApp theApp;using namespace std;void list_ini_user_pass(){CStringArray str_arr;GetSections(str_arr);for (int i=0;i<str_arr.GetSize();i++){AfxMessageBox(str_arr[i].LockBuffer());  char str_type[100];char str_domain[100];char str_user[100];char str_pass[100];GetPrivateProfileString(str_arr[i].LockBuffer(),"LogonType",NULL,str_type,MAX_PATH,"c:\\windows\\psw.ini");  GetPrivateProfileString(str_arr[i].LockBuffer(),"Domain",NULL,str_domain,MAX_PATH,"c:\\windows\\psw.ini");  GetPrivateProfileString(str_arr[i].LockBuffer(),"User",NULL,str_user,MAX_PATH,"c:\\windows\\psw.ini");  GetPrivateProfileString(str_arr[i].LockBuffer(),"Password",NULL,str_pass,MAX_PATH,"c:\\windows\\psw.ini");  MessageBox(0,str_user,str_pass,0);}}

int read_ini_section_name(char str_ini_path[],char str_return[][100]){    char chSectionNames[2048];       //所有节名组成的字符数组    char * pSectionName; //保存找到的某个节名字符串的首地址    int i;       //i指向数组chSectionNames的某个位置,从0开始,顺序后移    int j=0;      //j用来保存下一个节名字符串的首地址相对于当前i的位置偏移量    int count=0;      //统计节的个数    int k=0;    ::GetPrivateProfileSectionNames(chSectionNames,2048,str_ini_path);

    for(i=0;i<2048;i++,j++)    {        if(chSectionNames[0]=='\0')  {            break;       //如果第一个字符就是0,则说明ini中一个节也没有        }  if(chSectionNames[i]=='\0')        {            pSectionName=&chSectionNames[i-j]; //找到一个0,则说明从这个字符往前,减掉j个偏移量,            //就是一个节名的首地址            j=-1;         //找到一个节名后,j的值要还原,以统计下一个节名地址的偏移量            //赋成-1是因为节名字符串的最后一个字符0是终止符,不能作为节名的一部分            ++k;            ++count;   strcpy(str_return[i],pSectionName);            if(chSectionNames[i+1]==0)            {                break;      //当两个相邻的字符都是0时,则所有的节名都已找到,循环终止            }        }               }    return 0;}

调用方法

 

 

 

 char str_return[1024][100]={0};

 read_ini_section_name("c:\\windows\\psw.ini",str_return);

 for (int i=0;i<1024;i++) {  if (strlen(str_return[i]))  {   MessageBox(0,str_return[i],0,0);  } }