c对txt文件的读取与写入源代码

上传人:文*** 文档编号:62181119 上传时间:2022-03-14 格式:DOC 页数:11 大小:35.50KB
收藏 版权申诉 举报 下载
c对txt文件的读取与写入源代码_第1页
第1页 / 共11页
c对txt文件的读取与写入源代码_第2页
第2页 / 共11页
c对txt文件的读取与写入源代码_第3页
第3页 / 共11页
资源描述:

《c对txt文件的读取与写入源代码》由会员分享,可在线阅读,更多相关《c对txt文件的读取与写入源代码(11页珍藏版)》请在装配图网上搜索。

1、文档供参考,可复制、编制,期待您的好评与关注! c+对txt文件的读取与写入/* 这是自己写程序时突然用到这方面的技术,在网上搜了一下,特存此以备后用*/#include #include #include using namespace std;int main()char buffer256;ifstream myfile (c:a.txt);ofstream outfile(c:b.txt);if(!myfile) cout Unable to open myfile; exit(1); / terminate with errorif(!outfile) cout Unable to

2、open otfile; exit(1); / terminate with errorint a,b;int i=0,j=0;int data62; while (! myfile.eof() ) myfile.getline (buffer,10); sscanf(buffer,%d %d,&a,&b); couta bendl; datai0=a; datai1=b; i+; myfile.close(); for(int k=0;ki;k+) outfiledatak0 datak1endl; coutdatak0 datak1endl; outfile.close();return

3、0; 无论读写都要包含头文件读:从外部文件中将数据读到程序中来处理对于程序来说,是从外部读入数据,因此定义输入流,即定义输入流对象:ifsteam infile,infile就是输入流对象。这个对象当中存放即将从文件读入的数据流。假设有名字为myfile.txt的文件,存有两行数字数据,具体方法:int a,b;ifstream infile;infile.open(myfile.txt); /注意文件的路径infileab; /两行数据可以连续读出到变量里infile.close()如果是个很大的多行存储的文本型文件可以这么读:char buf1024; /临时保存读取出来的文件内容stri

4、ng message;ifstream infile;infile.open(myfile.js);if(infile.is_open() /文件打开成功,说明曾经写入过东西while(infile.good() & !infile.eof() memset(buf,0,1024); infile.getline(buf,1204); message = buf; . /这里可能对message做一些操作 coutmessageendl;infile.close();写:将程序中处理后的数据写到文件当中对程序来说是将数据写出去,即数据离开程序,因此定义输出流对象ofstream outfile

5、,outfile就是输出流对象,这个对象用来存放将要写到文件当中的数据。具体做法:ofstream outfile;outfile.open(myfile.bat); /myfile.bat是存放数据的文件名if(outfile.is_open()outfilemessageendl; /message是程序中处理的数据 outfile.close();else cout不能打开文件!endl;c+对文件的读写操作的例子/*/从键盘读入一行字符,把其中的字母依次放在磁盘文件fa2.dat中,再把它从磁盘文件读入程序,将其中的小写字母改成大写字母,再存入磁盘fa3.dat中*/i ncludei

6、 ncludei ncludeusing namespace std;/从键盘上读取字符的函数void read_save() char c80; ofstream outfile(f1.dat);/以输出方工打开文件 if(!outfile) cerropen error!=65&ci=97&ci=122)/保证输入的字符是字符 outfile.put(ci);/将字母字符存入磁盘文件 coutci; coutendl; outfile.close(); void creat_data() char ch; ifstream infile(f1.dat,ios:in);/以输入的方式打开文件

7、 if(!infile) cerropen error!endl; exit(1); ofstream outfile(f3.dat);/定义输出流f3.dat文件 if(!outfile) cerropen error!endl; exit(1); while(infile.get(ch)/当读取字符成功时 if(ch=97) ch=ch-32; outfile.put(ch); coutch; coutendl; infile.close(); outfile.close(); int main() read_save(); creat_data(); system(pause); ret

8、urn 0; / win32console.cpp : 定义控制台应用程序的入口点。 /C+读取txt文件的方法 #include stdafx.h #include #include using namespace std; typedef struct node int data; struct node *next; node; node *creat(ifstream &ifp) node *h=NULL,*p=NULL,*q=NULL; int data; while (ifpdata) p=new node; p-data=data; p-next=NULL; if (!h) h=

9、p; else q-next=p; q=p; return h; void prt(node *h) if (h) coutdatanext); int _tmain(int argc, _TCHAR* argv) /-file1 open ifstream fp(d:data.txt,ifstream:in); node *hst=creat(fp); fp.close(); prt(hst); /-file2 open ifstream r(d:test.txt,ifstream:in); if(!r) cout打开文件出错!line) coutlineq; return 0; /- /d

10、:data.txt 内容 /1 2 3 4 5 6 /7 8 9 10 11 12 /100998877 /55 56 57 58 59 60 /d:test.txt 内容 /姓名:hehe年龄:12备注:123 /姓名:haihai年龄:18备注:12ff /姓名:aa年龄:22备注:ff11 /objectarx 文件处理方法 /使用纯c+来处理文本文件,可以使用ifstream、ofstream以及iofstream, /在arx中由于强制使用unicode,应该使用arx提供的AcFStream -6.968460 9.500076 -2.258133 -1063-6.965408 9

11、.503708 -2.263412 -1162-6.967453 9.502518 -2.261032 -1135-6.965408 9.511703 -2.259720 1020-6.966354 9.508987 -2.261917 -1061-6.967209 9.510147 -2.264633 -1179-6.965134 9.511337 -2.262192 22-6.967056 9.513962 -2.265244 -660-6.963394 9.512955 -2.264664 825c+怎样安最后一列的正负把数据分开,分别存入俩个txt文件里?typedef struct

12、tagData float f1; float f2; float f3; int n1;SData;int main() FILE *fp = fopen(dota.txt, wb); if (NULL = fp) return; / 生成测试数据 for (int i=0; i 100; i+) SData stData = 0.1 + i, 0.2 + i, 0.3 + i, rand() - 10000; fprintf(fp, %f %f %f %dn, stData.f1, stData.f2, stData.f3, stData.n1); fclose(fp); fp = NUL

13、L; FILE *fp2 = fopen(dota.txt, rb); if (NULL = fp2) return; for (int i=0; i 0) / 写到文件A else / 写到文件B fclose(fp2); fp2 = NULL;#include stdafx.h#include #include #include using namespace std;int _tmain(int argc, _TCHAR* argv)ifstream infiled(123.txt);ofstream outFile_negative(negative.txt),outFile_posi

14、tive(positive.txt);string data_total;/string data_positive,data_negative;string data_temp;int len;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_negativedata_totalendl;/写

15、入文件elseoutFile_positivedata_totalendl;return 0;C+举个例子假若有一个文本文件data.txt!我要将文本里面的数据输进去程序!是读取文本,不是手工输入,可以有什么方法,用什么函数?#include#includeusing namespace std;int main()ifstream infile(data.txt,ios:in|ios:_Nocreate);if(infile.fail()cerrOPEN ERROR!endl;int a10 = 0;for(int i = 0;i ai;coutai ;infile.close();cou

16、tendl;return 0;1.txt里数据liming 1 200xiaoqiang 2 300xiaozhu 3 100xiaokai 4 600然后进行修改 比如将100改为50有人建议是先放入string 然后再放到txt里不知道怎么写 高分悬赏 方法不限 #include#include#include#includeusing namespace std;ofstream fout(data_m.txt,ios:app);class Apublic:A(string name,int index,int count)m_name=name;m_index=index;m_coun

17、t=count;void Modify(int count)m_count=count;void Output()coutName is:m_name ;Index is:m_index ; Count is:m_countendl;void Write()foutm_name m_index m_countendl;private:string m_name;int m_index;int m_count;int _tmain(int argc, _TCHAR* argv)ifstream fin(data.txt);if(!fin)coutOpen file error!name;finindex;fincount;A obj(name,index,count);obj.Output();if(count=100)obj.Modify(500);cout修改后的结果:endl;obj.Output();obj.Write();return 0;data.txt的数据为:liming 1 200xiaoqiang 2 300xiaozhu 3 100xiaokai 4 600最终实现把100修改成500data_m为修改后的文件11 / 11

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!