By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,795 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,795 IT Pros & Developers. It's quick & easy.

Creating a text editor in C

P: n/a
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.

Nov 30 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
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

Nov 30 '05 #2

P: n/a
siliconwafer said:
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.


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)
Nov 30 '05 #3

P: n/a
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:
siliconwafer said:
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.


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)


Nov 30 '05 #4

P: n/a

siliconwafer wrote:
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?


look up malloc()

(that should be a sufficient hint)

HTH,
ed

Nov 30 '05 #5

P: n/a
siliconwafer wrote:
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.

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.

www.ticon.net/~tatimmer/programming.htm

TomT

Nov 30 '05 #6

P: n/a
siliconwafer wrote
(in article
<11**********************@f14g2000cwb.googlegroups .com>):
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.


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

Nov 30 '05 #7

P: n/a
"Becker" <we*******@devhr.com> writes:
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


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_Keith) ks***@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.
Nov 30 '05 #8

P: n/a
Randy Howard <ra*********@FOOverizonBAR.net> writes:
siliconwafer wrote
(in article
<11**********************@f14g2000cwb.googlegroups .com>):
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.


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.


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_Keith) ks***@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.
Nov 30 '05 #9

P: n/a
Keith Thompson wrote
(in article <ln************@nuthaus.mib.org>):
Randy Howard <ra*********@FOOverizonBAR.net> writes:
siliconwafer wrote
(in article
<11**********************@f14g2000cwb.googlegroups .com>):
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.


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.


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.


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

Nov 30 '05 #10

P: n/a

siliconwafer wrote:
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.


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

Dec 1 '05 #11

P: n/a
Here is my first code for a line editor.
Point out(A hell lot of?)mistakes if any.Suggest corrections whereever
applicable(everywhere?).
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(sizeof(char),10);

if((fptr = fopen("c:\\Shekhar\\C\\myTextInEditor.txt","w")) == NULL) {
printf("File Cannot be created!!\n");
exit(EXIT_FAILURE);
}
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_FAILURE);
}

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

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

Regards,
-Siliconwafer

siliconwafer wrote:
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.


Dec 1 '05 #12

P: n/a
Here is my first code for a line editor.
Point out(A hell lot of?)mistakes if any.Suggest corrections whereever
applicable(everywhere?).
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(sizeof(char),10);
if((fptr = fopen("c:\\Shekhar\\C\\myTextInEditor.txt","w")) ==
NULL) {
printf("File Cannot be created!!\n");
exit(EXIT_FAILURE);
}
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_FAILURE);
}
memcpy(tptr,cptr,startpt*10);
free(cptr);
cptr = tptr;
cptr = cptr + count*startpt;
count = 0;
size++;startpt++;
}
}
free(tptr);
fclose(fptr);
getch();
return(0);

}
Regards,
-Siliconwafer

Dec 1 '05 #13

P: n/a
siliconwafer said:
#include<stdio.h>
#include<conio.h>
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.
#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();
Non-standard function.
fflush(stdin);
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.
cptr = (char*)calloc(sizeof(char),10);
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.
if((fptr = fopen("c:\\Shekhar\\C\\myTextInEditor.txt","w")) ==
NULL) {
Later, you'll want to modify this to get the name of the file from the user
in some way.
printf("File Cannot be created!!\n");
exit(EXIT_FAILURE);
}
while((*cptr = getchar()) != '\n') {
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.

fprintf(fptr,"%c",*(cptr));
*(cptr): the () are unnecessary; *cptr is fine.
cptr++;
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.
if(++count < 10);
else {
Simpler: if(++count >= 10) {
if((tptr =(char*)calloc(size*10,sizeof(char)))
== NULL) {
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().
printf("Program Terminating!!");
exit(EXIT_FAILURE);
}
memcpy(tptr,cptr,startpt*10);
free(cptr);
Undefined behaviour - cptr doesn't point to where it used to any more.

<snip>
getch();
Non-standard function.
return(0);


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

--
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)
Dec 1 '05 #14

P: n/a
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(ctrl +
a,ctrl + b)etc.
in C code?i.e their ASCII values or key codes or anything else?
-Siliconwafer


siliconwafer wrote:
Here is my first code for a line editor.
Point out(A hell lot of?)mistakes if any.Suggest corrections whereever
applicable(everywhere?).
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(sizeof(char),10);
if((fptr = fopen("c:\\Shekhar\\C\\myTextInEditor.txt","w")) ==
NULL) {
printf("File Cannot be created!!\n");
exit(EXIT_FAILURE);
}
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_FAILURE);
}
memcpy(tptr,cptr,startpt*10);
free(cptr);
cptr = tptr;
cptr = cptr + count*startpt;
count = 0;
size++;startpt++;
}
}
free(tptr);
fclose(fptr);
getch();
return(0);

}
Regards,
-Siliconwafer


Dec 2 '05 #15

P: n/a
Ref:
cptr++;
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. "

If I freeup the space using cptr, and temp & cptr are pointing to same
space,would'nt it create a dangling pointer,temp ?I.e where will temp
point when cptr frees up the space?
This actually gives me a message:"Null pointer assignment" at the end
of the program execution.

Dec 2 '05 #16

P: n/a
"siliconwafer" <sp*********@yahoo.com> writes:
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(ctrl +
a,ctrl + b)etc.
in C code?i.e their ASCII values or key codes or anything else?
-Siliconwafer


Now you are straying into implementation specific details. As
you are probably well aware of, the C FAQ has a section on this
and pointers on where to go with such questions.

The only I/O defined by the C standard is reading from and
writing to streams (represented by pointers to FILE objects).

But don't despair. Almost all platforms that has a keyboard
has some platform specific way how to get access to the kind
of things you ask, this is just not the place to ask how to.

/Niklas Norrthon
Dec 2 '05 #17

P: n/a
On 2 Dec 2005 04:07:21 -0800, in comp.lang.c , "siliconwafer"
<sp*********@yahoo.com> wrote:
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(ctrl +
a,ctrl + b)etc.
in C code?i.e their ASCII values or key codes or anything else?


You can't do this in Standard C. Youd have to use features of your OS
/ compiler / GUI, and for that you'd need to ask in a
Windows/.Linux/VMS/AmigaOS group or whatever.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 3 '05 #18

P: n/a
On 2 Dec 2005 04:26:26 -0800, in comp.lang.c , "siliconwafer"
<sp*********@yahoo.com> wrote:
"Use a temp to point to the same place (initially), so
that you can modify the temp rather than cptr. "
If I freeup the space using cptr, and temp & cptr are pointing to same
space,would'nt it create a dangling pointer,temp ?
All pointers 'dangle' once you free what they used to point to. This
is normal. cptr would be exactrly the same if you think about it.
I.e where will temp
point when cptr frees up the space?
nowhere, just like cptr points to nowhere.
This actually gives me a message:"Null pointer assignment" at the end
of the program execution.


Thatd be for a different reason.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 3 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.