读写txt
读:ifstream 写:ofstream 读写:fstream
所以我们直接用fstream就行
文件打开方式
1 .ios::in 为了读文件而打开
2 .ios::out 为了写文件而打开
3 .ios::ate 初始位置 是文件末尾
4 .ios::app 以追加的方式写文件
5 .*ios::trunc 先删除文件的内容 再创建
6 .ios::binary 二进制方式
读txt
fs = 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 int
string 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__”)
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
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( ) ;
}