473,322 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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.

Nov 30 '05 #1
18 30781
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
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
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

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
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
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
"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
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
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

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
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
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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ed Suominen | last post by:
I'm thinking of implementing a real-time collaborative text editor in Python using Twisted. An initial plan is to use a Twisted PB server daemon that accepts user:password:file connections from...
1
by: IkBenHet | last post by:
Hello, I found this script to create a simple rich text form (http://programmabilities.com/xml/index.php?id=17): <html> <head> <title>Rich Text Editor</title> </head> <body>
3
by: m3ckon | last post by:
Hi there, I can succesfully create a word doc from my asp.net page, but I have 2 issues I need to resolve in order to use it in my app: 1) Creating a table: I seem unable to create a table,...
2
by: DaWoE | last post by:
Hi all, I'm fairly new to ASP.NET. What i want to do is creat a online registration form. On the first step is getting the users details and the number of people he wants to register. Based on...
2
by: ValK | last post by:
Hello All. I have a windows service, that monitors file directory. Now I'm trying to create custom install wizard for this service. I added projectinstaller class to my application, where I...
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
3
by: Sean C. | last post by:
Hey All, I'm having a little problem here. I have a project that I'm working on that involves a MySQL server database backend. I'm having no problem creating the database on the fly if it...
3
by: hellboss | last post by:
Hi every one , iam working wit asp.net vs05,i need to built a editor to view the document. the editor should be similar to that of the message area in your site...
3
by: danesh1354 | last post by:
Hi All, First I need to construct a text editor by python programming and add this code to a biger code that has been written before, and i would like that by my code for this editor have a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.