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;
}
Explanation / Answer
please rate - thanks this will do what you want #include #include #define SIZE 4 using namespace std; 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 words[i]; } sort(words, words + SIZE,sCompare); fout.open("text_1_0.txt"); for(int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.