Qbasic, Quickbasic/Combining files

Advertisement


Question
Hi Alex, I have a series of .txt files that are all the same number rows and columns with rows separated by commas. I would to combine the files into a single text file. Many of the commands allow me to add the contents of a file to the end, but I would like to combine the contents horizontally. For example let's say I have four text files (2 x 16389), how would I open them and combine them into one file that is 8R x 16389C? Thanks in advance - I really appreciate your help."

Answer
Hi, Tim,

If I were you, this is how I would do it:

' x is the number of files you intend on using.
CONST x%  = 100
DIM FileList(1 to x%) AS INTEGER
DIM NewFile AS INTEGER

' Open these files like this:
DIM f AS INTEGER
f = 0
' You'd have a loop something like this.  I don't know how you are opening the files (file name pattern?  "data01.txt", "data02.txt", etc.?)
DO
 f = f + 1
 FileList( f ) = FREEFILE
 OPEN "the file you want to open.txt" FOR INPUT AS #FileList( f )
LOOP WHILE x% <= f

' -- This is where they are merged
NewFile = FREEFILE
OPEN "merger.txt" FOR OUTPUT AS #NewFile
' -- You may have to manually get each piece of data (random access file?), but this will also work (perhaps not as nicely...you'll have to tinker with things here)
DIM MyData AS STRING

DO
 ' Get the (next) line from each file (and merge that line with the rest of the files)
 FOR f = 1 TO x%00
   LINE INPUT #FileList( f ), MyData
   PRINT #NewFile, MyData,
 NEXT f
 ' Move to next line in our new file
 PRINT #NewFile, ""
LOOP UNTIL EOF( FileList( 1 ) )

FOR f = 1 TO x%
 CLOSE #FileList( f )
NEXT f
CLOSE #NewFile

To make this work with your program, you'll need to do some tinkering, but this is the basic principle behind it.

Let me know if there is anything in my code you're not sure about,
-Alex

Qbasic, Quickbasic

All Answers


Answers by Expert:


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2012 About.com, a part of The New York Times Company. All rights reserved.