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';
}
*/

Recommended Answers

All 2 Replies

I still don't know what 0R1,0R2,0S4,1S4 does! Can you give an example of what each of the instruction should do on some input

This is how I understand the instructions in the file. The format of each instruction is: two characters followed by a positive number (possibly a multi-digit number), with no spacing in between (e.g., 0R16; here '0' is the first character, 'R' is the second, and 16 is the number).

The meaning of these characters is as follows:

-- The first character (either '0' or '1') is the character that your program needs to write to the output sequence in place of (i.e., at the same position) the character that has just been read from that sequence is.

-- The second character (one of 'L', 'R', or 'S') represents how the sequence pointer will move at the end of the current instruction. Namely, 'L' moves it to the left by one; 'R' moves it to the right by one; and 'S' means that the pointer will stay in place (i.e., no movement).

-- The positive number at the end of each instruction represents the index of the row of instructions that needs to be looked at next, upon completion of the operations for the present row of instructions. (Once again, remember that row indices start from 1, not 0.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.