1.ini文件的读写
//读取ini后缀的文件
void ClassSelf::LoadUIConfig() { char readbuf[20];
//文件路径可以是相对路径,也可以是绝对路径 GetPrivateProfileString( "标题", "关键字1", "0", readbuf, 20, "文件路径"); m_delta_tad = atof(readbuf); GetPrivateProfileString( "标题", "关键字2", "0", readbuf, 20, "文件路径"); m_inject_el = atof(readbuf); }
//保存ini后缀文件 void CFormView_Annt::SaveUIConfig() { CString stmp; stmp.Format("%.4lf",m_delta_tad); WritePrivateProfileString("标题", "关键字1", stmp ,"文件路径"); stmp.Format("%.4lf",m_inject_el); WritePrivateProfileString("标题", "关键字2", stmp ,"文件路径"); } void CMainFrame::LoadIniConfig() { string filename = workingdir + "\config\settings.ini"; char ip[50] = {0}; int port = 0; GetPrivateProfileString("网络配置","IP地址","",ip,50,filename.c_str()); m_mul_ip_to_center = ip; port = GetPrivateProfileInt("网络配置","port端口号",0,filename.c_str()); m_port_to_center = port; WritePrivateProfileString("网络配置","IP地址",dlg.m_to_center_ip_buff,filename.c_str()); }
2.txt文件的读写
(1)分割字符串
vector<string> tokenize(const string& src, string tok, bool trim=false, string null_subst="") { vector<string> v; if( src.empty() || tok.empty() ) return v; int pre_index = 0, index = 0, len = 0; while( (index = src.find_first_of(tok, pre_index)) > -1 ) { if( (len = index-pre_index)!=0 ) { v.push_back(src.substr(pre_index, len)); } else if(!trim) { v.push_back(null_subst); } pre_index = index+1; } string endstr = src.substr(pre_index); if( !trim ) { v.push_back( endstr.empty()?null_subst:endstr ); } else if( !endstr.empty() ) { v.push_back(endstr); } return v; }
(2)文件有多列相同含义
void CMainFrame::LoadOrderConfig() { string filename = workingdir + "\config\order.txt"; ifstream in(filename.c_str()); char rdBuf[10000]; int line = 0; int invalid_lines_num = 0; while(in.getline(rdBuf,10000)) { line++; /* if (line==1) { continue; }*/ string s = rdBuf; vector<string> col_strs = tokenize(s," ",true); if (col_strs.size() < 5) { invalid_lines_num++; continue; } vector<u8> v_order; int i = 0; int value; for (size_t col=1;col<col_strs.size();++col) { sscanf(col_strs[col].c_str(),"%x",&value); v_order.push_back(value); } m_maporder.insert(map<int,vector<u8>>::value_type(atoi(col_strs[0].c_str() + 1), v_order)); int map_size = m_maporder.size(); ASSERT(map_size+invalid_lines_num == line); } }
(3)文件有2列
void CMainFrame::LoadDllConfig() { string filename = workingdir + "\config\dll.txt"; ifstream in(filename.c_str()); char rdBuf[1000]; int line = 0; while(in.getline(rdBuf,1000)) { line++; if (line==1) { continue; } string s = rdBuf; vector<string> col_strs = tokenize(s," ",true); m_mapdll.insert(map<int,int>::value_type(atoi(col_strs[0].c_str() + 1),atoi( col_strs[1].c_str() ) ) ); } }
(4)文件有多列不同含义
void CMainFrame::LoadCKCmdsConfig() { string filename = workingdir + "\config\ck_cmds.ini"; ifstream in(filename.c_str()); char rdBuf[1000]; int line = 0; while(in.getline(rdBuf,1000)) { line++; if (line==1) { continue; } string s = rdBuf; vector<string> col_strs = tokenize(s," ",true); if (col_strs.size()>=7) { S_CKCMD cmd; for (size_t col=0;col<col_strs.size();++col) { string col_str = col_strs[col]; switch (col) { case 0: cmd.time = atoi(col_str.c_str()); break; case 1: cmd.cmd_idx = atoi(col_str.c_str()+1); break; case 2: cmd.sSP = col_str; break; case 3: cmd.start_byte = atoi(col_str.c_str()); break; case 4: cmd.start_bit = atoi(col_str.c_str()); break; case 5: cmd.len_bit = atoi(col_str.c_str()); break; case 6: { int length = strlen(col_str.c_str()); if (length>2) { if (!strncmp(col_str.c_str(),"0X",2)) { sscanf_s(col_str.c_str(),"0X%X",&cmd.value); } else { cmd.value = atoi(col_str.c_str()); } } else { cmd.value = atoi(col_str.c_str()); } break; } default: break; } } m_ck_cmds.push_back(cmd); } } m_cur_idx = 0; }
(5)文件内容具有相同含义
void CSPPFrame::LoadInitialDataArea() { if (m_code.size()<1) { return; } string filename = str(boost::format("%s\config\XXX\XXX\%s.txt")%workingdir%m_code); CFile file; if (file.Open(filename.c_str(),CFile::modeRead|CFile::shareDenyNone)) { int len = file.GetLength(); char* readbuf = new char[len+1]; memset(readbuf,0,len+1); int readlen = file.Read(readbuf,len+1); string s = readbuf; vector<string> col_strs = tokenize(s," ",true); assert(col_strs.size() == m_pkLen); int pos = 0; for (size_t col=0;col<col_strs.size();++col) { int value; sscanf(col_strs[col].c_str(),"%x",&value); if (pos>=m_pkLen) { return; } m_pFrame->data[pos++] = value; } delete[] readbuf; file.Close(); } }
3. 得到当前目录
char sbuf[2000]; GetCurrentDirectory(2000, sbuf); workingdir = sbuf;
4.遍历文件
(1)读取文件内容
void CTopoRouteManger::_LoadTemporaryTopo() { xltlockguard m_lock_temporary; for (int i=0; i<SAT_NUM; ++i) { m_temporaryTopo[i].clear(); } WIN32_FIND_DATA fileData; HANDLE hfile; DWORD errcode = 0; string strTopoPath = m_strWorkPath; strTopoPath += "\config\table\topology\temporary\*-3200.txt"; hfile = FindFirstFileA(strTopoPath.c_str(),&fileData); if (hfile!=INVALID_HANDLE_VALUE && errcode!=ERROR_NO_MORE_FILES) { bool bHasFile = true; while (bHasFile) { string strName = fileData.cFileName; _LoadTemporaryFile(strName); bHasFile = FindNextFileA(hfile,&fileData); } } FindClose(hfile); return; }
(2)读取文件名字
vector< SP<struct XXX> > ClassSelf::Func(u32 a ,u32 b,int c) { vector<SP<XXX>> x; WIN32_FIND_DATA fileData; HANDLE hfile; DWORD errcode = 0; string strTopoPath = m_strWorkPath; strTopoPath += "\文件路径\*.txt"; hfile = FindFirstFileA(strTopoPath.c_str(),&fileData); if (hfile!=INVALID_HANDLE_VALUE && errcode!=ERROR_NO_MORE_FILES) { bool bHasFile = true; while (bHasFile) { CString strName_temp; string strName = fileData.cFileName; strName_temp = strName.c_str(); strName_temp = strName_temp.Right(strName_temp.GetLength()-strName_temp.FindOneOf("-")-1); int vali_wn = atoi(strName_temp.Left(3)); strName_temp = strName_temp.Right(strName_temp.GetLength()-4); int vali_sow = atoi(strName_temp.Left(strName_temp.FindOneOf("-"))); strName_temp = strName_temp.Right(strName_temp.GetLength()-strName_temp.FindOneOf("-")-1); int invali_wn = atoi(strName_temp.Left(3)); strName_temp = strName_temp.Right(strName_temp.GetLength()-4); int invali_sow = atoi(strName_temp.Left(strName_temp.FindOneOf("-"))); int vali_total_sow = vali_sow; int invali_total_sow = invali_sow; int start_total_sow = start_sow; if (start_total_sow>=vali_total_sow && start_total_sow<invali_total_sow) { func(strName,scid_tested,vecTopo); break; } bHasFile = FindNextFileA(hfile,&fileData); } } FindClose(hfile); return vecTopo; }
(3)找到文件读取文件
void CTopoRouteManger::_LoadTemporaryFile(const string& filename) { string strFilePath; strFilePath = m_strWorkPath; strFilePath += "\文件路径\"; strFilePath += filename; FILE* pfile; if(0 != fopen_s(&pfile, strFilePath.c_str(), "r")) { return; } SP<struct XXX> toporoute[SAT_NUM]; for (int i=0; i<SAT_NUM; ++i) { toporoute[i] = SP<XXX>(new TopoRoute()); } char rdBuf[2000]; ifstream in(strFilePath.c_str()); i32 line=0,col=0; int cnt = 0; while(in.getline(rdBuf,2000) && line<145) { string s = rdBuf; int startPos = 0; int pos = 0; while(pos>-1 && startPos>-1) { pos = s.find_first_not_of(' ',startPos); if(pos>-1) { int d; sscanf_s(rdBuf+pos,"%d",&d); if (line == 0) { toporoute[col]->sn = d; } else if (line == 1) { toporoute[col]->emp_wn = d; } else if (line == 2) { toporoute[col]->emp_sow = d; } else if (line == 3) { toporoute[col]->inp_wn = d; } else if (line == 4) { toporoute[col]->inp_sow = d; } else { for (int i=line-5; i<140; ++i) { int slot = i/7; int idx = i%7; switch (idx) { case 0: toporoute[col]->m_comm_slot[slot].a= d; break; case 1: toporoute[col]->m_comm_slot[slot].b= d; break; case 2: toporoute[col]->m_comm_slot[slot].c= d; break; case 3: toporoute[col]->m_comm_slot[slot].d= d; break; case 4: toporoute[col]->m_comm_slot[slot].e= d; break; case 5: toporoute[col]->m_comm_slot[slot].f= d; break; case 6: toporoute[col]->m_comm_slot[slot].g= d; break; default: break; } } } col++; } startPos = s.find_first_of(' ',pos); } line++; cnt = col; col = 0; } fclose(pfile); for (int i=0; i<SAT_NUM; ++i) { m_temporaryTopo[i].push_back(toporoute[i]); } }
(4)文件拷贝
/* 文件拷贝练习 */ #include <stdio.h> int main(int argc, char **argv) { int size = 0; FILE *p_src = NULL, *p_dest = NULL; char buf[100] = {0}; if (argc < 3) { printf("命令错误 "); return 0; } p_src = fopen(*(argv + 1), "rb"); if (!p_src) { printf("原始文件打开失败 "); return 0; } p_dest = fopen(*(argv + 2), "wb"); if (!p_dest) { printf("目标文件打开失败 "); fclose(p_src); p_src = NULL; return 0; } while (1) { size = fread(buf, sizeof(char), 100, p_src); if (!size) { break; } fwrite(buf, sizeof(char), size, p_dest); } fclose(p_dest); p_dest = NULL; fclose(p_src); p_src = NULL; return 0; }