点云数据处理
tob_id_3208
cout<<data[k][0] <<" "<<data[k][1]<<endl; } out(); return 0; }无论读写都要包含<fstream>头文件读:从外部文件中将数据读到程序中来处理对于程序来说,是从外部读入数据,因此定义输入流,即定义输入流对象:ifsteam in就是输入流对象。这个对象当中存放即将从文件读入的数据流。假设有名字为my的文件,存有两行数字数据,具体方法:int a,b; ifstream infile; in("my"); //注意文件的路径infile>>a>>b; //两行数据可以连续读出到变量里in()如果是个很大的多行存储的文本型文件可以这么读:char buf[1024]; //临时保存读取出来的文件内容string message; ifstream infile; in("my"); if(in()) //文件打开成功,说明曾经写入过东西{ while(in() && !in()) { memset(buf,0,1024); in(buf,1204); message = buf; ...... //这里可能对message做一些操作cout<<message<<endl; } in(); }写:将程序中处理后的数据写到文件当中对程序来说是将数据写出去,即数据离开程序,因此定义输出流对象ofstream out就是输出流对象,这个对象用来存放将要写到文件当中的数据。具体做法:ofstream outfile;
c++对txt文件的读取与写入/*这是自己写程序时突然用到这方面的技术,在网上搜了一下,特存此以备后用~
*/ #include <iostream>
#include <iomanip>
#include <fstream>
using namespace std; i
nt main(){ char buffer[256];
out("my"); //my是存放数据的文件名if(out()) { outfile<<message<<endl; //message是程序中处理的数据out(); } 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]; ofstream outfile("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){//保证输入的字符是字符out(c[i]);//将字母字符存入磁盘文件cout<<c[i]<<""; } cout<<endl; out(); } void creat_data(){ char ch; ifstream infile("f1.dat",ios::in);//以输入的方式打开文件if(!infile){ cerr<<"open error!"<<endl; exit(1);
ifstream myfile ("c:\\a.txt");
ofstream outfile("c:\\b.txt");
if(!myfile){ cout << "Unable to open myfile";
exit(1); // terminate with error }
if(!outfile){
} ofstream outfile("f3.dat");//定义输出流f3.dat文件if(!outfile){ cerr<<"open error!"<<endl; exit(1); } while(in(ch)){//当读取字符成功时if(ch<=122&&ch>=97) ch=ch-32;out(ch); cout<<ch; } cout<<endl; in(); out(); } int main(){ read_save(); creat_data(); system("pause"); return 0; } // win32console.cpp :定义控制台应用程序的入口点。//C++读取txt文件的方法#include "stdafx.h" #include <fstream> #include <iostream> using namespace std; typedef struct node{ int data; struct node *next;} &ifp)
cout << "Unable to open otfile";
exit(1); // terminate with error } int a,b; int i=0,j=0; int data[6][2]; while (! my() ) { my (buffer,10); sscanf(buffer,"%d %d",&a,&b); cout<<a<<" "<<b<<endl; data[i][0]=a; data[i][1]=b; i++; } my(); for(int k=0;k<i;k++){ outfile<<data[k][0] <<" "<<data[k][1]<<endl;