I have a text file in which the data is stored in following format:-
aabb:aabb aacc:aacc aadd:aadd bbdd:bbdd bbaa:bbaa I am reading from the file line by line and trying to separate the words on either side of ':'. I am taking each line into a string line. I am assigning the word left to ':', char by char in the string w and the word right to ':', char by char to string m. But the problem is the the string w and m become empty after both the loops are executed. Why are string w and m empty?
int flag; string line, w, m; ifstream fin; fin.open("files/file2.txt",ios::in); if (fin.is_open()) { while (getline(fin,line)) { for (int i=0; i<line.length(); i++) { if (line[i] == ':') { flag = i+1; break; } else w[i] = line[i]; } for(int i=flag,k=1; i<line.length(); i++,k++) { m[k] = line[i]; } cout<<w<<'\n'; cout<<m<<'\n'; } fin.close(); } Thank you for your help.
w += line[i];andm += line[i];