knuth> edit final.txt
[Editing "final.txt" 151 lines,
1884 characters]
CS 127
1> 1,3p
CS 127
Winter 1998
Final Projects
3> d
Due: 5:00, Wednesday March
18
3> a
This is a new line typed by
the user.
The additions end with a period
alone on a line.
.
5> s
[Saved "final.txt" 152 lines,
1884 characters]
5> q
[Bye-bye]
knuth>
Here, we have started editing the file "final.txt," asked to look at lines 1 through 3, deleted line 3 (making the old line 4 into the new line 3), added two new lines of text, saved the resulting file, and quit.
To get a better idea of how ed actually works, you can run it yourself in any UNIX window. Note that the example above is a bit different from the real edit. I have included the current line number in the edit prompt, enclosed all editor messages in square brackets, replaced the "w" ("write") command with the "s" ("save") command, and printed the first line as soon as the editing session began. If you want to make small modifications of this nature, do so, but document them well.
The core of this project will be to implement a small version of edit that includes the following commands:
M,Np Print lines M through N, inclusive.In addition to the core, you might add the ability to copy, cut, and paste lines, save to a new file, start editing a non-existent file (that is, create a new, empty file and start editing it), make global substitutions of one string for another, undo the previous operation, etc.
<return> Print the next line.
a Add new text following the current line.
The new text will be ended by a period
alone on a line.
i Insert new text before the current line.
The new text will be ended by a period
alone on a line.
d Delete the current line.
/bleah Print the next line on which the string "bleah"
occurs.
w Save the file.
q Quit
One of the biggest decisions you will need to make for this project is how to store and manipulate the text file you are editing. Ideally, your program should be able to handle an arbitrarily large file. Thus, if you shoot for the ideal, you won't be able to declare a fixed-size array in which you will store the entire file. Even if you do work with a fixed-size array, you'll need to make sure that your various operations take a reasonable amount of time (for example, finding the next occurrence of a string should take less than a second to keep your user from getting cranky).
#include <iostream>the output might look like this:
int main(){
int i;
for(i=0;i<10;i++) cout << "Hello\n";}
#include <iostream>The most important style points you should concentrate on are indentation and placement of braces.
int main()
{
int i;
for( i=0; i < 10; i++ )
cout << "Hello\n";
}
Once you have indentation and bracing working well, some other style elements you could work on include spacing within for, while, and if statements, spacing in parameter lists in function headers and function calls, consistent spacing in assignment and arithmetic statements, blank lines placed after local variable declarations, etc.
If you have time and inclination, you could have your program offer the user multiple styles. For example, the bracing convention some books use:
if( this == that )is different from the one I use:
{
dosomething();
}
if( this == that ){
dosomething();
}