欢迎来到天天文库
浏览记录
ID:8798021
大小:62.00 KB
页数:11页
时间:2018-04-08
《c对txt文件的读取与写入源代码》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
c++对txt文件的读取与写入/*这是自己写程序时突然用到这方面的技术,在网上搜了一下,特存此以备后用~*/#include#include#includeusingnamespacestd;intmain(){charbuffer[256];ifstreammyfile("c:\a.txt");ofstreamoutfile("c:\b.txt");if(!myfile){cout<<"Unabletoopenmyfile";exit(1);//terminatewitherror}if(!outfile){cout<<"Unabletoopenotfile";exit(1);//terminatewitherror}inta,b;inti=0,j=0;intdata[6][2];while(!myfile.eof()){myfile.getline(buffer,10);sscanf(buffer,"%d%d",&a,&b);cout<头文件读:从外部文件中将数据读到程序中来处理对于程序来说,是从外部读入数据,因此定义输入流,即定义输入流对象:ifsteaminfile,infile就是输入流对象。这个对象当中存放即将从文件读入的数据流。假设有名字为myfile.txt的文件,存有两行数字数据,具体方法:inta,b;ifstreaminfile;infile.open("myfile.txt");//注意文件的路径infile>>a>>b;//两行数据可以连续读出到变量里infile.close()如果是个很大的多行存储的文本型文件可以这么读:charbuf[1024];//临时保存读取出来的文件内容stringmessage;ifstreaminfile;infile.open("myfile.js");if(infile.is_open())//文件打开成功,说明曾经写入过东西{while(infile.good()&&!infile.eof()){memset(buf,0,1024);infile.getline(buf,1204);message=buf;......//这里可能对message做一些操作cout<#include#includeusingnamespacestd;//////////////从键盘上读取字符的函数voidread_save(){charc[80];ofstreamoutfile("f1.dat");//以输出方工打开文件if(!outfile){cerr<<"openerror!"<=65&&c[i]<=90||c[i]>=97&&c[i]<=122){//保证输入的字符是字符outfile.put(c[i]);//将字母字符存入磁盘文件cout<=97)ch=ch-32;outfile.put(ch);cout<#includeusingnamespacestd;typedefstructnode{intdata;structnode*next;}node;node*creat(ifstream&ifp) {node*h=NULL,*p=NULL,*q=NULL;intdata;while(ifp>>data){p=newnode;p->data=data;p->next=NULL;if(!h)h=p;elseq->next=p;q=p;}returnh;}voidprt(node*h){if(h){cout<data<next);}}int_tmain(intargc,_TCHAR*argv[]){//--file1openifstreamfp("d:\data.txt",ifstream::in);node*hst=creat(fp);fp.close();prt(hst);//--file2openifstreamr("d:\test.txt",ifstream::in);if(!r){cout<<"打开文件出错!"<>line){cout<>q;return0;}//---------------------------------------------------------------------------//d:\data.txt内容//123456//789101112//100998877//555657585960//d:\test.txt内容//姓名:hehe年龄:12备注:123//姓名:haihai年龄:18备注:12ff//姓名:aa年龄:22备注:ff11//objectarx文件处理方法//使用纯c++来处理文本文件,可以使用ifstream、ofstream以及iofstream,//在arx中由于强制使用unicode,应该使用arx提供的AcFStream-6.9684609.500076-2.258133-1063-6.9654089.503708-2.263412-1162-6.9674539.502518-2.261032-1135-6.9654089.511703-2.2597201020-6.9663549.508987-2.261917-1061-6.9672099.510147-2.264633-1179-6.9651349.511337-2.26219222-6.9670569.513962-2.265244-660-6.9633949.512955-2.264664825c++怎样安最后一列的正负把数据分开,分别存入俩个txt文件里? typedefstructtagData{floatf1;floatf2;floatf3;intn1;}SData;intmain(){FILE*fp=fopen("dota.txt","wb");if(NULL==fp){return;}//生成测试数据for(inti=0;i<100;i++){SDatastData={0.1+i,0.2+i,0.3+i,rand()-10000};fprintf(fp,"%f%f%f%d ",stData.f1,stData.f2,stData.f3,stData.n1);}fclose(fp);fp=NULL;FILE*fp2=fopen("dota.txt","rb");if(NULL==fp2){return;}for(inti=0;i<100;i++){SDatastData;fscanf(fp2,"%f%f%f%d ",&stData.f1,&stData.f2,&stData.f3,&stData.n1);printf("%f%f%f%d ",stData.f1,stData.f2,stData.f3,stData.n1);if(stData.n1>0){//写到文件A}else {//写到文件B}}fclose(fp2);fp2=NULL;}#include"stdafx.h"#include#include#includeusingnamespacestd;int_tmain(intargc,_TCHAR*argv[]){ifstreaminfiled("123.txt");ofstreamoutFile_negative("negative.txt"),outFile_positive("positive.txt");stringdata_total;//stringdata_positive,data_negative;stringdata_temp;intlen;for(data_total;getline(infiled,data_total);)//一行一行的读取{len=data_total.length();data_temp.assign(data_total,29,len-29);//Right(data_total,len-19);//取值if('-'==data_temp.at(0))//是负数{outFile_negative<#includeusingnamespacestd;intmain(){ifstreaminfile("data.txt",ios::in|ios::_Nocreate);if(infile.fail())cerr<<"OPENERROR!"<>a[i];cout<#include#include#includeusingnamespacestd;ofstreamfout("data_m.txt",ios::app); classA{public:A(stringname,intindex,intcount){m_name=name;m_index=index;m_count=count;}voidModify(intcount){m_count=count;}voidOutput(){cout<<"Nameis:"<>name;fin>>index;fin>>count; Aobj(name,index,count);obj.Output();if(count==100)obj.Modify(500);cout<<"修改后的结果:"<
此文档下载收益归作者所有
举报原因
联系方式
详细说明
内容无法转码请点击此处