ifstream读文件时,区分以“,”为分隔符的字符串
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string file_path= "D:\\###\\###\\mac.txt";
string mac_;
ifstream ifs;
ifs.open(file_path, ios::in);
if(ifs.is_open())
{
while(getline(ifs,mac_))
{
// 输出每行总字符串
cout<<mac_<<endl;
//记录逗号出现的位置
long pos= mac_.find(",",0);
cout<<"逗号出现的位置:"<<pos<<endl;
// 记录字符串的总长度
int mac_size= mac_.size();
cout<<"mac_的总长度:"<<mac_size<<endl;
// 以逗号为分隔符 分别输出前后字符串
cout<< mac_.substr(0,pos)<<"------"<<mac_.substr(pos+1, mac_size-pos-1)<<endl;
cout<<endl;
}
}
system("pause");
return 0;
}
特别注意:string.substr()的用法,第一个参数为 以0为基准的索引值,第二个参数为 从改值开始的后 n 个数字。