c++对txt文件的读取与写入/* 这是自己写程序时突然用到这方面的技术,在网上搜了一下,特存此以备后用~*/ #include <iostream>#include <iomanip>#include <fstream>using namespace std; int main(){ char buffer[256];ifstreammyfile ("c:\\a.txt");ofstreamoutfile("c:\\b.txt");if(!myfile){ cout<< "Unable to open myfile";exit(1); // terminate with error }if(!outfile){cout<< "Unable to open otfile";exit(1); // terminate with error } inta,b; int i=0,j=0; int data[6][2]; while (! myfile.eof() ) { myfile.getline (buffer,10); sscanf(buffer,"%d %d",&a,&b); cout<<a<<" "<<b<<endl; data[i][0]=a; data[i][1]=b; i++; } myfile.close(); for(int k=0;k<i;k++){ outfile<<data[k][0] <<" "<<data[k][1]<<endl;tob_id_3208cout<<data[k][0] <<" "<<data[k][1]<<endl; } outfile.close(); return 0; } 无论读写都要包含<fstream>头文件读:从外部文件中将数据读到程序中来处理对于程序来说,是从外部读入数据,因此定义输入流,即定义输入流对象:ifsteaminfile,infile就是输入流对象。
这个对象当中存放即将从文件读入的数据流。
假设有名字为myfile.txt的文件,存有两行数字数据,具体方法:inta,b; ifstreaminfile; infile.open("myfile.txt"); //注意文件的路径infile>>a>>b; //两行数据可以连续读出到变量里infile.close() 如果是个很大的多行存储的文本型文件可以这么读:char buf[1024]; //临时保存读取出来的文件内容string message; 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<<message<<endl; } infile.close(); } 写:将程序中处理后的数据写到文件当中对程序来说是将数据写出去,即数据离开程序,因此定义输出流对象ofstreamoutfile,outfile就是输出流对象,这个对象用来存放将要写到文件当中的数据。
具体做法:ofstreamoutfile;outfile.open("myfile.bat"); //myfile.bat是存放数据的文件名if(outfile.is_open()) { outfile<<message<<endl; //message是程序中处理的数据outfile.close(); } else { cout<<"不能打开文件!"<<endl; } c++对文件的读写操作的例子/*/从键盘读入一行字符,把其中的字母依次放在磁盘文件fa2.dat中,再把它从磁盘文件读入程序,将其中的小写字母改成大写字母,再存入磁盘fa3.dat中*/ #i nclude<fstream>#i nclude<iostream>#i nclude<cmath> using namespace std; //////////////从键盘上读取字符的函数void read_save(){ char c[80]; ofstreamoutfile("f1.dat");//以输出方工打开文件if(!outfile){ cerr<<"open error!"<<endl;//注意是用的是cerr exit(1); } cin.getline(c,80);//从键盘读入一行字符for(int i=0;c[i]!=0;i++) //对字符一个一个的处理,直到遇到'/0'为止if(c[i]>=65&&c[i]<=90||c[i]>=97&&c[i]<=122){//保证输入的字符是字符outfile.put(c[i]);//将字母字符存入磁盘文件cout<<c[i]<<""; } cout<<endl; outfile.close(); } void creat_data(){ char ch; ifstreaminfile("f1.dat",ios::in);//以输入的方式打开文件if(!infile){ cerr<<"open error!"<<endl; exit(1);} ofstreamoutfile("f3.dat");//定义输出流f3.dat文件if(!outfile){ cerr<<"open error!"<<endl; exit(1); } while(infile.get(ch)){//当读取字符成功时if(ch<=122&&ch>=97) ch=ch-32; outfile.put(ch); cout<<ch; } cout<<endl; infile.close(); outfile.close(); } int main(){ read_save(); creat_data(); system("pause"); return 0; } // win32console.cpp : 定义控制台应用程序的入口点。
//C++读取txt文件的方法#include "stdafx.h" #include <fstream> #include <iostream> using namespace std; typedefstruct node{ int data; struct node *next; } node; node *creat(ifstream&ifp){ node *h=NULL,*p=NULL,*q=NULL; int data; while (ifp>>data) { p=new node; p->data=data; p->next=NULL; if (!h) h=p; else q->next=p; q=p; } return h; } void prt(node *h) { if (h) { cout<<h->data<<endl; prt(h->next); } } int _tmain(intargc, _TCHAR* argv[]) { //--file1 open ifstreamfp("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<<"打开文件出错!"<<endl; } else { char line[4000]; //[100]2000 while(r>>line) { cout<<line<<endl; } r.close();函数用于读取.xyz的点云文件,点云的格式为:[cpp] view plain copy 在CODE上查看代码片派生到我的代码片17.371559 -6.531680 -8.080792 0.242422 0.419118 0.87497015.640106 -16.101347 -9.550241 -0.543610 -0.382877 0.74692217.750742 -6.395478 -8.307115 0.333093 0.494766 0.80265515.432834 -15.947010 -9.587061 -0.548083 -0.385148 0.74247323.626318 -7.729815 -13.608750 0.081697 0.502976 0.86043115.300377 -15.610346 -9.547507 -0.569658 -0.341132 0.74774323.975805 -7.512131 -13.775388 0.082388 0.564137 0.82156124.251831 -7.345085 -13.949208 0.099309 0.574142 0.81271114.999881 -15.463743 -9.748975 -0.629676 -0.333713 0.70153014.804974 -15.162496 -9.758424 -0.616575 -0.334426 0.71273727.607445 -6.731058 -16.160894 0.387612 0.713240 0.58399114.560967 -14.955154 -9.909436 -0.638394 -0.335827 0.69258427.938255 -6.707172 -16.443462 0.379390 0.740941 0.55413914.290791 -14.852806 -10.137550 -0.692395 -0.381273 0.61255214.386531 -15.114174 -10.178914 -0.719801 -0.337913 0.60638414.001437 -14.247000 -10.103112 -0.735267 -0.343587 0.58423513.762934 -13.909647 -10.200064 -0.752330 -0.295280 0.588906前面3个数字是坐标,后面3个数字是法向量,有多少行就代表有多少个点。