1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
#include<iostream>
#include<string>
#include<cstring>
#include<fstream>
char** wrs_lst(char *fn);
char** createMtx(int n, int m);
void deleteMtx(char** mtx, int n, int m);
int findNthOccur(std::string str, char ch, int N);
int main(){
std::string s = "authors.txt";
int l = s.length();
char arr[l + 1];
strcpy(arr, s.c_str());
char** wrs_lst(arr);
return 0;
}
char** wrs_lst(char *fn){
int m = 2; //2 columns for first and middle name
std::string filename = fn;
int wnum = 0;
int zero = 0;
std::string str;
int counter = 0;
std::ifstream file(filename);
if(file.is_open()){
while(!file.eof()){
while(counter < 1){
std::getline(file, str);
wnum = std::stoi(str) + zero;
}
std::getline(file, str);
char** list = createMtx(wnum, m);
int space = findNthOccur(str, ' ', 1);
int shossz = str.length();
for(int i = 0; i < wnum; i++){
list[i][0] = str.substr(0, space);
list[i][1] = str.substr(space+1, shossz-space);
}
}
file.close();
}else{
std::cout << "Unable to open file!\n";
}
}
char** createMtx(int n, int m){
char** mtx = new char*[n];
if(mtx){
for(int i = 0; i<n; i++){
mtx[i] = new char[m];
if(!mtx[i]){
exit(1);
}
}
}else{
exit(1);
}
return mtx;
}
void deleteMtx(char** mtx, int n, int m){
for(int i = n-1; i != 0; i--){
delete[] mtx[i];
}
delete[] mtx;
}
int findNthOccur(std::string str, char ch, int N){
int occur = 0;
for (unsigned int i = 0; i < str.length(); i++) {
if (str[i] == ch) {
occur += 1;
}
if (occur == N)
return i;
}
return -1;
}
|