Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

text editor

Status
Not open for further replies.

shanley06

Programmer
Nov 26, 2002
101
0
0
US
can anyone please tell me how i can make a text editor in qbasic that will allow me to creat and open text files and edit them and save them
 
That basically splits to several tasks.
The first question is what you are going to do. Another Notepad? No? ;)
How much text you would like to edit (one page may be or up to 2Gb of text).
:) it would say how you'll store that text).
How much of functionality you wish to implement.

Suppose you want create small window with editable text in your program.
Then you can store all text in a string. (or array of strings if you like)
You have to process keyboard input, command/cursor movement keys; and output chars on screen in right directions.
(You can't use "input" because it does not know about window bounds you would like to stay it in (word wrap)).
How to store paragraph marks (ENTER).
And you'll have to handle scrolling, if text grows... And think how implement select/cut/copy/paste/undo.
Of cource you can choose not to implement some of this.

Opening and saving is relatively easy parts, example for open, text stored in lines array:
(
maxLines=100
dim text$(maxLines)
open aFName for input as #1
i=0
while not eof(1)
i=i+1
line input #1, text$(i)
wend
)

the editing is the thing that would be hard.

BTW I think there was file selector dialog posted in this forum. I can't find it...

BTW, I'm pretty sure there is some ready code available.

Hope this can help.
 
1st look into arrays...
DIM myarray(X) AS String
where X is the Max number of lines

here are the VERY BASICS of file I/O

'Load a file...
N = 0
OPEN "file.txt" FOR INPUT AS #1
DO UNTIL EOF(1)
INPUT #1, myarray(N)
N=N+1
LOOP
CLOSE


Save a file...
OPEN "file.txt" FOR OUTPUT AS #1
FOR N = 0 TO UBOUND(myarray)
PRINT #1, myarray(N)
LOOP
CLOSE


as for the actually editor... there are a million ways to create one... You might want to be a little more specific...

Good Luck,
-Josh

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
There is a great text editor program. It is called QBwrite. I belive that you can find a copy of it at if not search the web for qbwrite27.bas and you will find it.
 
Most text editors have one of these two problems:

1. When you get to the bottom of the page, it starts reprinting the last line:
a
aa
aaa
aaaa
aaaaa

2. When you get to the bottom of the page, every letter you type causes the line to blink.
 
you can write directly to the screen buffer using &hB800, similar to the way you write to the graphics buffer at &hA000...

this might avoid the problems listed above, and be a little bit faster than QB's PRINT command...

each character needs 2 bytes...
1 is the ascii character, the second is the attribute...

Yes, textmode buffer is at &HB800
Each char is 2 bytes, first one is the char and second is the arrtibute.

attribute: (128 * flash) + (16 * background) + foreground.

flash: 0 or 1 (1 is on)
background: 0 to 7 (the dark colors)
foreground: 0 to 15 (all 16 colors)

fyi... The byte looks like this in binary:
00000000
flash
background
foreground



To use this...
you need to set the: DEF SEG = &hB800

the characters in memory look like this...

0 1 2 ... 78 79
80 81 ... 158 159
...
1920 ... 1999

so the formual for the location will start by looking like this...
L = X + (Y * 80)
...But... since there are 2 bytes per character...
L = (X + (Y * 80)) * 2

Calculate the attribute...
attr = color + (back *16) + (flash * 128)

Then you poke like this...
POKE L, ASC("A")
POKE L + 1, Attr


Play with it a little bit, and see what you can come up with...

Good luck

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
not trying to mean anything, but why not use microsoft word or such? just wondering.
 
I hope nobody minds if I jump in here. A text editor is a basic component for most programs that do more than print "Hello World". The higher level programming interfaces have features that allow the programmer to drag, say, a text box or a Rich Text box onto a form to give the user editing capabilities. Qbasic programmers are forced to create this functionality from scatch.


Remember... though eagles may soar, weasels don't get sucked into jet engines.
 
I wanted to make an IDE for borlands free c++ compiler
 
You Might Want To try Borland Turbo Pascal 7

It is close enough to QBasic... And has a set of libraries where you can Make A QBasic IDE/Windows style interface... Includeing Multi Window editors, etc...
It even comes with examples of how to create them...

If you can program in QBasic, Pascal should not be that hard to pick up on...

There should be several places where you can Download it "for educational use" the same way you can get QB...

After all, Nobody wants to pay for DOS applications Anymore...

Pascal was obsoleted by delphi, the same way QBasic was by VB

In fact Delphi is the VB of Pascal...

Pascal is Faster because it is a real 'Compiled Language' where QuickBasic is still more of a Translated Language...

Pascal was developed to compete with Microsofts Quick Basic...

Which is Where Delphi came in to keep the race going with Visual Basic...

If nothing else It would be a good language to learn...

It's more or less a cross between C/C++ and QuickBasic...

Good Luck

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
shanley06,
I think you can find some free IDE.
One came to mind is RHIDE (very much like BC++ 3.1), initially written to DJGPP free C compiler.
(Of cource it apply if you need an IDE - and not just writing program for the sake of it).
 
If that is the case... You can also set up Turbo Pascal 7 to Compile C Code...

It has all kinds of options...

You just create a tool item for the tools menu... tell it what program you want to run (the compiler) and whatever else you want to have it do... you can use macros for the file names, paths...

Then just create a new file...
Save it as a C file...
Click on your tool in the menu...
And you are good to go... ;-)

*Note...
If you do just need an editor for the compiler...
You might just want to use NotePad or a different editor...
Then make a *.BAT file to make/build/compile your project...
That is usually what I did for DJGPP...

...I still hate VC++ they made it so *EASY TO USE*... I can't even figure it out half the time...

If you do decide to go visual... Go Visual Basic (6... screw .NET)

Good Luck

Have Fun, Be Young... Code BASIC
-Josh Stribling
cubee101.gif

 
First of all, the reason your text is doing the a aa aaa thing is because you can only go to about line 20 in QBasic. Anything beyond that scrolls up because the lines below that autoscroll up. So, you have to keep this in mind.

As for the text editor... I made mine using random access files for storing the edited data, then just wrote the text file from the random access file when it came time to create it. Here's how I did it:

Type textfiletemplate
lineoftext as string * 80
End Type

Dim Template as textfiletemplate


Now we have a random access file template which is 80 characters long... a typical line on an 80x25 screen.

Open template.dat for random access read write as 1 len=len Template

'get max lines in temp data file
MaxLines = LOF(1) / Len(Template)

Now we have our data file, which is pretty much gonna be empty every time we open our program. This file will store 80 characters of text, which is equal to 1 line. If you think about it, random access files are a perfect solution because of the huge size you can have and the speed of data reading and writing.

Now simply make the shell of your text editor and have each line written as changes are made:

Template.lineoftext = mylinestring
Put 1, linenumber, Template

Likewise, read a line:

Get 1, linenumber, Template
mylinestring = Template.lineoftext

So, what you end up with is each line is a record. This works wonders especially when loading and saving the file. You can run a loop of 1 thru MaxLines and write your output text file in a fraction of a second. Likewise, you can read a text file into a new data file and use it to display HUGE files with little overhead. With random access files there are no limit (well, there are, but it's like 3 million records and who's gonna exceed that in DOS?). Best of all, the only memory you need to use are 20 strings of length 80 (i.e. 80x20 screen, since we all know anything much below line 20 will scroll on you). I've written a text file viewer in QBasic which works great. It's fast and does the job. I'd be willing to send it to you as an example. It doesn't let you edit text, but it'll give you what you need to write your own using random access files. In QB there's no other way to go if you want huge file reading/writing capability without using any memory whatsoever. E-mail me if you want the code: cgfiend@hotmail.com
 
Ah heck... I decided to post the code instead. Here's my text file viewer using random access files. Compile it and give it a file to read (i.e. myprogram.exe myfile.txt). Throw the largest text file you can at it. It won't even flinch.

'Readme.bas - a text viewer using a random access file as a temp
'holder of text file. This is just a project I did just to say I
'had done it. I did. Yes, there's room for improvement, but I
'never did add to it.
'
'Written by CGFiend
'
'

'strip is a sub to remove chr$(12) (line feed char) from a string
DECLARE SUB strip (a$)

'template for data file
TYPE DataFileTemplate
LineOfText AS STRING * 80
END TYPE

DIM DataFile AS DataFileTemplate
CLS
IF COMMAND$ = "" THEN
PRINT : PRINT "No filename specified. Usage: README filename.ext": PRINT : PRINT
GOTO endit2
END IF

'File not found? etc.
ON ERROR GOTO preend

'record = 1 - record counter
Record = 1

'open input text file
OPEN COMMAND$ FOR INPUT AS #1
'open output temp random access file
OPEN "87654321.TMP" FOR RANDOM ACCESS WRITE AS 2 LEN = LEN(DataFile)

'On slow slow computers this'll display, otherwise it'll be too fast
LOCATE 1, 1: PRINT "Processing..."

'a return point
redo:

'get me a line from the text file
LINE INPUT #1, a$

'check for end of file
IF EOF(1) THEN CLOSE 1: CLOSE 2: GOTO nexter

'strip off chr$(12) (line feed) from string
strip (a$)

'truncate to 80 characters and store in temp data file template
DataFile.LineOfText = LEFT$(a$, 80)

'put line in temp data file
PUT 2, Record, DataFile

'increment record counter
Record = Record + 1

'loop
GOTO redo

'display routine
nexter:

'open temp data file
OPEN "87654321.TMP" FOR RANDOM ACCESS READ AS 1 LEN = LEN(DataFile)

'get max lines in file (max records)
Total = LOF(1) / LEN(DataFile)

'clear screen
CLS

'go to line 2, character 1
LOCATE 2, 1

'display first 20 lines by reading from temp data file
FOR t = 1 TO 20
GET 1, t, DataFile
PRINT DataFile.LineOfText
NEXT t

'set min/max lines displayed on screen
Minimum = 1
Maximum = 20

'Display program title and usage keys on line 22
COLOR 15, 7: LOCATE 1, 1: PRINT " "
LOCATE 1, 1: PRINT COMMAND$
LOCATE 22, 1: COLOR 9, 7: PRINT "CGFiend's Text Engine v1.0"; : COLOR 15, 7: PRINT " Up,Down,PgUp,PgDn,Home,End,Q=Quit ": COLOR 7, 0

'another return point, this one is for user input
redo2:

'trap for a key, if = nothing goto byp
k$ = INKEY$: IF k$ = "" THEN GOTO byp

'if not a scroll key (2 character input) go to byp: and check for
'single character input (Q = Quit)
IF LEN(k$) = 1 THEN GOTO byp

'up
IF ASC(MID$(k$, 2, LEN(k$))) = 72 THEN
IF Minimum - 1 <= 0 THEN Minimum = 1 ELSE Minimum = Minimum - 1: Maximum = Maximum - 1
LOCATE 2, 1: FOR t = Minimum TO Maximum: GET 1, t, DataFile: PRINT DataFile.LineOfText: NEXT t
END IF

'down
IF ASC(MID$(k$, 2, LEN(k$))) = 80 THEN
IF Maximum + 1 > Total THEN Maximum = Total ELSE Maximum = Maximum + 1: Minimum = Minimum + 1
LOCATE 2, 1: FOR t = Minimum TO Maximum: GET 1, t, DataFile: PRINT DataFile.LineOfText: NEXT t
END IF

'left
IF ASC(MID$(k$, 2, LEN(k$))) = 73 THEN
IF Minimum - 20 <= 0 THEN Minimum = 1: Maximum = 20 ELSE Minimum = Minimum - 20: Maximum = Maximum - 20
LOCATE 2, 1: FOR t = Minimum TO Maximum: GET 1, t, DataFile: PRINT DataFile.LineOfText: NEXT t
END IF

'right
IF ASC(MID$(k$, 2, LEN(k$))) = 81 THEN
IF Total < 20 THEN GOTO skipper
IF Maximum + 20 >= Total THEN Maximum = Total: Minimum = Total - 19 ELSE Maximum = Maximum + 20: Minimum = Minimum + 20
LOCATE 2, 1: FOR t = Minimum TO Maximum: GET 1, t, DataFile: PRINT DataFile.LineOfText: NEXT t
skipper:
END IF

'home
IF ASC(MID$(k$, 2, LEN(k$))) = 71 THEN
Minimum = 1: Maximum = 20
LOCATE 2, 1: FOR t = Minimum TO Maximum: GET 1, t, DataFile: PRINT DataFile.LineOfText: NEXT t
END IF

'end
IF ASC(MID$(k$, 2, LEN(k$))) = 79 THEN
Maximum = Total: Minimum = Total - 19
LOCATE 2, 1: FOR t = Minimum TO Maximum: GET 1, t, DataFile: PRINT DataFile.LineOfText: NEXT t
END IF

'a label for skipping scroll key checks if len(K$) = 1
byp:

'exit to DOS if Q pressed
IF UCASE$(k$) = &quot;Q&quot; THEN CLS : PRINT &quot;Text Engine v1.0&quot;: PRINT &quot;(c) 1995 CGFiend&quot;: PRINT : GOTO endit

'status bar on line 1
'display # of last line on screen
'calculate location in file you are viewing as percent
perc = INT((Maximum * 100) / Total)
LOCATE 1, 20: COLOR 14, 7: PRINT &quot;Line: &quot;; Maximum; &quot; &quot;
LOCATE 1, 40: COLOR 8, 7: PRINT perc; &quot;% &quot;: COLOR 7, 0

'go back and check for keys
GOTO redo2

'error occurred, assume file not found
preend:
PRINT : PRINT &quot;Path\Filename.Ext not found.&quot;: PRINT : PRINT

'skip kill of temp file since no temp file created
RESUME endit2

'close and kill temp file
endit:
CLOSE 1
KILL &quot;87654321.TMP&quot;

'end
endit2:
END

'sub to strip chr$(12) (linefeed)
SUB strip (a$)
k$ = a$
IF INSTR(k$, CHR$(12)) THEN
a$ = CHR$(32) + MID$(k$, 2, LEN(k$))
ELSE k$ = &quot;&quot;
END IF
IF a$ = &quot;&quot; THEN a$ = STRING$(80, 32)
END SUB

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top