Stuck on Last Character in Input File

Inputing a Text File using Fstream

Page 1 of 1

3 Replies - 1629 Views - Last Post: 24 October 2009 - 10:35 PM Rate Topic: -----

#1 ericzmeh  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 13-September 09

Stuck on Last Character in Input File

Posted 23 October 2009 - 07:47 PM

Hi all, tad bit confused here. I'm inputting a text file using Fstream, and writing to an output file as well. I am using "char" to read each character as it goes, and have created functions to handle the characters based on certain conditions. My problem is, I input the text file provided by my instructor, and the output is fine. It copies all characters and spaces accordingly, and counts words, characters, lines, character spaces, etc..... Now, I created a few of my own txt files to run against, always with the same result of getting stuck in a loop. NOW, I found that if I manually(purposely) hit enter at the end of my document, it inputs no problem without getting stuck in a loop. Otherwise, I get stuck on the last character in the file in my copyText function.

My Question: Is a "new line" the only way to read to the end of the file successfully, using the eof function (of course, in a while loop). If I use the peek function, it does not seem to be able to read past the last character, which I thought would, and see there is nothing (which would flag eof???).

Here is my code that works as is. Please ignore updatePara function and any variables not being used at the end. Im only using three until I figure out just exacly my eof prob.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

void initialize(int& numWords, int& numLines, int& numLinesW, int& numParag, int& numChars, int& numCharSp, int& numCharTab, int& msParag);
void processBlank(ifstream& Fin, ofstream& Fout, char& f, int& numLines, int& numLinesW);
void copyText(ifstream& Fin, ofstream& Fout, char& f, int& numChars, int& numLines);
void updateCount(int& numWords, int& numLines, int& numLinesW, int& numParag);
void updatePara(ifstream& Fin, ofstream& Fout, char& f, int& msParag);
void printTotal(ofstream& Fout, int numWords, int numLines, int numParag, int msParag);


int main ()
{
	char f, e;
	string FileStr;
	int XnumWords, XnumLines, XnumLinesW, XnumParag, XnumChars, XnumCharSp, XnumCharTab, XmsParag;
	initialize(XnumWords, XnumLines, XnumLinesW, XnumParag, XnumChars, XnumCharSp, XnumCharTab, XmsParag);

	ifstream in;
	ofstream out;
		in.open("Test1.txt");
		out.open("Testout.txt");
		//out << fixed << showpoint;

		if (!in.is_open())
		{
			do {
			cout << "Incorrect File Name or Path Upon Execution" << endl;
			cout << "To Exit, Press E or e, To Enter File Name, Press Something Else " << endl;
			cin >> e;
				if (e == 'e' || e == 'E')
				{
					in.close();
					out.close();
					system("Exit");
				}
				else 
				{
				cout << "Type Text File Name or Path:  " << endl;
				getline(cin, FileStr);
				//cin >> FileStr;
				//system(FileStr); 
				}
			  if (in.is_open())
					FileStr = "E";

			   } while (FileStr != "E" || FileStr != "e");
		}
				
		
			in.get(f);
				
			while (!in.eof())
			{
			   while (f != '\n')
			   {
				processBlank(in, out, f, XnumLines, XnumLinesW);
				copyText(in, out, f, XnumChars, XnumLines);
				//updatePara(in, out, f, XmsParag);

			   }
			   updatePara(in, out, f, XmsParag);
			   updateCount(XnumWords, XnumLines, XnumLinesW, XnumParag);
			   in.get(f);
				if (f == '\n')
				  out << f;
			}

			printTotal(out, XnumWords, XnumLines, XnumParag, XmsParag);
			in.close();
			out.close();
			return 0;
			system("pause");
}

void initialize(int& numWords, int& numLines, int& numLinesW, int& numParag, int& numChars, int& numCharSp, int& numCharTab, int& msParag)
{
	numWords=numLines=numChars=numLinesW=numCharSp=numCharTab=msParag=0;
	numParag = 1;

}

void processBlank(ifstream& Fin, ofstream& Fout, char& f, int& numLines, int& numLinesW)
{
	while (f == ' ' && f != '\n') 
	{	
	   Fout << f; 
	   Fin.get(f);
	}

				
	  if (f != '\n')
		  numLinesW++;
			
}

void copyText(ifstream& Fin, ofstream& Fout, char& f, int& numChars, int& numLines)
{
	while (f != ' ' && f != '\n') 
	{
		numChars++;
		Fout << f;
		Fin.get(f);

	}
	if (f == '\n')
	{
		Fout << f;
		numLines++;
	}


		
}


void updateCount(int& numWords, int& numLines, int& numLinesW, int& numParag)
{
	numWords=numLinesW + numWords;
	
	if (numLinesW == 0)
		numParag++;
	else
		numLines=numLines;

		numLinesW = 0;

}

void updatePara(ifstream& Fin, ofstream& Fout, char& f, int& msParag)
{
	while (f == '\r')
	{
		Fout <<"((P))";
		Fout << f;
		Fin.get(f);
		msParag++;
	}

		/*if (f == '\r')
		{
			Fout <<"((P))";
			Fout <<f;
			msParag++;
		}*/
}

void printTotal(ofstream& Fout, int numWords, int numLines, int numParag, int msParag)
{ 
   if(numLines==0)
	numParag = 0;

cout << setfill(' ') << endl;
cout <<"Characters(no spaces):" << setw(13) << "XXX" << endl;
cout <<"Characters(single-spaces):" << setw(9) << "XXX" << endl;
cout <<"Tabs:" << setw(32) << "XXX" << endl;
cout <<"Words:" << setw(31) << numWords << endl;
cout <<"Lines:" << setw(31) << numLines << endl;
cout <<"Parag:" << setw(31) << numParag << endl;
cout <<"MS Parag:" << setw(28) << msParag << endl;

}



This will run against any text file (.txt) as long as the user hits enter after very last word, character, whatever typed.

I have thought about opening the file, copying all characters to the output file, and at the end stick a new line or some sort of sentinel value in there so I can signify a value other than MS's way. Problem is, like before, I get stuck on the very last character in that same loop, and can't go anywhere. I just dont understand why the very last character is not read once if there's nothing after :-] Thanks for any help

Is This A Good Question/Topic? 0
  • +

Replies To: Stuck on Last Character in Input File

#2 ericzmeh  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 13-September 09

Re: Stuck on Last Character in Input File

Posted 23 October 2009 - 07:52 PM

Well, I didn't think about changing the condition of my main loop.......sheesh. Just realized that. I still would like to know if anyone has any klnowloedge Im failing to recognize, though, about my eof question :-]
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: Stuck on Last Character in Input File

Posted 23 October 2009 - 07:58 PM

Not sure if you've read its page, but peek() will throw eof if it fails and sets the various fail/badbits accordingly.
Was This Post Helpful? 1
  • +
  • -

#4 ericzmeh  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 13-September 09

Re: Stuck on Last Character in Input File

Posted 24 October 2009 - 10:35 PM

Thanks, I see what I've done. Assigned to peek on the same character going through already doesn't work very well :-/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1