I was working on a small project to practice i/o files in c++ but i can't figure out this problem. I wanted to write and algorithm that rearranges words in alphabetical order in a text file.(preferable bubble sorting). This is what i have so far
ifstream file("lab01.txt");
ofstream fileOut("lab01_out.txt");
char s[20][10];//variable for copying the words
//check if file was oppened
if (!file.is_open()) {
cout << "Error, file was not oppened!" << endl;
return -1;
}
//copy words from file to 2d array
for (int i = 0; i < 20; i++)
file >> s[i];
char check[1];
//bubble sort
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 18 - i; j++) {
if (strcmp(s[j], s[j + 1]) > 0) {
strncpy_s(check, s[j], _TRUNCATE);//if not truncated error "buffer to small"
strncpy_s(s[j], s[j + 1], _TRUNCATE);
strncpy_s(s[j + 1], check, _TRUNCATE);
}
}
}
//printing array to output file and to console.
for (int i = 0; i < 20; i++) {
cout << s[i] << endl;
fileOut << s[i] << endl;
}
//closing files.
file.close();
fileOut.close();
The problem is that this is what my output file looks like. I'm getting these symbols instead of words... enter image description here
Any help will be appreciated!