I am required to read contents of a file and then modify it and then store it on vectors for easy manipulation. But unfortunately, Push_back cannot assign a string variable. I have looked for another solution online, nothing matches mine.
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
// Initializing variables and vectors
size_t pos;
string last_name;
string first_name;
string number{};
string full_name{};
string line{};
ifstream myfile ("employees.dat");
vector<string > number_vector;
vector<string> last_name_vector;
vector<string> first_name_vector;
// Reading File Contents:
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);
// Extracting Numbers from Lines
for (size_t i {0}; i < line.length(); ++i){
if (line[i] == '#'){
line.clear();
}
else if (isdigit (line[i]))
number += line[i];
// Extracting Full Name
else if (isalpha(line[i]) || line[i] == ' ')
full_name += line[i];
}
number_vector.push_back(full_name);
cout<< (number_vector[0]);
}
myfile.close();
}
else cout << "Unable to open file"<<endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire