Thread: Simple text file editor - Having some problems - C program

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sarah,

    Would you post your current code?
    Last edited by Adak; 05-30-2007 at 08:31 AM.

  2. #17
    Registered User
    Join Date
    May 2007
    Posts
    12
    Sorry,

    Current code is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h> 
    
    char text[23][80];
    
    int main() {
    	int i=0, 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++;
    	}
    	fclose(g);
    	system("cls"); 
    	
    	for(i=0; i<23; i++) {
    		printf("&#37;s", text[i]);
    	}
    	printf("Command:");
    	fgets(temp, sizeof(temp), stdin); 
    	sscanf(temp, "%c %d", c, &num); 
    	
    	while(strcmp(temp, "Q") !=0) { 
    		if(strcmp(temp, "R") ==0) {
    			fgets(text[num],80,stdin);
    		}
    		if(strcmp(temp, "D") ==0) {
    			for(i=21; i<num; i++) {  
    				strcpy(text[i], text[i+1]);
    				/* "\n" (line 22) ??? */ 
    			}
    		}
    		if(strcmp(temp, "I") ==0) {
    			for(i=22; i>num; i--) {
    				strcpy(text[i], text[i-1]);
    			}
    		}
    		if(strcmp(temp, "S") ==0) {
    			for(i=0; i<23; i++) {
    				fprintf(g, text[i]);
    			}
    		}	
    	} 	     		
    }

  3. #18
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You could compare the first character and see what the command is by running it through a switch statement, and then operating on it. For example:

    Code:
    switch(c[0])
    {
    	case 'R':
    		/* Handle Replace command */
    	break;
    	case 'D':
    		/* Handle Delete command */
    	break;
    	case 'I':
    		/* Handle Insert command */
    	break;
    	case 'S':
    		/* Handle Save command */
    	break;
    	case 'Q':
    		/* Handle Quit command */
    	break;
    	default:
    		/* Bad command.  Print error message to user. */
    }
    There are many different ways of getting this done.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Would it be OK to break this program up into smaller functions?

    Doing it in ONE HUGE FUNCTION, is possible, but much more difficult.

    I have your new code now and will be firing up my lappy for a look-see, in a bit.

    Adak

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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 &#37;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.
    Last edited by Adak; 05-30-2007 at 05:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. problems reading from a text file
    By korbitz in forum C Programming
    Replies: 4
    Last Post: 12-21-2001, 06:11 PM