0

I have a kelimeler.txt file.

101,Tarkan,E,1,22,1,1,0 
102,Mustafa,E,2,54,0,0,1
103,Seher,K,2,65,0,1,0
104,Ali,E,1,88,0,1,0
105,Ahmet,E,3,12,1,0,0
106,Osman,E,5,2,0,0,0
107,Remziye,K,3,63,0,0,1
108,Ayse,K,1,10,0,0,0
109,Esma,K,2,25,0,0,0
110,Mahmut,E,1,35,0,0,0
111,Rıza,E,2,6,0,0,0
112,Sultan,K,4,14,0,0,0
113,Selim,E,1,85,0,1,0
114,Bekir,E,3,42,0,0,0
115,Salih,E,5,81,1,1,1
116,Fatma,K,2,25,0,1,0
117,Aliye,K,2,3,0,0,0
118,Yilmaz,E,1,69,0,1,0
119,Omer,E,2,93,1,0,1
120,Ibrahim,E,1,68,0,0,0
121,Azra,K,5,58,0,0,0
122,Meryem,K,1,15,0,0,0
123,Berk,E,2,80,0,0,0
124,Emre,E,3,60,1,1,0
125,Enes,E,2,42,1,0,0
126,Cemre,K,3,72,0,0,1
127,Burak,E,3,66,0,1,0
128,Nalan,K,4,45,0,0,0
129,Yusuf,E,5,41,0,0,0
130,Sait,E,5,50,0,0,0
131,Mehmet,E,2,30,0,0,0
132,Burcu,K,4,75,0,1,0
133,Elif,K,4,21,1,0,1
134,Zeynep,K,2,32,1,0,0
135,Aydin,E,2,33,1,0,0
136,Zeliha,K,3,87,0,1,0
137,Umut,E,1,28,0,0,0
138,Dogan,E,2,18,0,0,0
139,Ela,K,3,36,0,0,0
140,Zehra,K,4,24,0,0,0
141,Recep,E,4,46,1,1,0
142,Halil,E,5,18,0,0,0
143,Irmak,K,4,39,0,0,1
144,Fadime,K,3,52,0,0,0
145,Yagmur,K,3,19,0,0,0
146,Busra,K,2,61,0,0,0
147,Ozkan,E,3,40,0,0,0
148,Tarik,E,1,37,1,0,0
149,Nesrin,K,1,20,0,0,0
150,Cenk,E,3,34,1,0,1 

I have to separate the elements in this file from commas and transfer them to a two dimensional matrix. First i read the file and equaled a value. Then i converted to string value from char array. Then i seperated from commans with strtok function. Then i created a for loop. But its not working.

#include <iostream>
#include <fstream>
#include <cstring>
#include <bits/stdc++.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;

void ReadFileFunc(){
    ifstream readFile("kisiler.txt"); 
    
    string cumle = "";
    string kelime = "";
    string word = "";
    while(readFile >> cumle){
        
        kelime += cumle;
        kelime+="\n";
        //cout<<"Aktarildi"<<endl;
    }
int n = kelime.length();

char kelime_array[n+1];
strcpy(kelime_array,kelime.c_str());
for(int i = 0; i<n;i++){
    cout << kelime_array[i];
}

//cout << kelime<<"\n \n";
cout << endl;

string a[50][8];
char *point;
int sayi = 0;
int i = 0;
int j = 0;
point = strtok(kelime_array,",");
while (point!=NULL){
    cout  << " ";
    for(i = 0 ; i<50 ; i++){
        for(j = 0; j<8; j++){
            a[i][j] = point ;
            cout << a[i][j] << " ";
            point = strtok(NULL,",");
        }
    }
}

    cout << a[0][1] ; // I tried but its not working

    readFile.close();
}

int main(int argc, char** argv) {
    
    ReadFileFunc();
    return 0;
}
  • Unrelated: Escape the [cargo cult](https://en.wikipedia.org/wiki/Cargo_cult_programming). Know what `#include ` does and then [stop directly including it](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – user4581301 Dec 09 '20 at 21:12
  • Don't use `strtok`, as it modifies the string. Consider using `std::getline(readFile, ',')` – Thomas Matthews Dec 09 '20 at 21:49
  • 1
    Why do you need a 2d matrix? I recommend creating a struct (or class) that models one record (line of input), then use `std::vector` of that class. – Thomas Matthews Dec 09 '20 at 21:51

0 Answers0