读写txt

读:ifstream  写:ofstream   读写:fstream
所以我们直接用fstream就行
文件打开方式1.ios::in  为了读文件而打开2.ios::out 为了写文件而打开3.ios::ate  初始位置 是文件末尾4.ios::app 以追加的方式写文件5.*ios::trunc 先删除文件的内容 再创建6.ios::binary 二进制方式#include<fstream>
读txtfs=fstream("abc.txt",ios::in);
string abc;
getline(fs,abc);写txt 二进制
fs=fstream("abc.txt",ios::out|ios::binary);
fs<<"hello world"<<endl;

常用的转换


stof(string) string to float
stod(string) string to double
stoi(string) string to intstring to char[]
string.c_str()char[] to str
char[] a;
string ls=a;

cpp的split和strip

split()//分割函数,根据spacer分割str,存储至v中
void split(string str,vector<string> &v,string spacer)
{int pos1,pos2;int len=spacer.length();     //记录分隔符的长度pos1=0;pos2=str.find(spacer);while( pos2 != string::npos ){v.push_back( str.substr(pos1,pos2-pos1) );pos1 = pos2 + len;pos2 = str.find(spacer,pos1);    // 从str的pos1位置开始搜寻spacer}if(pos1 != str.length()) //分割最后一个部分v.push_back(str.substr(pos1));
}strip()//去除两边符合要求字符串的函数
比如 strip(“__abc__”)
#include <string>
#include <cctype>
std::string strip(const std::string &inpt,const std::string &spacer)
{auto start_it = inpt.begin();auto end_it = inpt.rbegin();while (*start_it==spacer)++start_it;while (*end_it==spacer)++end_it;return std::string(start_it, end_it.base());
}

如何直接读取最后一行? seekg peek getline

通过file.seekg()和file.peek() 可以定位到最后一行file.seekg(1,ios::in);//光标至第二个字符file.seekg(-2,ios::end);//光标至倒数第二个字符file.seekg(1,ios::cur)//光标前进1个字符file.peek() //得到光标的当前字符,但不移动光标file.getline()//光标至下一个“/n”的文本,光标前进至“/n”之后通过file.seekg()和file.peek() 可以定位到最后一行
逻辑为:file.seekg(-2, ios::end); //到倒数第二个字符 即最后一行的”/n“之前while (file.peek() != '\n'){file.seekg(-1, ios::cur);//一直往前进,直至倒数第二行的“/n”}file.seekg(1,ios::cur);//往后前进一个,至最后一行的第一个字符string cur_viewport;getline(file,cur_viewport);//getline() 得到最后一行

code

#include <fstream>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
void split(string str,vector<string> &v,string spacer)
{int pos1,pos2;int len=spacer.length();     //记录分隔符的长度pos1=0;pos2=str.find(spacer);while( pos2 != string::npos ){v.push_back( str.substr(pos1,pos2-pos1) );pos1 = pos2 + len;pos2 = str.find(spacer,pos1);    // 从str的pos1位置开始搜寻spacer}if(pos1 != str.length()) //分割最后一个部分v.push_back(str.substr(pos1));
}
void get_param(string line,float& b0,float& b1)
{vector<string> vec;split(line,vec," ");b0=stof(vec[0]);b1=stof(vec[1]);}
int main()
{//读取最后一次的视口fstream file ("traj.txt", ios::in);file.seekg(-2, ios::end);while (file.peek() != '\n'){file.seekg(-1, ios::cur);}file.seekg(1,ios::cur);string cur_viewport;getline(file,cur_viewport);vector<string> cur_viewport_vec;split(cur_viewport,cur_viewport_vec," ");string date=cur_viewport_vec[0];string time_cur=cur_viewport_vec[1];float x=stof(cur_viewport_vec[2]);float y=stof(cur_viewport_vec[3]);float z=stof(cur_viewport_vec[4]); float yaw=stof(cur_viewport_vec[5]);float pitch=stof(cur_viewport_vec[6]);float roll=stof(cur_viewport_vec[7]);file.close();// 读取参数string line;fstream model_file ("model.txt", ios::in);float x_b0,x_b1;getline(model_file,line);get_param(line,x_b0,x_b1);float y_b0,y_b1;getline(model_file,line);get_param(line,y_b0,y_b1);float z_b0,z_b1;getline(model_file,line);get_param(line,z_b0,z_b1);float yaw_b0,yaw_b1;getline(model_file,line);get_param(line,yaw_b0,yaw_b1);float pitch_b0,pitch_b1;getline(model_file,line);get_param(line,pitch_b0,pitch_b1);float roll_b0,roll_b1;getline(model_file,line);get_param(line,roll_b0,roll_b1);model_file.close();ofstream ofile;ofile.open("predict.txt");float x_next=x,y_next=y,z_next=z,yaw_next=yaw,pitch_next=pitch,roll_next=roll;for(int i=0;i<30;i++){x_next=x_b0+x_b1*x_next;y_next=y_b0+y_b1*y_next;z_next=z_b0+z_b1*z_next;yaw_next=yaw_b0+yaw_b1*yaw_next;pitch_next=pitch_b0+pitch_b1*pitch_next;roll_next=roll_b0+roll_b1*roll_next;ofile<<x_next<<" "<<y_next<<" "<<z_next<<" "<<yaw_next<<" "<<pitch_next<<" "<<roll_next<<endl;}ofile.close();}

cpp 读取txt文件相关推荐

  1. C++实现分割读取txt文件以及对齐打印设置

    假设现在有txt文件如下,需要将里面每一项的内容分别读取并存储,以读取文件中的FrameNo.Y_bits以及Y_psnr三个变量为例进行分析. txt文件下载:点击下载txt文件 完整C++程序如下 ...

  2. C++按列读取txt文件并保存,替代excel处理

    C++按列读取txt文件并保存,替代excel处理 问题描述 原内容为txt格式,每行数据以"逗号"为分割. 目标为提取其中4.5列,单独以"空格"为分割存储到 ...

  3. C语言如何读取txt文件(最新免费代码)

    C语言如何读取txt文件(最新免费代码) 运行效果展示 第一步添加头文件 写函数 完整的代码 运行效果展示 第一步添加头文件 #include <stdio.h> 写函数 使用的API是 ...

  4. load python txt文件_详解Python中numpy.loadtxt()读取txt文件

    为了方便使用和记忆,有时候我们会把 numpy.loadtxt() 缩写成np.loadtxt() ,本篇文章主要讲解用它来读取txt文件. 读取txt文件我们通常使用 numpy 中的 loadtx ...

  5. python将二维列表内容写入和读取.txt文件

    python读取txt文件至列表当中 首先看txt文件中内容. 上代码: readlines() 方法用于读取所有行(直到结束符 EOF)并返回列表 每一行的内容为列表种的一个元素 我们通过循环将内容 ...

  6. java读取txt文件

    java如何读入txt文本文件的内容:java从txt文件中读取内容有多种方法,包括按照行读取文件,按照字节读取文件,首先我们来看看按照行读取txt文件中的内容的一般的步骤: 首先是我们创建一个fil ...

  7. python删除重复值所在的行数_使用python读取txt文件的内容,并删除重复的行数方法...

    注意,本文代码是使用在txt文档上,同时txt文档中的内容每一行代表的是图片的名字. #coding:utf-8 import shutil readDir = "原文件绝对路经" ...

  8. python读取txt文件并画图

    1,使用python读取txt文件 已知txt文件内容如下: 0 01 12 43 94 165 256 36 请以第一列为x轴,第二列为y轴画图 步骤如下: 1)使用readlines读取文件 2) ...

  9. python接口自动化参数化_Python读取txt文件数据的方法(用于接口自动化参数化数据)...

    小试牛刀: 1.需要python如何读取文件 2.需要python操作list 3.需要使用split()对字符串进行分割 代码运行截图 : 代码(copy) #encoding=utf-8 #1.r ...

最新文章

  1. 简单借还书管理系统c语言,急求程序!!!简单图书馆借/还书管理子系统
  2. aspnetpager分页,不使用存储过程
  3. SANS:2012年度日志管理调查报告
  4. Consul 服务注册与发现02—— 服务提供者
  5. 【计网】IP地址、子网掩码、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
  6. POJ1151(矩形切割入门题)
  7. window.navigator.userAgent用来区分设备和浏览器
  8. activex java 控件_java 如何调用ActiveX控件??
  9. 蓝桥杯第八届省赛JAVA真题----拉马车
  10. Linux 建立文件夹的链接
  11. JDK8之后的 接口的新特性:
  12. Linux系统原理(工作模式)
  13. linux内核模块的程序结构
  14. 解决微信小程序安卓手机访问不到图片,无法显示图片
  15. 数据分析学习笔记1---zip(),numpy.where
  16. GUI实现学生点名系统
  17. cox回归模型python实现_cox回归模型python实现_生存分析Cox回归模型(比例风险模型)的spss操作实例...
  18. 一壶浊酒尽余欢、今宵别梦寒!
  19. 《算法导论3rd第一章》算法在计算中的作用
  20. Flask源码阅读(六)——Flash消息

热门文章

  1. 怎么在一张图片中隐藏文件?
  2. 搬砖的成长之路——VMWare网络原理
  3. Educational Codeforces Round 126 (Rated for Div. 2)(A-E)
  4. Electron 中的 webview 实战 —— 手写简易浏览器
  5. 喜茶奶茶技术培训哪里学?
  6. spot卫星介绍与数据下载
  7. 怎么依靠网络赚钱,网上可以做什么副业
  8. 机器学习数据的预处理
  9. 程序员不能只会敲代码还要会投资理财
  10. 歌对华为使用安卓设限,但物联网操作系统还有更大机会