Hi there,
I'm having some problems with my latest programming attempt.
----------------------------------------------------------------------------------------------------------------
So you know what I'm talking about, the question is:
"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 away 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.
"
---------------------------------------------------------------------------------------------------------------
The code I have written is as follows:
Code:#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> char text[23][80]; int main() { int i, num; char c[80]; char temp[80]; FILE *g; printf("Enter file name:\n"); gets(c); g = fopen(c, "r"); if(g == NULL) { printf("Error: File did not open"); exit(0); } while(fgets (text[i], 80, g) !=NULL) { i++; /* started getting the microsoft send/dontsend error here */ } fclose(g); system("cls"); for(i=0; i<23; i++) { printf("%s", text[i]); } printf("Command:"); sscanf(temp, "%c %d", &c, &num); while(temp != "Q") { if(temp == "R") { fgets(text[num],80,stdin); } if(temp == "D") { for(i=21; i<num; i++) { /*"loop from num to 21"...should it be a while loop?? */ strcpy(text[i], text[i+1]); /* "\n" (line 22) ??? */ } } if(temp == "I") { for(i=22; i>num; i--) { strcpy(text[i], text[i-1]); } } if(temp == "S") { for(i=0; i<23; i++) { fprintf(g, text[i]); } } } }
When I run the program in my GCC compiler, the program runs. Then it prompts me "Enter file name" (so far so good) Then when I enter in my file name (the one I am using is a text file called junk.txt - basicly a list of movies numbered 0 to 22) it comes up with the microsoft "send/don't send" error. then crashes. The program runs further if I delete everything in my junk.txt file... ie a blank txt file. If I do this, it will prompt me to enter a "Command:".
When I debug my program, it stops me at this line, so I'm thinking, this might be the problem:
Any guidance on where to go from here would be appreciated.Code:while(fgets (text[i], 80, g) !=NULL) {
Thanks