// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
typedef struct data{
int a;
int b;
};
void fkt(string str, int s, vector<data> vektor){
stringstream ss(str);
int s1;
ss>>s1;
vektor[s].a=s1;
}
void fktb(string str, int s, vector<data> vektor){
stringstream ss(str);
int s1;
ss>>s1;
vektor[s].b=s1;
}
void fkt3(int s, vector<string> vektor2, string str){
char *cstr, *p;
cstr=new char [str.size()+1];
strcpy(cstr, str.c_str());
p=strtok(cstr, ” \n”);
while (p!=NULL)
{
vektor2[s]=p;
p=strtok(NULL,” \n”);
cout<<vektor2[s]<<”;”;
}
}
int main () {
vector<data> vektor(10);
vector<string> vektor2(10);
string line;
ifstream myfile (“c:\\text.txt”);
int s=0;
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
string str1;
str1=line;
size_t pos;
pos = str1.find(” “)+1;
fkt(str1.substr(0,pos-1),s,vektor);
stringstream ss(str1.substr(0,pos-1));
int s1;
ss>>s1;
vektor[s].a=s1;
string str2;
str2=str1.substr(pos);
stringstream st(str2);
int s2;
st>>s2;
vektor[s].b=s2;
cout<<”vektor:”<<vektor[s].a<<” “<<vektor[s].b<<endl;
s++;
fkt3(s,vektor2,line);
}
myfile.close();
}
else cout << “Unable to open file”;
cin.get();
return 0;
}
Recent Comments