I’m trying to read from a simple text file. Whenever I run the program it prints out the file,
but not before printing out a bunch of garble. Any suggestions?
Garble:
Φ#· ├ï Uï∞ Φ╖ ≈╪←└≈╪YH]├ 5tδ╝
abc
Buffer.cc
//-----------------------------------------------------
// TextInputBuffer - Constructor for TextInputBuffer.
//-----------------------------------------------------
TextInputBuffer::TextInputBuffer(char *InputFileName)
{
//--Open file. Abort if failed.
InputFile.open(InputFileName, std::ios::in);
if (!InputFile.good()) exit(1);
}
//-----------------------------------------------------
// GetNextLine - Get next line from input file.
//
// Return: The first character of the next line.
//-----------------------------------------------------
char TextInputBuffer::GetNextLine()
{
//--Get next line from input file.
if (InputFile.eof()) *ptrChar = eofChar;
else
{
InputFile.getline(Text, MaxInputBufferSize);
ptrChar = Text;
}
return *ptrChar;
}
//-----------------------------------------------------
// GetNextChar - Get next character from the text
// buffer.
//
// Return: The next character in the text buffer.
//-----------------------------------------------------
char TextInputBuffer::GetNextChar()
{
char ch;
if (*ptrChar == eofChar) ch = eofChar;
else if (*ptrChar == eolChar) ch = GetNextLine();
else
{
++ptrChar;
ch = *ptrChar;
}
return ch;
}
List.cc
TextInputBuffer InputBuffer(argv[1]);
char ch;
do {
ch = InputBuffer.GetNextChar();
if (ch == eolChar)
std::cout << std::endl;
std::cout << ch;
} while (ch != eofChar);
I don’t think it’s reading the first line after opening the file, so it’s fetching junk characters from the uninitialised line storage until one happens to be the newline character, at which point it actually reads the first line.