txt文件中每一行的内容格式为 dsadas:da!hfg:1
dasrger、gefew:2
uiorew:3
读取同时txt文件的内容,并提取每行末尾的数字
思路是,先使用rfind找到“:”的位置n
再用substr函数获取n+1处的数字
// strTest1.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"
#include <fstream>
#include<string>
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream fin("test.txt");
string line;
string temp;
int n=0;
if(!fin.is_open())
{
cout<<"不能打开文件"<<endl;
return 1;
}
while(getline(fin,line))
{
n=line.rfind(':');
temp=line.substr(n+1,n+2);
cout<<temp<<endl;
}
// getchar();
return 0;
}