Reading and Writing to the buffer

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
bigjoe11a
Coder
Posts: 17
Joined: Sun Jun 10, 2007 10:43 am
Contact:

Reading and Writing to the buffer

Post by bigjoe11a » Mon Jun 11, 2007 8:56 am

Does any one know how to read and write from the buffer

I can't beleave that no one knows how to do this

Toper
Topers BBS

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Re: Reading and Writing to the buffer

Post by moneo » Mon Jun 11, 2007 1:16 pm

bigjoe11a wrote:Does any one know how to read and write from the buffer

I can't beleave that no one knows how to do this

Toper
Topers BBS
What "buffer" are you talking about?

Regards..... Moneo
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.

bigjoe11a
Coder
Posts: 17
Joined: Sun Jun 10, 2007 10:43 am
Contact:

Re: Reading and Writing to the buffer

Post by bigjoe11a » Mon Jun 11, 2007 1:23 pm

moneo wrote:
bigjoe11a wrote:Does any one know how to read and write from the buffer

I can't beleave that no one knows how to do this

Toper
Topers BBS
What "buffer" are you talking about?

Regards..... Moneo
ok, no one has ever ask me that before. Just like reading and writing to text files. reading and writing to the buffer. I only been doing this for a week an and 1/2.

I don't know how to answer that

Toper

Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph » Mon Jun 11, 2007 9:51 pm

From the Wikipedia:
Buffer (computer science), memory used to temporarily store output or input data.

"Writing" to a buffer:
When you read a file from your hard drive, from a floppy, etc. you first OPEN the file, and assign a buffer file number, as in the code line:

Code: Select all

OPEN "C:\Letters\MyText.txt" FOR INPUT AS #3
which reads:
Open the file MyTextr.txt that is on my hard drive (C:\), in folder, or directory Letters, for reading (FOR INPUT) into the computer's memory, and assign (AS) an area of memory that will be referred to as buffer file #3. The data in MyText.txt is now in the computer memory, in the space called, simply, "#3".
Next, you proceed to read from the buffer file #3, and write to a string variable, say, A$, and proceed to print it to the screen:

Code: Select all

  LINE INPUT #3, A$ 'read the first line of buffer file #3
  PRINT #3, A$
For more information, in QuickBASIC, go to Help, Index, enter O, and double click on OPEN. Also, CLOSE.

And, for even more information, please read any of the many QuickBASIC tutorials that are available.
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish

bigjoe11a
Coder
Posts: 17
Joined: Sun Jun 10, 2007 10:43 am
Contact:

Post by bigjoe11a » Mon Jun 11, 2007 9:57 pm

Ralph wrote:From the Wikipedia:
Buffer (computer science), memory used to temporarily store output or input data.

"Writing" to a buffer:
When you read a file from your hard drive, from a floppy, etc. you first OPEN the file, and assign a buffer file number, as in the code line:

Code: Select all

OPEN "C:\Letters\MyText.txt" FOR INPUT AS #3
which reads:
Open the file MyTextr.txt that is on my hard drive (C:\), in folder, or directory Letters, for reading (FOR INPUT) into the computer's memory, and assign (AS) an area of memory that will be referred to as buffer file #3. The data in MyText.txt is now in the computer memory, in the space called, simply, "#3".
Next, you proceed to read from the buffer file #3, and write to a string variable, say, A$, and proceed to print it to the screen:

Code: Select all

  LINE INPUT #3, A$ 'read the first line of buffer file #3
  PRINT #3, A$
For more information, in QuickBASIC, go to Help, Index, enter O, and double click on OPEN. Also, CLOSE.

And, for even more information, please read any of the many QuickBASIC tutorials that are available.
No, thats reading and writing to a text file.

Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph » Mon Jun 11, 2007 11:27 pm

Copy some text file to a floppy on the A:\ drive, let's say, the file, "AnyFile.txt"

Now, enter this little program in QuickBASIC:

Code: Select all

CLS
OPEN "A:\AnyFile.txt" FOR INPUT AS #1
  WHILE NOT EOF(1)
    WHILE INKEY$ = "":WEND
    LINE INPUT #1, A$
    PRINT A$
  WEND
CLOSE #1
Run the program. You will see the A:\ drive turn on. As soon as the program stops, at the line,
WHILE INKEY$ ="":WEND
Open drive A:\ and take the floppy out. Obviously, the computer will not be able to read anything from that drive, as long as the floppy is not there.

Now, press the spacebar. The program continues to run READS THE FIRST LINE FROM THE BUFFER FILE #1, assigns it to the variable A$, and, in the next program line, prints it to the screen. The program continues on, goes up to the line,
WHILE INKEY$ = "":WEND
and stops, until you press the spacebar again.
Notice that the A:\ drive is not turning on, and that it cannot read anything, as it is open!!!

Hold the spacebar down, and the program continues to read line after line from the computer's memory, from buffer file #1, until it runs out of data, at which time the program ends.

So, the program first opened the file, AnyFile.txt on drive A:\ and wrote it into the computer memory's area assigned to the buffer file #1. Later, it reads the buffer file #1, one line at a time, assigns it to the variable, A$, then prints it to the screen.

I hope this clarifies the "writing to" and "reading from" a buffer!
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish

bigjoe11a
Coder
Posts: 17
Joined: Sun Jun 10, 2007 10:43 am
Contact:

Ah, No

Post by bigjoe11a » Tue Jun 12, 2007 5:47 am

Ralph wrote:Copy some text file to a floppy on the A:\ drive, let's say, the file, "AnyFile.txt"

Now, enter this little program in QuickBASIC:

Code: Select all

CLS
OPEN "A:\AnyFile.txt" FOR INPUT AS #1
  WHILE NOT EOF(1)
    WHILE INKEY$ = "":WEND
    LINE INPUT #1, A$
    PRINT A$
  WEND
CLOSE #1
Run the program. You will see the A:\ drive turn on. As soon as the program stops, at the line,
WHILE INKEY$ ="":WEND
Open drive A:\ and take the floppy out. Obviously, the computer will not be able to read anything from that drive, as long as the floppy is not there.

Now, press the spacebar. The program continues to run READS THE FIRST LINE FROM THE BUFFER FILE #1, assigns it to the variable A$, and, in the next program line, prints it to the screen. The program continues on, goes up to the line,
WHILE INKEY$ = "":WEND
and stops, until you press the spacebar again.
Notice that the A:\ drive is not turning on, and that it cannot read anything, as it is open!!!

Hold the spacebar down, and the program continues to read line after line from the computer's memory, from buffer file #1, until it runs out of data, at which time the program ends.

So, the program first opened the file, AnyFile.txt on drive A:\ and wrote it into the computer memory's area assigned to the buffer file #1. Later, it reads the buffer file #1, one line at a time, assigns it to the variable, A$, then prints it to the screen.

I hope this clarifies the "writing to" and "reading from" a buffer!
What your talking about is not the buffer. Your talking about is a file file. or Reading and writing to a text file.

You know like copy and paste. You can write to a buffer, and then read from or then save it to a text file.

I don't know of any other way to define what I mean. Maybe theres no way to do to read or write to a buffer.

Thanks

User avatar
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Post by sid6.7 » Tue Jun 12, 2007 12:05 pm

well what it sounds like is you want to store something in
EMS or XMS memory and not an actual file...

here is one explaination of a "buffer" :

Code: Select all

Buffer:

When referring to memory a buffer is temporary storage in memory used to temporarily store information while other information is being processed. 


The Buffer RAM is a vital part of the operation of the Hard Drive. This buffer is a temporary storage for data before it is written to or read from the unit. 
The buffer RAM stores the most-accessed files so that the drive will not physically have to retrieve them. A well-designed Hard Drive buffer can speed up the overall performance of the unit. Until recently, buffer sizes where 512K or smaller, while many of the fastest drives currently available offer 2-8 MB of RAM. 

Unfortunately, a larger buffer does not necessarily mean a faster drive. Although a large buffer can be very important in determining the speed
what would help is if you could tell us SPECIFICALLY what your trying
to do.....

here is an article on using EMS with quickbasic...

http://www.phatcode.net/articles.php?id=155

bigjoe11a
Coder
Posts: 17
Joined: Sun Jun 10, 2007 10:43 am
Contact:

Temp storage

Post by bigjoe11a » Tue Jun 12, 2007 12:16 pm

sid6.7 wrote:well what it sounds like is you want to store something in
EMS or XMS memory and not an actual file...

here is one explaination of a "buffer" :

Code: Select all

Buffer:

When referring to memory a buffer is temporary storage in memory used to temporarily store information while other information is being processed. 


The Buffer RAM is a vital part of the operation of the Hard Drive. This buffer is a temporary storage for data before it is written to or read from the unit. 
The buffer RAM stores the most-accessed files so that the drive will not physically have to retrieve them. A well-designed Hard Drive buffer can speed up the overall performance of the unit. Until recently, buffer sizes where 512K or smaller, while many of the fastest drives currently available offer 2-8 MB of RAM. 

Unfortunately, a larger buffer does not necessarily mean a faster drive. Although a large buffer can be very important in determining the speed
what would help is if you could tell us SPECIFICALLY what your trying
to do.....

here is an article on using EMS with quickbasic...

http://www.phatcode.net/articles.php?id=155
Ok, I want to write to the buffer before I write to the disk file

Lets say I have

Name
Location
Age

Before I write these to the disk file. The user mite want to change them,

or

Lets say I have the disk file with these lines

Jobs
Auto's
and so on

what happens when I want to delete the Auto's from the file. Instead of creating 2 disk files. I would copy the one I want to keep in the buffer and then delete the old disk disk and rewrite the contents of the buffer to the disk file

I hope that helps

User avatar
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations » Wed Jun 13, 2007 10:53 am

You could load the list of strings into the memory buffer as variables in an array. Using A:\AnyFile.Txt again...

Code: Select all

DIM Buffer$(500)      'Change to line numbers in file
OPEN "A:\AnyFile.txt" FOR INPUT ACCESS READ AS #1
DO
LINE INPUT #1, Buffer$(A%)
A% = A% + 1
LOOP UNTIL EOF(1)
Then, each line will be loaded as a seperate element of Buffer$. You can freely edit these, and then write them to a file (the same file if you really want to).

bigjoe11a
Coder
Posts: 17
Joined: Sun Jun 10, 2007 10:43 am
Contact:

No, thats not it

Post by bigjoe11a » Wed Jun 13, 2007 1:20 pm

Patz QuickBASIC Creations wrote:You could load the list of strings into the memory buffer as variables in an array. Using A:\AnyFile.Txt again...

Code: Select all

DIM Buffer$(500)      'Change to line numbers in file
OPEN "A:\AnyFile.txt" FOR INPUT ACCESS READ AS #1
DO
LINE INPUT #1, Buffer$(A%)
A% = A% + 1
LOOP UNTIL EOF(1)
Then, each line will be loaded as a seperate element of Buffer$. You can freely edit these, and then write them to a file (the same file if you really want to).
I thought there would be a way to read a write to the memory buffer. Maybe there isn't a way to do this is Basic

What your doing is reading from a file. Thats fine. Thats what I wanted to avoid until I'm ready to write it to a file.

Thanks for your time

historian
Newbie
Posts: 4
Joined: Sat Jun 16, 2007 12:11 pm

Post by historian » Sat Jun 16, 2007 5:55 pm

I'm just guessing here, but what you seem to want to "write" to a string as if it was a file, kind of like snprintf in C. You're right: QBASIC doesn't support this (directly), but you can implement a few SUBs and FUNCTIONS that will make it hard to tell the difference. Let's suppose you'd be happy with a read and write command, similar to the Linux system calls, except they work on strings instead of files.

Unfortunately, QBASIC is very picky about what we call our SUBs and FUNCTIONs, so we have to call our routines readbuf and writebuf. Like Linux's read and write, they operate on an integer "descriptor" that represents the buffer.

Furthermore, QBASIC doesn't allow structured types to contain variable-length strings or any kind of array.

QBASIC also doesn't have a way to lengthen an array without erasing the contents. But you can lengthen strings that aren't part of TYPEs with impunity (within the 32,767-character limit, of course).

What this is leading up to is a need to use global variables, which is generally considered a bad thing. If we were writing this in C++, we would declare no global variables. But, since we have no choice here in QB, we'll do it anyway:

Code: Select all

    CONST maxStreams = 100
    DIM SHARED streams(maxStreams) AS STRING
    DIM SHARED positions(maxStreams) AS INTEGER
    DIM SHARED numStreams AS INTEGER
This allows there to be many streams in memory that can be "read" or "written to". numStreams holds the number of currently-active streams.

The only fortunate thing is that we can put this code in its own module, and then only FUNCTIONs and SUBs from that module can access the variables. I'll bet Microsoft never realized that QuickBASIC supports object-oriented programming, complete with private data members! Having said that, QuickBASIC still sucks ass, and you should really consider migrating to a real programming language, like Python.

We'll need a function to allocate a new stream. Since our streams are allocated on a stack, we'll call our allocation function pushbuf%. It returns an integer descriptor that will be accepted by the other functions that make up the interface.

Code: Select all

FUNCTION pushbuf%
    pushbuf% = numStreams
    numStreams = numStreams + 1
END FUNCTION
And here's a SUB that frees the most recently allocated stream:

Code: Select all

SUB popbuf
    ' Reset the stream and deallocate memory.
    positions(numStreams-1) = 0
    streams(numStreams-1) = ""
    numStreams = numStreams - 1
END SUB
Here's a couple of procedures that can be used to manipulate the current position in the buffer. tellbuf% tells you the current position in the buffer (like ANSI C's ftell), and seekbuf sets the position within the stream:

Code: Select all

FUNCTION tellbuf%(desc AS INTEGER)
    tellbuf = positions(desc)
END FUNCTION

SUB seekbuf(desc AS INTEGER, position AS INTEGER)
    IF position > LEN(streams) THEN
        position = LEN(streams)
    END IF
    positions(desc) = position
END SUB
Finally, we can write our functions for "reading" and "writing" a buffer. WARNING: MID$ is 1-based, going against convention:

Code: Select all

FUNCTION readbuf% (desc AS INTEGER, outbuf AS STRING, count AS INTEGER)
    outbuf=""
    IF desc >= numStreams THEN
        ' Bad file name or number
        ERROR 52
    END IF
    IF positions(desc) > LEN(streams(desc)) THEN
        ' Input past end of file
        ERROR 62
    END IF
    outbuf = MID$(streams(desc), positions(desc)+1, count)
    positions(desc) = positions(desc) + LEN(outbuf)
    readbuf% = LEN(outbuf)
END FUNCTION

SUB writebuf(desc AS INTEGER, inbuf AS STRING)
    IF desc >= numStreams THEN
        ' Bad file name or number
        ERROR 52
    END IF
    streams(desc) = streams(desc) + inbuf
    positions(desc) = positions(desc) + LEN(inbuf)
END FUNCTION
Save all the above to one module, STREAMS.BAS, and use File|Create File to create your main module. (Set the Main Module in Run|Set Main Module or your program will do nothing)

Here is a main main module that shows how to use the new interface:

Code: Select all

' These declarations are absolutely necessary.
DECLARE FUNCTION pushbuf% ()
DECLARE FUNCTION tellbuf% (desc AS INTEGER)
DECLARE FUNCTION readbuf% (desc AS INTEGER, outbuf AS STRING, count AS INTEGER)
DECLARE SUB writebuf (desc AS INTEGER, inbuf AS STRING)
DECLARE SUB seekbuf (desc AS INTEGER, position AS INTEGER)
DECLARE SUB popbuf ()

' Our variables.
DIM fd AS INTEGER
DIM hello AS STRING
DIM status AS INTEGER

CLS
' Work with a new buffer.
fd = pushbuf%

' Store the string "Hello, world!" in the buffer
writebuf fd, "Hello, world!"

' Seek the beginning of the buffer.
seekbuf fd, 0

' Retrieve the first 5 bytes stored in the buffer.
status = readbuf%(fd, hello, 5)

' Print the retrieved bytes
PRINT hello

' Retrieve five more bytes from the buffer.
status = readbuf%(fd, hello, 5)
PRINT hello ' Different this time.
________
Buy Vaporizer
Last edited by historian on Thu Feb 10, 2011 9:29 pm, edited 1 time in total.

bigjoe11a
Coder
Posts: 17
Joined: Sun Jun 10, 2007 10:43 am
Contact:

That seem too much

Post by bigjoe11a » Sat Jun 16, 2007 7:12 pm

Thats a lot of code just to read and write to a buffer. Thanks

User avatar
sid6.7
Veteran
Posts: 318
Joined: Tue Jun 21, 2005 8:51 am
Location: west USA
Contact:

Re: That seem too much

Post by sid6.7 » Sat Jun 16, 2007 9:51 pm

bigjoe11a wrote:Thats a lot of code just to read and write to a buffer. Thanks
thats old qbasic for you....it generally takes a bit more coding
to do the same thing as some of the higher level languages.

its probably just as easy to not try the buffer storage thing
with qbasic and just handle things with files.

bigjoe11a
Coder
Posts: 17
Joined: Sun Jun 10, 2007 10:43 am
Contact:

Thanks

Post by bigjoe11a » Sat Jun 16, 2007 9:54 pm

Yes, I guess so. Maybe I'll just stick with text files

Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph » Sun Jun 17, 2007 2:23 pm

Why couldn't one use something like this:

Code: Select all

CLS

DIM buf1 AS STRING
buf1 = "Hello, world"

PRINT buf1
PRINT MID$(buf1, 1, 5)
PRINT SPC(5); MID$(buf1, 5 + 1, 4)
PRINT SPC(5 + 4); MID$(buf1, 5 + 4 + 1, LEN(buf1) - 5 - 4)
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish

Post Reply