• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Retrieving a character from a file (C++)

Status
Not open for further replies.
Level 4
Joined
Jan 4, 2006
Messages
60
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.

Code:
cout << "\nRandom number is: " << startingPoint << endl;
cout << "\nmsgVector is: " << msgVector[0] << endl;
cout << "\nc is: " << c << endl;

Code:
#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
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
file.get() is the correct method...

It's also pretty obvious why you're getting "no" character. You see:

bookSize = getSize(bookIn);
The getSize function will get characters until the end of file is reached. So if you try to get another character, you won't be able to because you've already reached the end of bookfile.

Solution: unget() bookSize times (although I think this isn't possible when eof is reached) or simply reopen the ifstream.
 
Status
Not open for further replies.
Top