C++读文件行+分割字符串
时间:2010-07-28 来源:axejonny
还是用到了万恶的strtok
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main()
{
ifstream f("x.cfg");
string str;
while(getline(f,str))
{
if(str[0]=='[')
cout<<str<<endl;
else
{
const char* needle="=";
char* ccc=const_cast<char*>(str.c_str());
char* c=strtok(ccc, needle);
if(c)
cout<<"F:"<<c<<" ";
c = strtok(NULL, needle);
if(c)
cout<<"R:"<<c<<endl;
}
}
return 0;
}
#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <string.h>
#include <stdlib.h>
using namespace std;
int main()
{
ifstream f("x.cfg");
string str;
while(getline(f,str))
{
if(str[0]=='[')
cout<<str<<endl;
else
{
const char* needle="=";
char* ccc=const_cast<char*>(str.c_str());
char* c=strtok(ccc, needle);
if(c)
cout<<"F:"<<c<<" ";
c = strtok(NULL, needle);
if(c)
cout<<"R:"<<c<<endl;
}
}
return 0;
}
相关阅读 更多 +