Crafting Text Editor with C: Tips on Efficient Text Handling

  • Thread starter Eus
  • Start date
  • Tags
    Text
In summary: It's an amazing program that is really simple to use and doesn't have all the bells and whistles of more popular editors. Once you get used to it, you can move on to something more complex, like emacs or vim.
  • #1
Eus
94
0
Hi Ho! ^^

I'm still learning programming in C (I'm still on my first year in Information Technology faculty)
Well, I'm interested in making something from scratch.
Hehehe... right now I'm curious about efficient text handling technique used in a text editor.

When creating a simple text editor, I create a temporary array of char in C.
It is: char *charInput[ 1000 ];
I hope the user doesn't type longer than 999 characters.
Then when the user press ENTER, I'll copy the content of the charInput into a file.
That's my idea to handle the text. I know this is a bad one because:
1. If the user input a text longer than 1000 (e.g. DNA chain code), my charInput can't accommodate it.
2. Time consuming of IO operation when the user press ENTER or ARROW key to navigate the screen because my program always read the file from HD.
3. charInput is such a waste of space because it reserves 1000 bytes in memory although the user only type 3 characters.

From my data structures course, I've an idea to create a linklist for every character which is typed. But this is a bad idea because it will waste more space for the pointer.

Yesterday I browsed through the Internet searching an efficient method for this problem. Unfortunately, I didn't find it.
But I've had some information.

According to you, does Emacs has the most efficient text handling method?
I've read several articles on Emacs and downloaded the source code from FSF.
I wonder how Emacs handles text using its buffer. The structure for buffer in buffer.h only contain some int variables. So, where's the text if it is not written directly to the file? (I read that Emacs buffer is on the main memory, not in HD)
So, could you please give me an information about how actually Emacs handles the text using the buffer?

Do you have any suggestions where I must go to find out more about text handling technique?

Mmm... sorry if I'm not straight to the point but I just want to explain my situation.

Thank you very much for helping me, guys!
 
Last edited:
Computer science news on Phys.org
  • #2
You will need to use a dynamic array. You are currently creating an array with 1000 holes that can be filled with letters. A better solution is to create an array with one hole and simple add holes as needed. To do this you will need to look into reallocating array space to grow or shrink as needed.

http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/dynamic.html

Also you will want to use a buffer(memory) to store your work. Don't read/write every letter as it is typed to a file because that is very inefficient as you have found out.

Look at the code for the pico text editor. It is much smaller than emacs but still very powerful. You might also try reading the code for vim.

Good luck.
 
  • #3
I would recommend going even simpler than pico or vim. Basically at first you want to get away from anything even related to a terminal (because this is where things get most complicated). I would start with ed.
http://directory.fsf.org/text/editors/ed.html
 

Related to Crafting Text Editor with C: Tips on Efficient Text Handling

1. What is a text editor?

A text editor is a software application that allows users to create and edit text files. Common features of text editors include the ability to change font styles and sizes, insert images and hyperlinks, and perform search and replace operations.

2. What are the benefits of crafting a text editor with C?

C is a powerful and efficient programming language, making it a good choice for creating a text editor. By using C, developers can create a lightweight and fast text editor with advanced features and customization options.

3. What are some tips for efficient text handling in a C-based text editor?

Some tips for efficient text handling in a C-based text editor include using memory allocation and management techniques, optimizing data structures for text processing, and implementing algorithms for common text operations such as search and replace.

4. Can a text editor created with C handle large files?

Yes, a text editor created with C can handle large files. This is because C allows for efficient memory management and processing, making it suitable for handling large amounts of data.

5. Is it possible to add additional features to a text editor crafted with C?

Yes, it is possible to add additional features to a text editor crafted with C. C is a versatile language that allows for the creation of complex and customizable programs, so developers can easily add new features to a C-based text editor as needed.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
16
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Computing and Technology
Replies
2
Views
23K
  • Computing and Technology
Replies
14
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
Replies
10
Views
2K
  • Feedback and Announcements
Replies
3
Views
970
  • Programming and Computer Science
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
6K
Back
Top