文件操作

来源:互联网 发布:windows查ip地址 编辑:程序博客网 时间:2024/05/18 13:31
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <iostream>


const char *p_str_server_ip = "server_ip";
const char *p_str_server_port = "server_port";
const char *p_str_db_name = "db_name";
const char *p_str_db_user = "db_user";
const char *p_str_db_pwd = "db_pwd";


#define CONFIG_MSG_LEN_MAX  256
#define CONFIG_PARA_LEN_MAX 32


typedef struct _CONFIG_MESSAGE
{
CString server_ip;
unsigned int server_port;
CString db_name;
CString db_user;
CString db_pwd;
}CONFIG_MESSAGE;




int str_to_num(unsigned char *p_str)//将字符串格式的十进制数转成十进制数
{
unsigned int i;
int temp = 0;


/* 限制下字符长度 */
if(sizeof(p_str)>10)
{
return -1;
}


/* 逐个读取字符 */
for(i=0;i<sizeof(p_str);i++)
{
temp = 10*temp;
if( (*p_str>='0')&&(*p_str<='9') )//字符串里的字符必须都是10进制字符
{
temp = temp + (*p_str-'0');
}
else
{
return -2;
}
p_str++;
}


return temp;
}




int get_config_info(void)
{
unsigned int i,j;
unsigned char file_buff[CONFIG_MSG_LEN_MAX];
unsigned char flg;
unsigned char msg_server_ip[CONFIG_PARA_LEN_MAX+1];
unsigned char msg_server_port[CONFIG_PARA_LEN_MAX+1];
unsigned char msg_db_name[CONFIG_PARA_LEN_MAX+1];
unsigned char msg_db_user[CONFIG_PARA_LEN_MAX+1];
unsigned char msg_db_pwd[CONFIG_PARA_LEN_MAX+1];


HANDLE hFile;
DWORD  dwToReadCharNums;
    DWORD  dwReadCharNums;
BOOL   bReadFile;


/* 打开或创建日志文件 */
hFile = CreateFile("config.txt",           
  GENERIC_READ,         
  FILE_SHARE_READ,       
  NULL,                  
  OPEN_EXISTING,            
  FILE_ATTRIBUTE_NORMAL,
  NULL);
if(INVALID_HANDLE_VALUE == hFile)
{
return -1;
}


/* 读取配置信息 */
dwToReadCharNums = CONFIG_MSG_LEN_MAX;
dwReadCharNums = 0;
bReadFile = ReadFile( hFile,            //文件句柄
                          file_buff,        //保存文件内容的缓冲区
                          dwToReadCharNums, //预读取的字符数
                          &dwReadCharNums,  //实际读取的字符数
                          NULL              //一般为空
                        );
if( (FALSE == bReadFile)||(dwReadCharNums<10) )
{
        return -1;
    }


/**************分析第一行配置信息****************/
flg = 0;
i = 0;
j = 0;
for(i=0;i<dwReadCharNums;i++)
{
if( ('\r'==file_buff[i])&&('\n'==file_buff[i+1]) )
{
i = i+2;//将读位置跳到下行信息
msg_server_ip[j] = '\0';//在数组末尾放置字符串结束符
break;
}


if( '=' == file_buff[i] )
{
flg = 1;
continue;
}


if(flg)
{
msg_server_ip[j] = file_buff[i];
if(++j>CONFIG_PARA_LEN_MAX)
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}
}
else
{
if( (*p_str_server_ip) != file_buff[i] )
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}
p_str_server_ip++;
}
}
if(0==flg)//如果本次没有检测到'=',则认为该行信息检测失败
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}


/**************分析第二行配置信息****************/
flg = 0;
j = 0;
for(i=i;i<dwReadCharNums;i++)
{
if( ('\r'==file_buff[i])&&('\n'==file_buff[i+1]) )
{
i = i+2;//将读位置跳到下行信息
msg_server_port[j] = '\0';//在数组末尾放置字符串结束符
break;
}


if( '=' == file_buff[i] )
{
flg = 1;
continue;
}


if(flg)
{
msg_server_port[j] = file_buff[i];
if(++j>CONFIG_PARA_LEN_MAX)
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}
}
else
{
if( (*p_str_server_port) != file_buff[i] )
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}
p_str_server_port++;
}
}
if(0==flg)//如果本次没有检测到'=',则认为该行信息检测失败
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}


str_to_num(msg_server_port);


/**************分析第三行配置信息****************/
flg = 0;
j = 0;
for(i=i;i<dwReadCharNums;i++)
{
if( ('\r'==file_buff[i])&&('\n'==file_buff[i+1]) )
{
i = i+2;//将读位置跳到下行信息
msg_db_name[j] = '\0';//在数组末尾放置字符串结束符
break;
}


if( '=' == file_buff[i] )
{
flg = 1;
continue;
}


if(flg)
{
msg_db_name[j] = file_buff[i];
if(++j>CONFIG_PARA_LEN_MAX)
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}
}
else
{
if( (*p_str_db_name) != file_buff[i] )
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}
p_str_db_name++;
}
}
if(0==flg)//如果本次没有检测到'=',则认为该行信息检测失败
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}


/**************分析第四行配置信息****************/
flg = 0;
j = 0;
for(i=i;i<dwReadCharNums;i++)
{
if( ('\r'==file_buff[i])&&('\n'==file_buff[i+1]) )
{
i = i+2;//将读位置跳到下行信息
msg_db_user[j] = '\0';//在数组末尾放置字符串结束符
break;
}


if( '=' == file_buff[i] )
{
flg = 1;
continue;
}


if(flg)
{
msg_db_user[j] = file_buff[i];
if(++j>CONFIG_PARA_LEN_MAX)
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}
}
else
{
if( (*p_str_db_user) != file_buff[i] )
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}
p_str_db_user++;
}
}
if(0==flg)//如果本次没有检测到'=',则认为该行信息检测失败
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}


/**************分析第五行配置信息****************/
flg = 0;
j = 0;
for(i=i;i<dwReadCharNums;i++)
{
if( ('\r'==file_buff[i])&&('\n'==file_buff[i+1]) )
{
i = i+2;//将读位置跳到下行信息
msg_db_pwd[j] = '\0';//在数组末尾放置字符串结束符
break;
}


if( '=' == file_buff[i] )
{
flg = 1;
continue;
}


if(flg)
{
msg_db_pwd[j] = file_buff[i];
if(++j>CONFIG_PARA_LEN_MAX)
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}
}
else
{
if( (*p_str_db_pwd) != file_buff[i] )
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}
p_str_db_pwd++;
}
}
if(0==flg)//如果本次没有检测到'=',则认为该行信息检测失败
{
if(NULL!=hFile)
{
CloseHandle(hFile);
}
return -2;
}
msg_db_pwd[j] = '\0';//在数组末尾放置字符串结束符




/**********************************************************/
if(NULL!=hFile)
{
CloseHandle(hFile);
}

return 0;


}
/**************************************************************/
0 0
原创粉丝点击