Creating a text editor in C

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • siliconwafer

    Creating a text editor in C

    I want to create a text editor using C.
    I want user to enter text freely without worring about what's gonig
    inside.
    I tried some combinations of fgets,fgetc with realloc and stdin as
    poniter.Bute can't do it successfully.
    What can be a way?
    How are editors created normally?
    If there are some functions in C ,welcomed.
    thanks.

  • Becker

    #2
    Re: Creating a text editor in C

    Maybe you'd like to read Richard Stallman's
    <<EMACS: The Extensible, Customizable Display Editor>>
    at:


    Comment

    • Richard Heathfield

      #3
      Re: Creating a text editor in C

      siliconwafer said:
      [color=blue]
      > I want to create a text editor using C.
      > I want user to enter text freely without worring about what's gonig
      > inside.
      > I tried some combinations of fgets,fgetc with realloc and stdin as
      > poniter.Bute can't do it successfully.
      > What can be a way?
      > How are editors created normally?
      > If there are some functions in C ,welcomed.
      > thanks.[/color]

      Using only ISO C, you can write a line editor reasonably easily. But if you
      want fully-addressable screen I/O, fancy graphics, and so on, you'll need
      to use libraries specific to your implementation (although two or three
      cross-platform libraries do exist which can, at least, make your code
      portable across the more popular desktop OSs).

      I suggest you start off by deciding how you wish to represent the text
      internally - a linked list of line buffers is one possible option, but by
      no means the only one. You should then be able to write a suite of
      functions to manage and manipulate this storage. So far, you can stay
      entirely within the remit of ISO C. Then, if you value flashy graphics over
      portability, I suggest you move your enquiries to a newsgroup dealing with
      programming for your platform. If you value portability over flashy
      graphics, this is probably a better newsgroup for your questions.

      Have a go, see how far you get, ask if you get stuck, and be ready to post
      your best-effort code.

      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at above domain (but drop the www, obviously)

      Comment

      • siliconwafer

        #4
        Re: Creating a text editor in C

        How does one create a 1 line editor with dynamic allocationi?
        Say,If user types only 3 characters,I want to reserve only 3 locations
        for it.
        With static allocation,I can take an array of 640 characters,but it
        would be inefficient
        if user types only 3 characters per line.
        Any hints?


        Richard Heathfield wrote:[color=blue]
        > siliconwafer said:
        >[color=green]
        > > I want to create a text editor using C.
        > > I want user to enter text freely without worring about what's gonig
        > > inside.
        > > I tried some combinations of fgets,fgetc with realloc and stdin as
        > > poniter.Bute can't do it successfully.
        > > What can be a way?
        > > How are editors created normally?
        > > If there are some functions in C ,welcomed.
        > > thanks.[/color]
        >
        > Using only ISO C, you can write a line editor reasonably easily. But if you
        > want fully-addressable screen I/O, fancy graphics, and so on, you'll need
        > to use libraries specific to your implementation (although two or three
        > cross-platform libraries do exist which can, at least, make your code
        > portable across the more popular desktop OSs).
        >
        > I suggest you start off by deciding how you wish to represent the text
        > internally - a linked list of line buffers is one possible option, but by
        > no means the only one. You should then be able to write a suite of
        > functions to manage and manipulate this storage. So far, you can stay
        > entirely within the remit of ISO C. Then, if you value flashy graphics over
        > portability, I suggest you move your enquiries to a newsgroup dealing with
        > programming for your platform. If you value portability over flashy
        > graphics, this is probably a better newsgroup for your questions.
        >
        > Have a go, see how far you get, ask if you get stuck, and be ready to post
        > your best-effort code.
        >
        > --
        > Richard Heathfield
        > "Usenet is a strange place" - dmr 29/7/1999
        > http://www.cpax.org.uk
        > email: rjh at above domain (but drop the www, obviously)[/color]

        Comment

        • Ed Prochak

          #5
          Re: Creating a text editor in C


          siliconwafer wrote:[color=blue]
          > How does one create a 1 line editor with dynamic allocationi?
          > Say,If user types only 3 characters,I want to reserve only 3 locations
          > for it.
          > With static allocation,I can take an array of 640 characters,but it
          > would be inefficient
          > if user types only 3 characters per line.
          > Any hints?
          >
          >[/color]

          look up malloc()

          (that should be a sufficient hint)

          HTH,
          ed

          Comment

          • tatimmer@ticon.net

            #6
            Re: Creating a text editor in C

            siliconwafer wrote:[color=blue]
            > I want to create a text editor using C.
            > I want user to enter text freely without worring about what's gonig
            > inside.
            > I tried some combinations of fgets,fgetc with realloc and stdin as
            > poniter.Bute can't do it successfully.
            > What can be a way?
            > How are editors created normally?
            > If there are some functions in C ,welcomed.
            > thanks.[/color]


            The "pec" multi-line edit control is written in C for Windows. It uses
            malloc on each keystroke and connects the blocks with a double link
            list.

            Get fast and reliable internet services for individuals, businesses, and organizations. Our high-speed internet, email, and web hosting solutions are designed to meet your specific needs. Stay connected to the world with our seamless connectivity.


            TomT

            Comment

            • Randy Howard

              #7
              Re: Creating a text editor in C

              siliconwafer wrote
              (in article
              <1133366771.757 839.100020@f14g 2000cwb.googleg roups.com>):
              [color=blue]
              > How does one create a 1 line editor with dynamic allocationi?
              > Say,If user types only 3 characters,I want to reserve only 3 locations
              > for it.
              > With static allocation,I can take an array of 640 characters,but it
              > would be inefficient
              > if user types only 3 characters per line.[/color]

              This is 2005. Are you really worried about maximizing memory
              information for a single line of text?

              If you are, (or if your professor is), then lookup the malloc()
              documentation on your system.


              --
              Randy Howard (2reply remove FOOBAR)
              "The power of accurate observation is called cynicism by those
              who have not got it." - George Bernard Shaw





              Comment

              • Keith Thompson

                #8
                Re: Creating a text editor in C

                "Becker" <webmaster@devh r.com> writes:[color=blue]
                > Maybe you'd like to read Richard Stallman's
                > <<EMACS: The Extensible, Customizable Display Editor>>
                > at:
                > http://www.gnu.org/software/emacs/emacs-paper.html[/color]

                First, please provide context when you post a followup.
                Read <http://cfaj.freeshell. org/google/>.

                Second, I don't think emacs is a good starting point for a C beginner
                who wants to write a text editor. Emacs is practically an operating
                system. A simpler line-oriented editor like "ed" would be a much
                better place to start.

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                We must do something. This is something. Therefore, we must do this.

                Comment

                • Keith Thompson

                  #9
                  Re: Creating a text editor in C

                  Randy Howard <randyhoward@FO OverizonBAR.net > writes:[color=blue]
                  > siliconwafer wrote
                  > (in article
                  > <1133366771.757 839.100020@f14g 2000cwb.googleg roups.com>):
                  >[color=green]
                  >> How does one create a 1 line editor with dynamic allocationi?
                  >> Say,If user types only 3 characters,I want to reserve only 3 locations
                  >> for it.
                  >> With static allocation,I can take an array of 640 characters,but it
                  >> would be inefficient
                  >> if user types only 3 characters per line.[/color]
                  >
                  > This is 2005. Are you really worried about maximizing memory
                  > information for a single line of text?
                  >
                  > If you are, (or if your professor is), then lookup the malloc()
                  > documentation on your system.[/color]

                  If a 1-line editor is to be the basis for a multi-line editor, it
                  certainly makes sense to conserve memory, so you can edit large files
                  without using an order of magnitude more memory than necessary. And
                  it makes sense to implement the allocation code at an early stage of
                  the project.

                  --
                  Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                  San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                  We must do something. This is something. Therefore, we must do this.

                  Comment

                  • Randy Howard

                    #10
                    Re: Creating a text editor in C

                    Keith Thompson wrote
                    (in article <lnr78xlwiu.fsf @nuthaus.mib.or g>):
                    [color=blue]
                    > Randy Howard <randyhoward@FO OverizonBAR.net > writes:[color=green]
                    >> siliconwafer wrote
                    >> (in article
                    >> <1133366771.757 839.100020@f14g 2000cwb.googleg roups.com>):
                    >>[color=darkred]
                    >>> How does one create a 1 line editor with dynamic allocationi?
                    >>> Say,If user types only 3 characters,I want to reserve only 3 locations
                    >>> for it.
                    >>> With static allocation,I can take an array of 640 characters,but it
                    >>> would be inefficient
                    >>> if user types only 3 characters per line.[/color]
                    >>
                    >> This is 2005. Are you really worried about maximizing memory
                    >> information for a single line of text?
                    >>
                    >> If you are, (or if your professor is), then lookup the malloc()
                    >> documentation on your system.[/color]
                    >
                    > If a 1-line editor is to be the basis for a multi-line editor, it
                    > certainly makes sense to conserve memory, so you can edit large files
                    > without using an order of magnitude more memory than necessary.[/color]

                    I don't disagree with that, but I was referring to the context
                    from upthread, wherein this was a (single) line editor, to meet
                    clc standard conformance, etc.


                    --
                    Randy Howard (2reply remove FOOBAR)
                    "The power of accurate observation is called cynicism by those
                    who have not got it." - George Bernard Shaw





                    Comment

                    • Gregory Pietsch

                      #11
                      Re: Creating a text editor in C


                      siliconwafer wrote:[color=blue]
                      > I want to create a text editor using C.
                      > I want user to enter text freely without worring about what's gonig
                      > inside.
                      > I tried some combinations of fgets,fgetc with realloc and stdin as
                      > poniter.Bute can't do it successfully.
                      > What can be a way?
                      > How are editors created normally?
                      > If there are some functions in C ,welcomed.
                      > thanks.[/color]

                      For a good line editor written in C, you can always grab the sources of
                      FreeDOS Edlin, available on ibiblio or alt.sources.

                      Gregory Pietsch

                      Comment

                      • siliconwafer

                        #12
                        Re: Creating a text editor in C

                        Here is my first code for a line editor.
                        Point out(A hell lot of?)mistakes if any.Suggest corrections whereever
                        applicable(ever ywhere?).
                        The code is working.I can say this because the program did not
                        terminate abnormally and I printed the output to a text file and it
                        showed me correctly what I typed on the console.
                        Only thing bothering me is at the end,I get a message"null pointer
                        alignment".What 's this?
                        Here is the code:

                        #include<stdio. h>
                        #include<conio. h>
                        #include<stdlib .h>
                        #include<string .h>
                        int main() {
                        unsigned char*cptr;
                        unsigned int count = 0;
                        unsigned char*tptr;
                        unsigned int size = 2;
                        unsigned int startpt = 1;
                        FILE*fptr;

                        clrscr();
                        fflush(stdin);
                        cptr = (char*)calloc(s izeof(char),10) ;

                        if((fptr = fopen("c:\\Shek har\\C\\myTextI nEditor.txt","w ")) == NULL) {
                        printf("File Cannot be created!!\n");
                        exit(EXIT_FAILU RE);
                        }


                        while((*cptr = getchar()) != '\n') {


                        fprintf(fptr,"% c",*(cptr));
                        cptr++;
                        if(++count < 10);
                        else {

                        if((tptr =(char*)calloc( size*10,sizeof( char))) == NULL) {
                        printf("Program Terminating!!") ;
                        exit(EXIT_FAILU RE);
                        }

                        memcpy(tptr,cpt r,startpt*10);
                        free(cptr);
                        cptr = tptr;
                        cptr = cptr + count*startpt;
                        count = 0;
                        size++;startpt+ +;
                        }
                        }

                        free(tptr);
                        getch();
                        return(0);
                        }

                        Regards,
                        -Siliconwafer



                        siliconwafer wrote:[color=blue]
                        > I want to create a text editor using C.
                        > I want user to enter text freely without worring about what's gonig
                        > inside.
                        > I tried some combinations of fgets,fgetc with realloc and stdin as
                        > poniter.Bute can't do it successfully.
                        > What can be a way?
                        > How are editors created normally?
                        > If there are some functions in C ,welcomed.
                        > thanks.[/color]

                        Comment

                        • siliconwafer

                          #13
                          Re: Creating a text editor in C

                          Here is my first code for a line editor.
                          Point out(A hell lot of?)mistakes if any.Suggest corrections whereever
                          applicable(ever ywhere?).
                          The code is working.I can say this because the program did not
                          terminate abnormally and I printed the output to a text file and it
                          showed me correctly what I typed on the console.
                          Only thing bothering me is at the end,I get a message"null pointer
                          alignment".What 's this?
                          Here is the code:


                          #include<stdio. h>
                          #include<conio. h>
                          #include<stdlib .h>
                          #include<string .h>
                          int main() {
                          unsigned char*cptr;
                          unsigned int count = 0;
                          unsigned char*tptr;
                          unsigned int size = 2;
                          unsigned int startpt = 1;
                          FILE*fptr;


                          clrscr();
                          fflush(stdin);
                          cptr = (char*)calloc(s izeof(char),10) ;


                          if((fptr = fopen("c:\\Shek har\\C\\myTextI nEditor.txt","w ")) ==
                          NULL) {
                          printf("File Cannot be created!!\n");
                          exit(EXIT_FAILU RE);
                          }


                          while((*cptr = getchar()) != '\n') {


                          fprintf(fptr,"% c",*(cptr));
                          cptr++;
                          if(++count < 10);
                          else {


                          if((tptr =(char*)calloc( size*10,sizeof( char)))
                          == NULL) {
                          printf("Program Terminating!!") ;
                          exit(EXIT_FAILU RE);
                          }


                          memcpy(tptr,cpt r,startpt*10);
                          free(cptr);
                          cptr = tptr;
                          cptr = cptr + count*startpt;
                          count = 0;
                          size++;startpt+ +;
                          }
                          }


                          free(tptr);
                          fclose(fptr);
                          getch();
                          return(0);



                          }


                          Regards,
                          -Siliconwafer

                          Comment

                          • Richard Heathfield

                            #14
                            Re: Creating a text editor in C

                            siliconwafer said:
                            [color=blue]
                            > #include<stdio. h>
                            > #include<conio. h>[/color]

                            Non-standard header. You don't need this unless you're trying to do a
                            full-screen editor. Non-standard libraries and headers are not covered here
                            in comp.lang.c - I'll point those out as I go.
                            [color=blue]
                            > #include<stdlib .h>
                            > #include<string .h>
                            > int main() {
                            > unsigned char*cptr;
                            > unsigned int count = 0;
                            > unsigned char*tptr;
                            > unsigned int size = 2;
                            > unsigned int startpt = 1;
                            > FILE*fptr;
                            >
                            >
                            > clrscr();[/color]

                            Non-standard function.
                            [color=blue]
                            > fflush(stdin);[/color]

                            fflush's behaviour is *only* defined for streams open for output or update.
                            This is not the way to "clear the buffer", so to speak.
                            [color=blue]
                            > cptr = (char*)calloc(s izeof(char),10) ;[/color]

                            cptr = malloc(10); would be perfectly acceptable, provided you remember to
                            null-terminate your data after reading it. Or you can do:

                            cptr = calloc(1, 10);

                            The cast is unnecessary. See http://www.cpax.org.uk/prg/writings/casting.php
                            for an explanation of why not to use a cast. Also note that sizeof(char) is
                            1 by definition. Finally, note that malloc, calloc, and realloc can all
                            fail. If they do, they return NULL, in which case you should not try to use
                            the resulting memory because there isn't any.
                            [color=blue]
                            > if((fptr = fopen("c:\\Shek har\\C\\myTextI nEditor.txt","w ")) ==
                            > NULL) {[/color]

                            Later, you'll want to modify this to get the name of the file from the user
                            in some way.
                            [color=blue]
                            > printf("File Cannot be created!!\n");
                            > exit(EXIT_FAILU RE);
                            > }
                            >
                            >
                            > while((*cptr = getchar()) != '\n') {[/color]

                            You need to test the possibility that the user has indicated a desire to end
                            standard input. In such a case, getchar() returns EOF - which is not a
                            character (and should not be treated like one) but a message to you, the
                            programmer, that there will be no more input from that stream.
                            [color=blue]
                            >
                            > fprintf(fptr,"% c",*(cptr));[/color]

                            *(cptr): the () are unnecessary; *cptr is fine.
                            [color=blue]
                            > cptr++;[/color]

                            Careful. You'll want to release this memory in due course by passing the
                            original pointer value to free() - which means you mustn't forget what that
                            pointer value was. Use a temp to point to the same place (initially), so
                            that you can modify the temp rather than cptr.
                            [color=blue]
                            > if(++count < 10);
                            > else {[/color]

                            Simpler: if(++count >= 10) {
                            [color=blue]
                            > if((tptr =(char*)calloc( size*10,sizeof( char)))
                            > == NULL) {[/color]

                            if((tptr = calloc(size * 10, 1)) == NULL) would do it, of course. Now, ten
                            at a time is one way to do it, I suppose - but there are better strategies.
                            But more of that at some other time. By the way, look up realloc().
                            [color=blue]
                            > printf("Program Terminating!!") ;
                            > exit(EXIT_FAILU RE);
                            > }
                            >
                            >
                            > memcpy(tptr,cpt r,startpt*10);
                            > free(cptr);[/color]

                            Undefined behaviour - cptr doesn't point to where it used to any more.

                            <snip>
                            [color=blue]
                            > getch();[/color]

                            Non-standard function.
                            [color=blue]
                            > return(0);[/color]

                            The parentheses are not required. Just do this: return 0;

                            --
                            Richard Heathfield
                            "Usenet is a strange place" - dmr 29/7/1999

                            email: rjh at above domain (but drop the www, obviously)

                            Comment

                            • siliconwafer

                              #15
                              Re: Creating a text editor in C

                              Ok,
                              Now I want to traverse through my line,back and forth,edit my line
                              using ctrl + d or ctrl + a etc.
                              I don't expect code.I want how can one catch the combinations(ct rl +
                              a,ctrl + b)etc.
                              in C code?i.e their ASCII values or key codes or anything else?
                              -Siliconwafer




                              siliconwafer wrote:[color=blue]
                              > Here is my first code for a line editor.
                              > Point out(A hell lot of?)mistakes if any.Suggest corrections whereever
                              > applicable(ever ywhere?).
                              > The code is working.I can say this because the program did not
                              > terminate abnormally and I printed the output to a text file and it
                              > showed me correctly what I typed on the console.
                              > Only thing bothering me is at the end,I get a message"null pointer
                              > alignment".What 's this?
                              > Here is the code:
                              >
                              >
                              > #include<stdio. h>
                              > #include<conio. h>
                              > #include<stdlib .h>
                              > #include<string .h>
                              > int main() {
                              > unsigned char*cptr;
                              > unsigned int count = 0;
                              > unsigned char*tptr;
                              > unsigned int size = 2;
                              > unsigned int startpt = 1;
                              > FILE*fptr;
                              >
                              >
                              > clrscr();
                              > fflush(stdin);
                              > cptr = (char*)calloc(s izeof(char),10) ;
                              >
                              >
                              > if((fptr = fopen("c:\\Shek har\\C\\myTextI nEditor.txt","w ")) ==
                              > NULL) {
                              > printf("File Cannot be created!!\n");
                              > exit(EXIT_FAILU RE);
                              > }
                              >
                              >
                              > while((*cptr = getchar()) != '\n') {
                              >
                              >
                              > fprintf(fptr,"% c",*(cptr));
                              > cptr++;
                              > if(++count < 10);
                              > else {
                              >
                              >
                              > if((tptr =(char*)calloc( size*10,sizeof( char)))
                              > == NULL) {
                              > printf("Program Terminating!!") ;
                              > exit(EXIT_FAILU RE);
                              > }
                              >
                              >
                              > memcpy(tptr,cpt r,startpt*10);
                              > free(cptr);
                              > cptr = tptr;
                              > cptr = cptr + count*startpt;
                              > count = 0;
                              > size++;startpt+ +;
                              > }
                              > }
                              >
                              >
                              > free(tptr);
                              > fclose(fptr);
                              > getch();
                              > return(0);
                              >
                              >
                              >
                              > }
                              >
                              >
                              > Regards,
                              > -Siliconwafer[/color]

                              Comment

                              Working...