eg:
文本中每一行的数据结构,它们是以tab键为间隔的
Afghanistan Baghlan 36.12 68.7
Afghanistan Balkh 36.758 66.896
#include "stdafx.h"
#include "fstream"
#include<string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string strLine;
string strCountry,strCity,strLat,strLong;
ifstream file;
int ipos=0;
file.open("D://test//testfile//Debug//CityList.txt");
while(std::getline(file,strLine)) //利用全局函数读取文本中的每一行。
{
ipos = strLine.find('/t');
strCountry = strLine.substr(0,ipos);
strLine = strLine.substr(ipos+1);
ipos =strLine.find('/t');
strCity = strLine.substr(0,ipos);
strLine = strLine.substr(ipos+1);
ipos =strLine.find('/t');
strLat = strLine.substr(0,ipos);
strLong = strLine.substr(ipos+1);
cout<<strCountry<<"/t"<<strCity<<"/t"<<strLat<<"/t"<<strLong<<endl;
}
file.close();
system("pause");
return 0;
}
PS:
String.substr()
可用性
Flash Player 5。
用法
my_str.substr(start, [length])
参数
start 一个整数,指示 my_str 中用于创建子字符串的第一个字符的位置。如果 start 为一个负数,则起始位置从字符串的结尾开始确定,-1 表示最后一个字符。
length 要创建的子字符串中的字符数。如果没有指定 length,则子字符串包括从 start 开始直到字符串结尾的所有字符。
返回
指定字符串的子字符串。
说明
方法;返回字符串中的字符,这些字符从 start 参数所指定的索引开始,直至达到 length 参数所指定的字符数为止。substr 方法不更改由 my_str 指定的字符串,而是返回一个新字符串。