C读取文件
C读取文件,这种写法不会多一行。
1 #include "stdafx.h" 2 #include <vector> 3 using namespace std; 4 struct PointXYZ 5 { 6 double X; 7 double Y; 8 double Z; 9 }; 10 11 int _tmain(int argc, _TCHAR* argv[]) 12 { 13 FILE* in=fopen("D:\\project60-cut-1000.txt","r"); 14 if(in==NULL) 15 { 16 printf("missing file"); 17 return 0; 18 } 19 double tmp=0; 20 //char buff[255] = {}; 21 float x; 22 float y; 23 float z; 24 vector<PointXYZ> *points=new vector<PointXYZ>(); 25 char* str; 26 int i=0; 27 // while(!feof(in)) 28 fscanf(in,"%f %f %f",&x,&y,&z); 29 while(feof(in)==0) /*判断是否文件尾,不是则循环*/ 30 { 31 i++; 32 PointXYZ point; 33 point.X=x; 34 point.Y=y; 35 point.Z=z; 36 points->push_back(point); 37 fscanf(in,"%f %f %f",&x,&y,&z); 38 } 39 fclose(in); 40 41 for (vector<PointXYZ>::iterator iter = points->begin(); iter != points->end(); ++iter) 42 { 43 PointXYZ tmp=(PointXYZ)*iter; 44 printf("%f %f %f\n",tmp.X,tmp.Y,tmp.Z); 45 } 46 printf("Number:%d\n",(int)points->size()); 47 system("pause");//getchar(); 48 return 0; 49 }
参考C++的读取
1 void LoadImages(const string &strAssociationFilename, vector<string> &vstrImageFilenamesRGB, 2 vector<string> &vstrImageFilenamesD, vector<double> &vTimestamps) 3 { 4 ifstream fAssociation; 5 fAssociation.open(strAssociationFilename.c_str()); 6 while(!fAssociation.eof()) 7 { 8 string s; 9 getline(fAssociation,s); 10 if(!s.empty()) 11 { 12 stringstream ss; 13 ss << s; 14 double t; 15 string sRGB, sD; 16 ss >> t; 17 vTimestamps.push_back(t); 18 ss >> sRGB; 19 vstrImageFilenamesRGB.push_back(sRGB); 20 ss >> t; 21 ss >> sD; 22 vstrImageFilenamesD.push_back(sD); 23 24 } 25 } 26 }
作者:太一吾鱼水
文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。
欢迎大家留言交流,转载请注明出处。
分类:
编程开发集合 / C++/STL
标签:
File
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2014-01-11 使用GitHub开源一个项目RGEOS