Recursive Function

The program should be able to read a file and reverse all the words inside, using recursive function.

This is my entire code. DO not bother about the main I know it is working
Do not bother with function" void OpenFile(const char *fileName, ifstream &inFile); " it is working
Do not bother with function " bool CheckIfCharIsASeparator(char charToCompare)" it is working
Do not bother with function " int Reverse(ifstream &inFile, int level)" it is working

I have a question though on function " int ReverseRecursively(char *tempLine, ofstream &MyDestinationFile) " that is being called by function " int Reverse(ifstream &inFile, int level)" .

TestFile2.txt contains these two lines
Text :is a, not: a number;
The big, brown , fox jumps! over? the lazy dog .

I read the character one at a time and.
when I reached a space ( where my pointer is pointing) I need to save that location but I am not allowed to use any static variables.the reason I wanted to save it is because as soon as I see a separator I go back and empty the stack and thus accomplish your first requirement which is to reverse the word. But in doing so my pointer also goes back to the first position before I called the recursive function.

So I ended up with "txeT txeT txeT txeT txeT".

When emptying the stack how can I keep the value of the pointer on that first space after the word "Text"?


THIS IS THE WHOLE PROGRAM SOMEHOW IT IS NOT DOING WHAT I WANT IT TO DO.
#include <cstdlib>
#include <fstream>
#include <iostream>
using namespace std;

const char * const FILENAME = "TestFile2.txt";
void OpenFile(const char *fileName, ifstream &inFile);
int Reverse(ifstream &inFile, int level);

int main()
{
ifstream inFile;
int thisSeparator;

OpenFile(FILENAME, inFile);
while ((thisSeparator = Reverse(inFile, 1)) != EOF)
cout.put(char(thisSeparator));
inFile.close();
return EXIT_SUCCESS;
}

//This function will just open the input file for processing
void OpenFile(const char *fileName, ifstream &inFile)
{
inFile.open(fileName, ios::in);
if(!inFile.is_open())
{
cout << cerr << " OPEN FILE " << fileName << " FAILED \n";
cout << " FILE WILL NOT OPEN: RETURN TO EXIT";
cin.get();
exit(EXIT_FAILURE);
}
}


bool CheckIfCharIsASeparator(char charToComp)
{
const char CHAR_SEPARATOR[] = " \t.?!,:;";
bool isCharASeprtor = false;
int charCntr, numOfSeprtor;
char *seprtorPntr;
seprtorPntr = (char*)CHAR_SEPARATOR;
numOfSeprtor = sizeof(CHAR_SEPARATOR);
for( charCntr = 0; charCntr < numOfSeprtor-1;seprtorPntr++, charCntr++)
if (charToComp == *seprtorPntr)
{
return(isCharASeprtor = true);
}
return(isCharASeprtor);
}


int ReverseRecursively(char *tempLine, ofstream &MyDestFle)
{
static int currentSeparator = 0;
char *tempLineDuplicatePointer, myChar;
tempLineDuplicatePointer = &tempLine[0];
//This gets the current separator
if (CheckIfCharIsASeparator(*tempLineDuplicatePointer))
{
currentSeparator = (int)*tempLineDuplicatePointer;
}
myChar = *tempLineDuplicatePointer;
if (!CheckIfCharIsASeparator(*tempLineDuplicatePointer))
{
ReverseRecursively(tempLineDuplicatePointer + 1, MyDestFle);
}
MyDestFle << *tempLineDuplicatePointer;
cout << myChar;
return(currentSeparator);
}


int Reverse(ifstream &inFile, int level)
{
//Variable declaration
const int LINE_LENGHT = 128;
int lineCntr = 0, funcCurrentSeparator = 0, ignoreErrorMessage = 0;
const char *REVERSEWORDFILE = "FinalOutput.txt";
char tempLine[LINE_LENGHT] = "";

char *TMP_FILE = tmpnam(NULL);
ofstream MyDestFle(TMP_FILE, ios::out);
if (!MyDestFle.is_open())
{
cout << cerr << "OPEN FILE " << TMP_FILE << " FAILED \n";
cout << " FILE WILL NOT OPEN: RETURN TO EXIT";
cin.get();
exit(EXIT_FAILURE);
}

if (!(level % 2))
{
inFile.getline(tempLine, LINE_LENGHT);
MyDestFle << tempLine << '\n';
}
else
{
while(!inFile.eof())
{
//This will get one line at a time of the file to be processed
inFile.getline(tempLine, LINE_LENGHT);
while(tempLine != '\0')
{
funcCurrentSeparator = ReverseRecursively(tempLine, MyDestFle);
}
}
if (inFile.eof())
{
funcCurrentSeparator = -1;
}
}
MyDestFle.close();
//delete the test file
if (remove(REVERSEWORDFILE) == -1)
{
cout << cerr << " RENAME FILE " << REVERSEWORDFILE << " FAILED \n";
cout << "FILE IS NOT AVAILABLE: 0 + RETURN TO IGNORE MESSAGE ";
cout << " 1 + RETURN TO EXIT \n";
cin >> ignoreErrorMessage;
if(ignoreErrorMessage)
exit(EXIT_FAILURE);
}
if (rename(TMP_FILE, REVERSEWORDFILE) == -1)
{
cout << cerr << " RENAMING THE FILE " << TMP_FILE << " FAILED \n";
cout << " DESTINATION FILE ALREADY EXIST: RETURN TO EXIT";
cin.get();
exit(EXIT_FAILURE);
}
return(funcCurrentSeparator);
}




Topic archived. No new replies allowed.