I need to find out how to type cast an int counter to a char andthen concatenate
ID: 3617876 • Letter: I
Question
I need to find out how to type cast an int counter to a char andthen concatenate that and another char. My problem is with the lineof code, strcat(fileName, fileNum); I need the file name toincrement every time the code loops so the first fileName should be"text_1_0.txt" the second would be "text_1_1.txt" and so on untilthe loops exits. Please let me know how to change my code or somenew code on how to do would be helpful.Here is my code.
int main ()
{
string words[SIZE];
ofstream fout;
int count = 0;
char fileExt[5] = ".txt";
ifstream fin;
fin.open("testFile.txt");
// test to see if file opened correctly
if(fin.fail())
{
cout << "Error openingfile. ";
return 0;
}
while(!fin.eof())
{
char fileNum =static_cast<char>(count);
char fileName[20] ="text_1_";
strcat(fileName,fileNum);
strcat(fileName,fileExt);
for(int i = 0; i < SIZE;i++)
{
fin>> words[i];
}
sort(words, words + SIZE,sCompare);
fout.open("text_1_0.txt");
for(int i = 0; i < SIZE;i++)
{
fout<< words[i] << endl;
}
count++;
}
return 0;
}