This program numbers the lines found in a text file. Write a program that reads text from a file and outputs each line to the screen and to another file preceded by a line number. Print the line number at the start of the line and right-adjusted in a field of three spaces. Follow the line number with a colon, then one space, then the text of the line. You should get a character at a time and write code to ignore leading blanks on each line. You may assume that the lines are short enough to fit within a line on the screen.
A somewhat harder version determines the number of spaces needed in the field for the line numbers by counting lines before processing the lines of the file. This version of the program should insert a new line after the last complete word that will fit within a 10-character line.(I changed it from 72-character line to 10-character line to make testing easier)
I solved the easier version of the exercise. It is the "somewhat harder version" that I am having trouble with. I'm not sure what the part about "determining the number of spaces needed in the field for the line numbers by counting lines before processing the lines of the file" is asking me to do, so I ignored that part.
The chapter of the book that this programming exercise is from doesn't teach the "getline" function. I must solve this problem using "get", "put", "putback", ">>", and "<<".
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 146 147 148 149 150 151 152 153 154 155 156
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void copy_file(ifstream& fin, ofstream& fout);
void number_lines(ifstream& fin_a, ifstream& fin_b, ofstream& fout);
int main()
{
ifstream fin_a, fin_b;
ofstream fout;
fin_a.open("textfile1.txt");
if (fin_a.fail())
{
cout << "Input file opening failed.\n";
system("pause");
exit(1);
}
fout.open("textfile2.txt");
if (fout.fail())
{
cout << "Output file opening failed.\n";
system("pause");
exit(1);
}
copy_file(fin_a, fout);
fin_a.close();
fout.close();
fin_a.open("textfile1.txt");
if (fin_a.fail())
{
cout << "Input file opening failed.\n";
system("pause");
exit(1);
}
fin_b.open("textfile2.txt");
if (fin_b.fail())
{
cout << "Input file opening failed.\n";
system("pause");
exit(1);
}
fout.open("textfile3.txt");
if (fout.fail())
{
cout << "Output file opening failed.\n";
system("pause");
exit(1);
}
number_lines(fin_a, fin_b, fout);
fin_a.close();
fin_b.close();
fout.close();
system("pause");
return 0;
}
void copy_file(ifstream& fin, ofstream& fout)
{
char next;
fin.get(next);
while (!fin.eof())
{
fout << next;
fin.get(next);
}
}
void number_lines(ifstream& fin_a, ifstream& fin_b, ofstream& fout)
{
int line_count = 1, char_count, char2_count, space_count, word_count;
int chars_after_last_space = 0, last_chars_after_last_space = 0;
char next_a, next_b;
bool count_ends_on_space;
const int SPACES_PER_LINE = 10;
fin_a.get(next_a);
fin_b.get(next_b);
while ((!fin_a.eof()) || (!fin_b.eof()))
{
char_count = 0, char2_count = 0, space_count = 0, word_count = 0;
if (next_a == ' ')
{
while (next_a == ' ')
fin_a.get(next_a);
}
while ((char_count < (SPACES_PER_LINE - last_chars_after_last_space)) && (!fin_a.eof()))
{
if ((next_a == ' ') || (next_a == '\n'))
chars_after_last_space = 0;
else
chars_after_last_space++;
if (next_a != '\n')
char_count++;
if ((next_a == ' ') || (next_a == '\n'))
space_count++;
fin_a.get(next_a);
}
if ((next_a == ' ') || (next_a == '\n'))
chars_after_last_space = 0;
if (next_a != ' ')
count_ends_on_space = false;
else
count_ends_on_space = true;
if (next_b == ' ')
{
while (next_b == ' ')
fin_b.get(next_b);
}
if (count_ends_on_space)
{
fout << setw(3) << line_count << ": ";
cout << setw(3) << line_count << ": ";
line_count++;
while ((word_count < space_count) && (!fin_b.eof()))
{
if (next_b == '\n')
{
fout << ' ';
cout << ' ';
}
else
{
fout << next_b;
cout << next_b;
}
if (isspace(next_b))
word_count++;
fin_b.get(next_b);
}
}
else if (!count_ends_on_space)
{
fout << setw(3) << line_count << ": ";
cout << setw(3) << line_count << ": ";
line_count++;
while ((char2_count < (SPACES_PER_LINE - chars_after_last_space)) && (!fin_b.eof()))
{
{
fout << next_b;
cout << next_b;
char2_count++;
}
fin_b.get(next_b);
if (next_b == '\n')
fin_b.get(next_b);
}
}
fout << endl;
cout << endl;
last_chars_after_last_space = chars_after_last_space;
}
}
|
This is what I have written in the source file:
123456789T
I went there
Is that?
I want to
I who Thee
This is what I end up with in the target file:
1: 123456789T
2: I went
3: thereIs
4: that?I
5: want to I
6: who Th
7: ee
I think I got pretty close to solving it but I've been going over it for days and I can' t get any further. When a word is too long to fit on the end of a line it is sent to the next line, but with no space between it and the following word.
My solution seems convoluted to me. I think there is a simpler way. I just can't see it. The book doesn't teach anything about creating a copy of a file in this chapter, so I think that's where I went wrong.