This is the your program with some more roughing in:
Code:
/* "Write a simple editor.
The editor should first ask the user for the name of a file to edit.
You may assume that the file already exists. Files will be restricted to
exactly 23 lines of text, with no line longer than 70 characters.
The editor should display the text in the file starting at the top of the
screen. The 24'th line is used by the user to give commands to the editor.
The prompt should be Command:
The only commands that are available are:
* D (number) which will delete the line with that number. All lines after that
one will move up one. The new 23rd line at the bottom will be empty.
* I (number) will insert the text typed on the 25'th line before the line with
that number. All lines will move down one to accommodate the new line. The old
23'rd line will be discarded.
* R (number) will replace, with the text typed on the 25'th line, the line
with that number
* S will save the changes, by writing the text back to disk.
* Q Will quit the program
eg
Command: I 6
this new line will be placed between line 5 and line 6 of the file
After each command the screen should be cleared using the clrscr() function
(in <conio.h>). The text should then be displayed again.
Line numbers given by the user will, in true computer science style, start
from 0 and go to 22.
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
/* #include <stdlib.h> didn't find a use for this one, but you might.*/
/* If you print to the 80 column on the older monitors, you'll cause an
unwanted carriage return + line feed or newline as we call it in C. Avoid it!
*/
char text[23][72];
char cmd_let = '\0'; /* the command letter */
int cmd_num = 0; /* the command number */
void ShowText(void);
void GetCmd(void);
int main() {
int i=0, j;
char filename[72];
char temp[72] = { '\0' };
FILE *fp; /* name a file pointer so you'll remember it. 'g' didn't work for me. */
clrscr();
printf("Enter file name: ");
gets(filename); /* safer alternative is fgets(), but I didn't change this one */
fp = fopen(filename, "rt");
if(fp == NULL) {
printf("Error: File did not open");
exit(0);
}
while(fgets (text[i], 72, fp) !=NULL)
i++;
fclose(fp);
do
{
ShowText();
GetCmd();
/* process the command */
switch (cmd_let) {
case 'D': /* delete the line, and move lines down in array, as needed */
for (i = cmd_num; i < 22 ; i++)
strcpy(text[i], text[i+1]);
text[i][0] = '\0';
break;
case 'I': /* insert a new line, move lines up in array, as needed */
gotoxy(1,25);
cprintf("New line on %d: ", cmd_num);
fgets(temp, sizeof(temp), stdin);
for (i = 21; i >= cmd_num; i--)
strcpy(text[i+1], text[i]);
strcpy(text[cmd_num], temp);
break;
case 'R': /* replace the line of text with new text */
gotoxy(1,25);
cprintf("New Text on %d: ", cmd_num);
fgets(temp, sizeof(temp), stdin);
strcpy(text[cmd_num], temp);
break;
default:
printf("\n\n You've found a bug in the switch statement!");
return 1;
} /* end of switch */
} while(cmd_let != 'Q');
getch();
return 0;
}
/* Shows the text */
void ShowText(void)
{
int i = 0;
clrscr(); /* required - not system("cls"); */
for (i = 0; i < 23; i++)
printf("%s", text[i]);
gotoxy(1,24);
cprintf("Commands- Del/Ins/Replce/Save/Quit Line Number: ");
}
/* get the command letter and number (both digits if it has two) */
void GetCmd(void)
{
int c, i;
char temp2[6] = { '\0' };
cmd_let = '';
cmd_num = 0;
fgets(temp2, sizeof(temp2), stdin);
c = temp2[0];
if (isalpha(c)) {
cmd_let = toupper(c);
if (cmd_let == 'Q')
return;
}
c = temp2[2];
if (isdigit(c))
cmd_num = c - '0';
c = temp2[3];
if (isdigit(c))
cmd_num = (cmd_num * 10) + (c - '0');
}
If you have any questions about it, post back. The "Save" function still needs to be added.