I already wrote the code to read in these two files:
processing_rules.txt - which will contain rules like these:
0R1, 0R2
1S3, 0S3
end
input_data.txt - will contain data:
5
00110101
The positive number (5, in the example above) represents the position in the second-line sequence, from which processing will begin.
The function should read in the two files, process the rules, and if it says 0R1 then it's supposed to change the 00110101
The first instruction is, as mentioned above, always on row #1. Of the two instructions in row #1 (0R1, 0R2), we will execute the first one (0R1), since it corresponds to the '0' underneath our position pointer in the input stream. (If it was pointing at a '1' in the input stream, we would be executing the second instruction.)
This is a sample input and output:
On the same input stream:
5
00110101
and with the following processing instruction stream:
0L1, 1L2
0L2, 0L3
1S4, 1S4
end
The output stream will be:
01010101
I do not want you to solve this for me, I want you to help me start it. Maybe a method or help me understand the concept of the problem
#include <iostream>
#include "Text File Reader Wrapper.h"
using namespace std;
// Constructor of the class: associates the given filename with an input stream handle
TextFileReaderWrapper::TextFileReaderWrapper( char * pcFilename )
{
// associates a file stream with a given file (as specified by its filename)
oInputFileStream.open(pcFilename);
if (!oInputFileStream)
{
cout << "Error opening file: " << pcFilename << endl;
exit(1);
}
}
// Retrieves the next word from the input file stream (a private attribute, associated with the given file).
// A word, for our purposes, is defined as a sequence of characters between whitespaces; hence, it may have punctuation in it.
// Returns a Boolean condition indicating whether this is the last word in the file.
// This can be helpful when reading in multiple words in a loop, as shown in the sample client code (File Reader.cpp)
bool TextFileReaderWrapper::retrieveNextWord( char * pcNextWord )
{
int nPos = 0; // position (index) of the latest extracted character for the current word
char ch; // temporary character variable to store the latest character read from the file
bool bWordCompleted = false; // do we have a complete word extracted, or is the process still ongoing (i.e., hasn't reached a whitespace yet)?
// while the current word is still incomplete (more non-whitespace consecutive characters left to read in)
// and the file hasn't reached an end yet...
while ( !bWordCompleted && (ch = this->oInputFileStream.get()) != EOF )
{
switch (ch)
{
case '\n': case '\r': case '\t': case ' ': // various whitespaces
if (nPos==0) continue; // skips leading whitespaces: ignores the current character and proceeds to reading the next one
pcNextWord[nPos] = '\0'; // non-leading whitespaces indicate the end of the current word, so the string gets terminated (by '\0')
bWordCompleted = true;
// cout << "The next extracted word is: " << pcNextWord << endl;
break;
default: // actual non-whitespace characters; they get stored, not skipped over
pcNextWord[nPos] = ch;
nPos++;
}
}
// wraps up the word, since the EOF symbol was reached and reading the input text file has ended,
// but the word hasn't been terminated (with the final '\0' character) in the string yet
if (!bWordCompleted)
{
pcNextWord[nPos] = '\0';
// cout << "The last extracted word is: " << pcNextWord << endl;
// since EOF has been reached, this is the last word in the file
return true;
}
else
// the word was completed without reaching the EOF, therefore this must not be the last word in the file
return false;
}
/*
// A sample method I had in earlier versions (for testing) that read the entire file of words with one invocation, not word by word
void TextFileReaderWrapper::retrieveAllWords( char * pcLastWord )
{
int nPos = 0; // position (index) of the latest extracted character for the current word
char ch; // temporary character variable to store the latest character read from the file
while ( (ch = this->oInputFileStream.get()) != EOF )
{
switch (ch)
{
case '\n': case '\r': case '\t': case ' ': // various whitespaces
if (nPos==0) continue; // skips leading whitespaces: ignores the current character and proceeds to reading the next one
pcLastWord[nPos] = '\0';
nPos = 0;
break;
default:
pcLastWord[nPos] = ch;
nPos++;
}
}
// wrap up the last word, since the EOF symbol was reached and reading the input text file has ended
pcLastWord[nPos] = '\0';
}
*/