精品文档
精品文档 c++对txt文件的读取与写入 /* 这是自己写程序时突然用到这方面的技术,在网上搜了一下,特存此以备后用~
*/ #include
#include
#include
using namespace std; i
nt main(){ char buffer[256];
ifstream myfile ("c:\\a.txt");
ofstream outfile("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 } int a,b; int i=0,j=0; int data[6][2]; while (!
myfile.eof() ) { myfile.getline (buffer,10); sscanf(buffer,"%d %d",&a,&b);
cout<
for(int k=0;k
tob_id_3208
cout<头文件 读:从外部文件中将数据读到程序中来处理 对于程序来说,是从外部读入数据,因此定义输入流,即定义输入流对象:ifsteam infile,infile就是输入流对象。 这个对象当中存放即将从文件读入的数据流。假设有名字为myfile.txt的文件,存有两行数字数据,具体方法: int a,b; ifstream infile; infile.open("myfile.txt"); //注意文件的路径 infile>>a>>b; //两行数据可以连续读出到变量里
infile.close() 如果是个很大的多行存储的文本型文件可以这么读: char buf[1024];
//临时保存读取出来的文件内容 string 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做一些操作
cout<
outfile.open("myfile.bat"); //myfile.bat是存放数据的文件名 if(outfile.is_open())
{ outfile<
{ cout<<"不能打开文件!"< #i
nclude #i nclude using namespace std; //////////////从键盘上读取字符的函数 void read_save(){ char c[80]; ofstream outfile("f1.dat");//以输出方工打开文件 if(!outfile){ cerr<<"open error!"<=65&&c[i]<=90||c[i]>=97&&c[i]<=122){//保证输入的字符精品文档
精品文档 是字符 outfile.put(c[i]);//将字母字符存入磁盘文件
cout<
outfile.close(); } void creat_data(){ char ch; ifstream
infile("f1.dat",ios::in);//以输入的方式打开文件
if(!infile){ cerr<<"open error!"<
} ofstream outfile("f3.dat");//定义输出流f3.dat文件
if(!outfile){ cerr<<"open error!"<
exit(1); } while(infile.get(ch)){//当读取字符成功时
if(ch<=122&&ch>=97) ch=ch-32; outfile.put(ch); cout<
cout<
main(){ read_save(); creat_data(); system("pause");
return 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 (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 prt(h->next); } } 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<<"打开文件出错!"< { char line[4000]; //[100]2000 while(r>>line) { cout< 精品文档 精品文档 函数用于读取.xyz的点云文件,点云的格式为: [cpp] view plain copy 在CODE上查看代码片派生到我的代码片 17.371559 -6.531680 -8.080792 0.242422 0.419118 0.874970 15.640106 -16.101347 -9.550241 -0.543610 -0.382877 0.746922 17.750742 -6.395478 -8.307115 0.333093 0.494766 0.802655 15.432834 -15.947010 -9.587061 -0.548083 -0.385148 0.742473 23.626318 -7.729815 -13.608750 0.081697 0.502976 0.860431 15.300377 -15.610346 -9.547507 -0.569658 -0.341132 0.747743 23.975805 -7.512131 -13.775388 0.082388 0.564137 0.821561 24.251831 -7.345085 -13.949208 0.099309 0.574142 0.812711 14.999881 -15.463743 -9.748975 -0.629676 -0.333713 0.701530 14.804974 -15.162496 -9.758424 -0.616575 -0.334426 0.712737 27.607445 -6.731058 -16.160894 0.387612 0.713240 0.583991 14.560967 -14.955154 -9.909436 -0.638394 -0.335827 0.692584 27.938255 -6.707172 -16.443462 0.379390 0.740941 0.554139 14.290791 -14.852806 -10.137550 -0.692395 -0.381273 0.612552 14.386531 -15.114174 -10.178914 -0.719801 -0.337913 0.606384 14.001437 -14.247000 -10.103112 -0.735267 -0.343587 0.584235 13.762934 -13.909647 -10.200064 -0.752330 -0.295280 0.588906