When i runt he code and try to get the size of the 2 files something goes wrong with the msgIn file. I'm using the same method for both files and the bookIn will work but msgIn just returns 0 as if it never ran.
this is the method that gets the size of the file
int getSize (istream& file)
{
int num = 0;
char c =file.get();
while (file)
{
++num;
c = file.get();
}
return num;
}
This code runs the method to get the size
int bookSize = 0;
int msgSize = 0;
//msgSize = getSize(msgIn);
//msgIn.clear();
//msgIn.seekg(0, ios::beg);
bookSize = getSize(bookIn);
bookIn.clear();
bookIn.seekg(0, ios::beg);
cout << "\nthe size of the book is: " << bookSize << endl;
cout << "\nthe size of the msg is: " << msgSize << endl;
#define _CRT_RAND_S
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
int getSize (istream& file)
{
int num = 0;
char c =file.get();
while (file)
{
++num;
c = file.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;
int msgSize = 0;
//msgSize = getSize(msgIn);
//msgIn.clear();
//msgIn.seekg(0, ios::beg);
bookSize = getSize(bookIn);
bookIn.clear();
bookIn.seekg(0, ios::beg);
cout << "\nthe size of the book is: " << bookSize << endl;
cout << "\nthe size of the msg is: " << msgSize << endl;
int startingPoint = 0;
char c = bookIn.get();
for (int i=0; i < msgSize; ++i)
{
//select a random number from the generator
startingPoint = random(0,bookSize);
//go to that number in the book
for (int ii=0; ii < startingPoint; ++ii)
{
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 ii=startingPoint; ii < bookSize; ++ii)
{
c = bookIn.get();
if (msgVector[0] == c)
{
//print number of char in bookFile to outFile, print a space before next char.
out << ii << " ";
break;
}
}//end for
}//end for
}//end stream
cin.ignore();
return 0;
}//end main