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

New Topic/Question
Reply




MultiQuote



|