I'm trying to get a char back from the bookFile.txt but, when i run the program it returns nothing. I'm not sure if .get() if the right thing to be using.

When i run this code i get the random number and the first letter of the msgFile.txt returned correctly. But the bookFile character(c) is blank.

cout << "\nRandom number is: " << startingPoint << endl;
cout << "\nmsgVector is: " << msgVector[0] << endl;
cout << "\nc is: " << c << endl;
#define _CRT_RAND_S
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;

int getSize (istream& bookFile)
{
	int num = 0;
	char c = bookFile.get();
	while (bookFile)
	{
		++num;
		c = bookFile.get();
	}
	return num;
}

int random(int low, int high)
{
	unsigned num;
	rand_s(&num);
	double normalized = static_cast<double>(num) / UINT_MAX;
	return static_cast<int>(normalized * (high - low +1)) + low;
}

int main(int argc, char *argv[])
{
	if (argc != 4)
	{
		cout << "Usage: encode.exe bookFile.txt msgFile.txt outFile.txt" << endl;
	}
	else
	{
		ifstream bookIn(argv[1]);
		ifstream msgIn(argv[2]);
		ofstream out(argv[3]);
		vector<char> msgVector;
		while (msgIn)
		{
			msgVector.push_back(msgIn.get());
		}

		cout << "contents of vector: " << endl;
		for(int i=0; i < msgVector.size(); ++i) cout << msgVector[i];

		//count number of chars in the book file and set the char size
		int bookSize = 0;
		bookSize = getSize(bookIn);
		cout << "\nthe size of the book is: " << bookSize << endl;
		int startingPoint = 0;

		//select a random number from the generator
		startingPoint = random(0,bookSize);

		//go to that number in the book
		char c = bookIn.get();
		for (int i=0; i < startingPoint; ++i)
		{
			c = bookIn.get();
		}
		cout << "\nRandom number is: " << startingPoint << endl;
		cout << "\nmsgVector is: " << msgVector[0] << endl;
		cout << "\nc is: " << c << endl;

		//find the next char that is = to the char of the msg
		for(int i=startingPoint; i < bookSize; ++i)
		{
			if (msgVector[0] == c)
			{
				//print number of char in bookFile to outFile, print a space before next char.
				cout << "\nThe offset is: " << i << endl;
			}
		}//end for

	}//end stream

	cin.ignore();

	return 0;
}//end main

line 10: that function gets the file size the hard and slooooow way. All you have to do is seek to the end of file then call tellg() to get the file size -- only two lines of code :)

If you want to leave that function as it is, then you will have to move the file pointer back to the beginning of the file and clear all errors before leaving the function. If you don't then the program won't be able to read the file any more.

>>But the bookFile character(c) is blank.
Look at the file with Notepad.exe (or some other text editor) and find the character your program should find. Is it a space?

i'm going to leave line 10 the way it is (tellg() is something we haven't done in school yet). So how do i make the file pointer to the beginning of the file and clear all errors? also i'm sure that its not a space coming up. I ran the program many times with the same result and wont feel like counting out up to 800,000 characters to find out.

>>and wont feel like counting out up to 800,000 characters to find out.
LOL :)

>>So how do i make the file pointer to the beginning of the file and clear all errors

infile.seekg(0, ios::beg);
infile.clear();

You should start reading about the various functions available to fstream class. Here is one good source.

I added it in here:

//count number of chars in the book file and set the char size
		int bookSize = 0;
		bookSize = getSize(bookIn);
		bookIn.seekg(0, ios::beg);
		bookIn.clear();
		cout << "\nthe size of the book is: " << bookSize << endl;
		int startingPoint = 0;

But it still shows up as blank. Did i put it in the wrong place?

You need to clear the flags before the seek.

file.clear();
    file.seekg (0, ios::beg);
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.