Creating a text editor in C

S

siliconwafer

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.
 
R

Richard Heathfield

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.
 
S

siliconwafer

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?
 
E

Ed Prochak

siliconwafer said:
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
 
T

tatimmer

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.


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
 
R

Randy Howard

siliconwafer wrote
(in article
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.
 
K

Keith Thompson

Becker said:
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.
 
K

Keith Thompson

Randy Howard said:
siliconwafer wrote
(in article


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.
 
R

Randy Howard

Keith Thompson wrote
(in article said:
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.
 
G

Gregory Pietsch

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.

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
 
S

siliconwafer

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
 
S

siliconwafer

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
 
R

Richard Heathfield

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.

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.


Non-standard function.
return(0);

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

siliconwafer

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
 
S

siliconwafer

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.
 
N

Niklas Norrthon

siliconwafer said:
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
 
M

Mark McIntyre

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.
 
M

Mark McIntyre

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top