Qbasicnews.com

Full Version: HELP!!!!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I don't know crap about QBASIC, except for a few things i've been able to read here and there. I need to read in a text file line by line, each line as an array. then I need to change elements 6 trhough 9 and create a new output file. any idea? Wink
Yeah. Use DIM to make your array, OPEN the file FOR INPUT AS #1, do a loop WHILE NOT EOF(1), read the file with INPUT #1, array, WEND, then CLOSE the file.

Try to do it. If you get stuck then we'll help you.
thanks, i'll go try that right now
ok, this is what i got so far, and it's not working:

CLS
DIM INFO$(500)
OPEN "T:\SAMPNET\SAMPLE.DAT" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, INFO$
A = INT(RND * 9)
B = INT(RND * 9)
C = INT(RND * 9)
D = INT(RND * 9)
INFO(1) = A
INFO(2) = B
INFO(3) = C
INFO(4) = D
WEND
CLOSE
I think I know what you are trying. You are trying to read a file to memory, then change some elements, the write the file again... I guess the elements are numbers, and that the 500 cells array is big enough. If this doesn't work, then tell me which kind of file you are trying to open.

Code:
DIM Info%(500)
OPEN "thefile.fil" FOR INPUT AS #1
index%=0
WHILE NOT EOF(1)        ' While not end of file 1
   INPUT #1, Info%(index%)   ' Read from file.
   index% = index% + 1         ' next time, next array cell.
WEND
CLOSE #1
' Now the data in the file is in memory. Let's change values
' 6, 7, 8 and 9. I dunno what you want to do with your data,
' so I'll just put random values
FOR i%=6 TO 9
   Info%(i%) = INT(RND*10)+1
NEXT i%
' Now write the file:
OPEN "thefile.out" FOR OUTPUT AS #1
' Note that index% is the number of records plus 1, so...
FOR i%=0 TO index%-1
   PRINT #1, Info(i%)
NEXT i%
CLOSE #1
CLS
DIM INFO$(500)
OPEN "T:\SAMPNET\SAMPLE.DAT" FOR INPUT AS #1
OPEN "T:\SAMPNET\SAMPLE.OUT" FOR OUTPUT AS #2
WHILE EOF(1)
INPUT #1, INFO$
PRINT #2, INFO$
WEND
CLOSE


WELL ACTUALLY I TRIED THIS CODE I JUST POSTED AND IT CREATES A NEW FILE WITH THE SAME DATA. SO I THOUGHT I WAS GOING ALONG THE RIGHT TRACK. THE PROBLEM IS THAT THE FIELDS I WANT TO CHANGE ARE THE LAST FOUR DIGITS OF A NUMBER, BUT MY ARRAY HAS LETTERS AND NUMBERS, SO I DECLARED IT WITH $. IF I TRY TO RANDOMLY GENERATE THE INTEGERS FOR THE LAST FOUR DIGITS IT WON'T WORK, BECAUSE I'LL HAVE THE VALUES STORED IN AN INT VARIABLE AND MY ARRAY IS $, I'M SURE THERE'S GOT TO BE A WAY TO CONVERT THE VARIABLE, BUT I HAVE NO CLUE WHAT IT IS. HERE IS HOPING YOU DO Wink
well, here's the way files work: you either have input (reading), output (writing) or append (adding to). if you want to merely *change* something, you have two options:

read the entire file, change what's needed, rewrite the entire file

or

somehow figure out the exact position in bytes of that part, and change it with binary access. (get # and put #)