Qbasicnews.com
February 22, 2020, 07:49:32 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 [2]
  Print  
Author Topic: How can QBasic write to certain lines of a file?  (Read 6470 times)
Moneo
Na_th_an
*****
Posts: 1971


« Reply #15 on: November 26, 2004, 01:08:55 AM »

Licentia, I'm in a bit of a rush. I'll get back to you.
*****
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #16 on: November 27, 2004, 10:12:42 PM »

Quote from: "Licentia"
Quote from: "Moneo"
Licentia, A few suggestions on your code.

1) You shouldn't trust the user to give you the correct number of lines on the file.  Open, read, and count every record into your variable A.



  I don't know yet how to do any differently, other than maybe to say that if the variable < 0 or something then don't load.


There are fancier ways to do this, but I always use the following straightforward way to find out how many records are on a file:
Code:
dim numrecs as long   'in case more than 32k
open "thefile" for input as #1
do while not eof(1)
     line input #1,r$
     numrecs=numrecs+1
loop
close #1
rem Now you have number of records in numrecs. Don't trust the user to input this correctly.


Quote from: "Moneo"

5) Actually, you dont need to store the data in an array. Open the input file and open a different output file. Then, as you read a record from the input file, write it to the output file using the semicolon technique. This way you don't need to figure out how many records there are, and no need for an array. Again, if the number of records is too large, the PRINT comand will hang up because it will be holding too many bytes in it's buffer, because of the semicolon.


 
Quote from: Licentia
I've seen other QuickBasic software that doesn't have any problems inputing large amounts of data.  Inputing 12,000 plus lines of 6/49 combinations from a file without any output to another file whatsoever.  I haven't found a way to do that.


Perhaps those programs you saw used "far" memory or somthing. I'm not really up to speed on these techniques. The point is, like I said before, you don't need to store all the data records for this application that we're working on.
*****
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #17 on: November 29, 2004, 03:53:01 PM »

Quote from: "Moneo"
Perhaps those programs you saw used "far" memory or something. I'm not really up to speed on these techniques. The point is, like I said before, you don't need to store all the data records for this application that we're working on.
*****


Here's a program of mine to store a file of any size (< 32 MB) into memory (it uses the NeoLib v1.6 VXMS Module):

[syntax="QBasic"]'@@@@@@@@@@@@@@@@
' Standard Stuff
'@@@@@@@@@@@@@@@@
DEFINT A-Z
'$DYNAMIC
'$INCLUDE: 'NeoLib.bi'

'@@@@@@@@@@@@@@@@@@@@@@@@
' Get length of the file
'@@@@@@@@@@@@@@@@@@@@@@@@
MYFILE$ = "ABC.TXT"
FF = FREEFILE
OPEN MYFILE$ FOR BINARY AS #FF
   l& = LOF(FF)
CLOSE #FF

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
' Check if virtual xms storage is possible
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF NOT neoVXMSinit THEN PRINT "You need Virtual XMS!": END
kbs& = l& \ 1024 + ABS(l& MOD 1024 > 0)
IF neoVXMSfree& < kbs& THEN PRINT "You don't have enought Virtual XMS!": END

'@@@@@@@@@@@@@@@@@
' Allocate memory
'@@@@@@@@@@@@@@@@@
Handle% = neoVXMSalloc(kbs&)

'@@@@@@@@@@@
' Open File
'@@@@@@@@@@@
FF = FREEFILE
buffersize = FRE("") \ 5
got& = 0
OPEN MYFILE$ FOR BINARY AS #FF

   '@@@@@@@@@@@@@@@@@@@@@@@@@@
   ' Read all bytes from file
   '@@@@@@@@@@@@@@@@@@@@@@@@@@
   DO WHILE got& < l&

      '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
      ' Adjust buffer size if necessary
      '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
      IF got& + buffersize > l& THEN buffersize = l& - got&
      Buffer$ = SPACE$(buffersize)
      GET #FF, , Buffer$

      '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
      ' Write data in buffer to Virtual XMS
      '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
      neoVXMSwrite VARSEG(Buffer$), SADD(Buffer$), Handle%, got&, buffersize
 
      '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
      ' Increase counter and loop stuff
      '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
      got& = got& + buffersize
   LOOP
CLOSE #FF

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
' Don't forget to deallocate the Virtual XMS
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
neoVXMSdealloc Handle%

'@@@@@@@@@@@@@@@
' Close program
'@@@@@@@@@@@@@@@
SYSTEM[/syntax]

Doesn't help much to solve your problem, but this is an example of those "programs" that can read and store big files in memory.
If you don't have the NeoLib v1.6b VXMS Module, you can download it from Pete's QB Site, in the Library Downloads. Wink
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #18 on: December 04, 2004, 05:45:28 PM »

Why do you guys insist on storing the input file in memory, when the requirements don't call for that? Is it that you just like to do things the complicated way? Remember, the complicated way of storing all the data into memory, is not going to work for large files.
*****
Logged
Pages: 1 [2]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!