64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main ()
{
string file("go.txt");//txt文件。
ifstream input(file.c_str());
vector<double> val;
char ch;
double dval = 0;
for(int i= 0; i != 5; ++i)//此循环使文件指针指向“/”后,也就是跳过goto/部分
input >> ch;
while (input >> dval,true)//读入每个变量至 double型 dval中,并通过dval读入vector<double> val容器中。
{
if(input.bad())
throw std::runtime_error("IO stream corrupted");
if(input.fail())
{
if(input.eof())break;
input.clear();
input.ignore(1,',');
continue;
}
val.push_back(dval);
}
input.clear();
for(vector<double>::const_iterator it = val.begin(); it != val.end() ; ++it)//输出容器中的变量。
cout << *it << ' ';
cout << endl;
return 0;
}
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void GetData(string *p_array)
{
ifstream in("test.txt");//你那文件的路径
if(!in)
{
exit(0);
}
string str_file_content,str_content,str_type,addressLine;
while( getline(in, str_file_content) )
{
int index = str_file_content.find_first_of('/');
str_type = str_file_content.substr(0,index);
str_content = str_file_content.substr(index+1, str_file_content.length());
if ("GOTO" == str_type)
{
index = str_content.find_first_of(',');
while(string::npos != index)
{
*p_array = str_content.substr(0, index);
str_content = str_content.substr(index+1, str_content.length());
index = str_content.find_first_of(',');
++p_array;
}
*p_array = str_content;
}
}
}
int main(void)
{
string test[6];
GetData(test);
for(int i = 0;i <6; ++i)
cout<<test[i]<<endl;
system("pause");
return 0;
}