1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/* <Anonymous Name Stuff Here>
Add line #'s to a text file. Filter out the leading space on
each line.
Advanced Version: If a line exceeds 72 characters, start a new
line without breaking any word.
*/
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
using namespace std;
string read_line(ifstream& fin);
int read_word(ifstream& fin, int& wcc, string& word);
void put_back(string& word, ifstream& fin);
int main ()
{
int count(0);
const int size(101);
char ans, infile[size], outfile[size], ext_prgram;
string word;
ifstream fin;
ofstream fout;
cout << "Please enter the file name to be opened: ";
cin >> infile;
fin.open(infile);
cout << "\nPlease enter the file name to be created: ";
cin >> outfile;
fout.open(outfile);
if (fin.fail())
{
cout << "\nProgram failed to open the input file. Ending program now.\n\n";
exit(1);
}
if (fout.fail())
{
cout << "\nProgram failed to open the output file. Ending program now.\n\n";
exit(1);
}
while (! fin.eof())
{
//make a function read_line
word = read_line(fin);
count++;
//output line number, then output line
fout << count << ": " << word;
cout << count << ": " << word;
}
//close both files
fin.close();
fout.close();
cout << "\n\nPress enter to exit: ";
do
{
cin.ignore();
cin.get(ext_prgram);
}while (!(isspace(ext_prgram)));
return 0;
}
//where \n is reached less than
//72 characters is a line, in addition, when character count
//exceeds 72, then a new line is inserted.
//Words must be kept whole.
string read_line(ifstream& fin)
{
//A line is formed either by reaching a \n or
//lcc exceeds 72 (put back the last word)
//variables: char c
char c;
//variable line_break is true when the word ends with a \n
int line_break;
//line character counter & word character counter
//keep the character count on the word(wcc)
//line length(lcc) is an accumlation of word length.
int lcc(0), wcc(0);
//string word, string line
string word = ""; string line = "";
//read a word at a time (a word is a string delimited by spaces)
//use a function to read a word
do
{
line_break = read_word(fin, wcc, word);
//condition to stop reading another word:
//1) wcc + lcc > 72: put back the word in fin, return line
if ((wcc + lcc) > 72)
{
put_back(word, fin);
return line;
}
//2) line_break is true: add word to line and return line
if (line_break > 0)
{
line += word;
return line;
}
// If neither of above condition is true:
// Add word to line
// Add wcc to lcc
// Repeat reading a word
line += word;
lcc += wcc;
}while (!line_break);
}
int read_word(ifstream& fin, int& wcc, string& word)
{
char c;
//start word from nothing
word = "";
wcc = 0;
do
{
fin.get(c);
word += c;
wcc ++;
}while(! isspace(c) && !fin.eof());
if (fin.eof())
{
return 2;
}
else if (c == '\n')
{
return 1;
}
else
{
return 0;
}
}
void put_back(string& word, ifstream& fin)
{
char c;
fin.get(c); // Very confused with this part.
}
|