C++ 读写文件总结

xiaoxiao2021-02-28  11

.建文件夹 int iNumber = GetCurrentDirectoryW(0,NULL); WCHAR dir[100]; GetCurrentDirectory(iNumber,dir);//这三句的意思见后12 CString csPath; if (i==0) { CString str1=_T("\\滚动勘探&油藏评价"); csPath=dir+str1; if (!PathIsDirectory(csPath)) { CreateDirectory(csPath,0); } CString strName=_T("\\ItemOptPar.txt"); csPath=csPath+strName; } //改变类型 const size_t strsize=(csPath.GetLength()+1)*2; // 宽字符的长度; char * pstr= new char[strsize]; //分配空间; size_t sz=0; wcstombs_s(&sz,pstr,strsize,csPath,_TRUNCATE); int n=atoi((const char*)pstr);  4.写入TXT 当前工作路径 FILE *fp; fp=fopen(文件路径(3中的pstr),"w"); fprintf(fp,"[遗传算法参数]\n"); fprintf(fp,"GA_N = "); fprintf(fp,"%S",inheritance[0]); fprintf(fp,";\n"); fprintf(fp,"GA_M = "); fprintf(fp,"%S",inheritance[1]); fprintf(fp,";\n"); fprintf(fp,"GA_Pm = "); fprintf(fp,"%S",inheritance[2]); fprintf(fp,";\n"); fclose(fp); //写入文件      CStdioFile file;      CString strPath;      GetModuleFileName( NULL, strPath.GetBuffer(MAX_PATH), MAX_PATH ); //NULL 返回应用程序的全路径      strPath.ReleaseBuffer(); //清空缓冲区      strPath = strPath.Left(strPath.ReverseFind(_T('\\')));      strPath=strPath+_T("\\index.txt");      if(!file.Open(strPath,CFile::modeWrite|CFile::modeCreate)) return;      for (int i = 0; i < dlg.m_select_data.index_ori.size(); i ++)      {       CString str;       str.Format("%d",dlg.m_select_data.index_ori[i]);       file.WriteString(str);       file.WriteString("\t");       str.Format("%d",dlg.m_select_data.index_now[i]);       file.WriteString(str);       file.WriteString("\t");       str.Format("%s",dlg.m_select_data.data[i][1]);       file.WriteString(str);       file.WriteString("\t");       str.Format("%s",dlg.m_select_data.data[i][2]);       file.WriteString(str);       file.WriteString("\n");      } file.Close(); 5.读取TXT 第一种: FILE *fp1,*fp2; int count,i; fp1=fopen("Table5-4.txt","r"); if(fp1==0) { printf("Error!Can't open it!\n"); return 0; } fscanf(fp1,"%d",&count); struct data1 { double x; double y; }*data; data=new data1[count]; for(i=0;!feof(fp1);i++) { fscanf(fp1,"%lf %lf",&data[i].x,&data[i].y); X=data[i].x+X; Y=data[i].y+Y; S=S+data[i].x*data[i].y; sum=sum+(data[i].x*data[i].x); } fclose(fp1); 第二种: #include <iostream> #include <fstream> infile.open("C:\\Users\\ZWL\\Desktop\\hlj.txt",ios::in); while(!infile.eof()) // 若未到文件结束一直循环 { getline(infile, str1, '\n');//读取一行,以换行符结束,存入 vstr.push_back(str1); i++; //下一行 } 第三种: FILE *fp; if((fp=fopen("C:\\Users\\ZWL\\Desktop\\ff3.TXT","r"))==NULL) //读取.txt的文件 { printf("读取文件失败\n "); exit(1); } std::cout<<"读取"<<"成功"<<std::endl; while(!feof(fp)) { string str1=_T(""); //str1=fgetc(fp); if (fgetc(fp)!='\n') { str1=str1+fgetc(fp); } //printf("%c",fgetc(fp));//每次获取一个字符并打印 } 第四种: CStdioFile file; CString strline; vector<CString> strtemp; product_data.clear(); measured_data.clear(); if(!file.Open(FilePath_DayData,CFile::modeRead)) return FALSE; while(file.ReadString(strline)){ if(strline != _T("")) { char *str = strline.GetBufferSetLength(strline.GetLength()); char *p; for (p=strtok(str,"\t");p!=NULL;p=strtok(NULL,"\t")) { strtemp.push_back(p); } product_data.push_back(strtemp); strtemp.clear(); } } file.Close(); if (product_data[0].size() < 5) { AfxMessageBox(_T("生产日数据文件选择错误!")); return FALSE; }
转载请注明原文地址: https://www.6miu.com/read-2250304.html

最新回复(0)