I read a text file which includes "," separated words.
I read this file line by line.
After taking a line, i split it in to words by using split function. Here i use ',' for separate words from the line.
I defined a global vector so i thought i can store all the words in that vector.
Everything works fine until the vector come out from the split function.
Code works fine. But it dose not store the words in the vector.
At the bottom of the split function you can see the line I've written. cout<<evec.size()<<endl;
that line is to check the size of the vector line by line.
If the first line have 13 comma separated words it shows 13 (Which means it stored it)
But as soon as you get out from the split function, that data is no longer available. what can i do to store all words in this vector?
#include<iostream>
#include<string>
#include<stdio.h>
#include <fstream>
#include <vector>
using namespace std;
std::vector<string> evec;
void split( string st,vector<string> evec){
string z = " ";
for ( char s : st){
if ( s==','){
evec.push_back(z);
z = " ";
}
else{
z = z + s;
}
}
cout<<evec.size()<<endl;
}
int main( ){
string st;
ifstream Myfile("file.txt");
while(getline(Myfile, st))
{
split(st,evec);
}
cout<< evec.size() <<endl;
for (auto& it : evec) {
cout << it << endl;
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire