C/C++教程

C++读取文件,把字符串按“空格”分成若干个字符串

本文主要是介绍C++读取文件,把字符串按“空格”分成若干个字符串,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文件形如:

1341846313.592026 rgb/1341846313.592026.png 1341846313.592088 depth/1341846313.592088.png
1341846313.654184 rgb/1341846313.654184.png 1341846313.654212 depth/1341846313.654212.png
1341846313.686156 rgb/1341846313.686156.png 1341846313.686172 depth/1341846313.686172.png

代码

#include<iostream>  //cin  cout
#include<fstream>  //ifstring ofstream
#include<string>   //string
#include<sstream>  //istringstream
#include<vector>   //vector

using namespace std;
int main(int atgc, char **argv)
{
	ifstream fin;  //1、实例化一个ifstream对象
	fin.open(argv[1]);  //2、关联文件
	/*
	ifstream fin(filename);  //等同于1+2
	*/

	将把内容原样输出到屏幕**********************************************
	char ch;
	while(fin.get(ch)) 
	{
		cout<<ch;
	}
	fin.close();  //断开流与文件的连接,流还在
	cout<<endl;
	//**********************************************************************

	//fin 以空格,tab,换行 为结束标志读取****************************************
	fin.open(argv[1]);
	string buf;
	fin>>buf;  //读一个字符串,遇到空格就结束
	cout<<"buf = "<<buf<<endl;
	fin>>buf;  //读一个字符串,遇到空格就结束
	cout<<"buf = "<<buf<<endl;
	fin>>buf;  //读一个字符串,遇到空格就结束
	cout<<"buf = "<<buf<<endl;
	fin>>buf;  //读一个字符串,遇到空格就结束
	cout<<"buf = "<<buf<<endl;
	fin.get(ch);  //剩了个换行,读掉,扔了,如果是fin.get(ch),就给了ch
	cout<<ch;
	//**********************************************************************

	//getline(fin,VAR_string)读一行****************************************
	string line;
	getline(fin,line);  //读一行,从开始到行尾
	cout<<"line = "<<line<<endl;
	getline(fin,line);  //读一行,从开始到行尾
	cout<<"line = "<<line<<endl;
	cout<<"length of line = "<<line.length()<<endl<<endl;
	fin.close();
	//********************************************************************

	vector<string> str;
	string word;
	
	//把字符串按照空格分割成若干个
	//******************************************************************
	istringstream devide(line);
	while(devide>>word)
	{
		str.push_back(word);
	}
	//*******************************************************************
	vector<string>::iterator pd;  //迭代器
	for(pd=str.begin();pd!=str.end();pd++)
	{
		cout<<*pd<<endl;
	}
		

	ofstream fout;
	fout.open("write.txt");
	fout<<"下面是写入的内容:";


	fout.close();

	return 0;
}

输入形如

wfq@wfq-xiaoxin:~/MyProjects/CFiles/build$ ./file_read_write ../associations.txt
1341846313.592026 rgb/1341846313.592026.png 1341846313.592088 depth/1341846313.592088.png
1341846313.654184 rgb/1341846313.654184.png 1341846313.654212 depth/1341846313.654212.png
1341846313.686156 rgb/1341846313.686156.png 1341846313.686172 depth/1341846313.686172.png

buf = 1341846313.592026
buf = rgb/1341846313.592026.png
buf = 1341846313.592088
buf = depth/1341846313.592088.png

line = 1341846313.654184 rgb/1341846313.654184.png 1341846313.654212 depth/1341846313.654212.png
line = 1341846313.686156 rgb/1341846313.686156.png 1341846313.686172 depth/1341846313.686172.png
length of line = 89

1341846313.686156
rgb/1341846313.686156.png
1341846313.686172
depth/1341846313.686172.png
这篇关于C++读取文件,把字符串按“空格”分成若干个字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!