Page 1 of 1

Prepend to file -- help?

Posted: Mon Aug 15, 2005 11:18 pm
by Quadricode
Hello everyone, I am Quadricode!

Anyway, that is my introduction. I will get straight to my question.

I am making an arbitrary precision calculator, but I am having a problem. It is as follows:

I have the user input numbers (via INKEY$), and it is directly put into a file names Num1.txt (and Num2.txt). Everything is good so far.

My algorithms require there to be leading zeros, and I need to know how to put on the leading zeros in the text file (prepending line number 1) AS WELL AS taking carry-overs over 40 digits, and putting them into the next line.

[The following is an example file]

Original File

Code: Select all

48923678924367043297309861037640398087423
58970276487136083468437614387647643471031
16767105757657647031658704670657765
et cetera. Note that each row has 40 characters (or less). Let us pretend I need to prepend 9 zeros. How can I put the zeros in the first line, and then take the overflow (numbers past the 40 char mark), and set them into the next line?

What I need.
From above, to this:

Code: Select all

[000000000]48923678924367043297309861037640[398087423]
58970276487136083468437614387647643471031
16767105757657647031658704670657765

to

[000000000]48923678924367043297309861037640
[398087423]58970276487136083468437614387647[643471031]
16767105757657647031658704670657765

to


[000000000]48923678924367043297309861037640
[398087423]58970276487136083468437614387647
[643471031]16767105757657647031658704670657[765]

to

[000000000]48923678924367043297309861037640
[398087423]58970276487136083468437614387647
[643471031]16767105757657647031658704670657
[765]

(Brackets are the overflowing numbers/inserted numbers.)

Final Result (num1.txt):

Code: Select all

00000000048923678924367043297309861037640
39808742358970276487136083468437614387647
64347103116767105757657647031658704670657
765
I hope I was coherent and articulate. Any help would be fine. I just hope I won't have to rebuild everything everytime a carry-over has to occur.

Posted: Tue Aug 16, 2005 8:36 am
by {Nathan}
I am not quite sure what you are talking about, but thanks for posting code. I would give you two tips. Converting to strings and...

For prepending, if you mean inserting to the beginning... then do this:

Code: Select all

OPEN "swapfilethatyouputthebeginningto.txt" for output as #1
(put the stuff in here)
open "numbers.txt" for input as #2
do until EOF(2) 'this loops until it is the end of the file
input #2, SwapVar
output #1, SwapVar
loop

Posted: Tue Aug 16, 2005 12:51 pm
by Quadricode
Hmm... that is one step further. But is there anyway to generate the file that would contain the startuing numbers?

The zeros are fine, I can easily create a file with the N number of zeros I need to 'prepend' to the start.

But my problem exists when I want to limit the size of each line to 40 characters. In my last post, I called the numbers past the 40 character "limit" overflow.

If I use that code you, Nathan, used; I can only add the starting zeros, unless I can find a way to put the "overflow" of each line in that file as well.

Posted: Tue Aug 16, 2005 1:47 pm
by moneo
How about reading all the characters from your text file and creating one large string preceeded by the 9 zeros. Then, writing 40 character pieces, or less, of this large string onto a final output file.

Code: Select all

defint a-z
open "Num1.txt" for input as #1
open "Num1Out.txt" for output as #2
big$="000000000"
do while not eof(1)
     line input #1,d$
     big$=big$+d$
loop

'big$ now has the 9 zeros plus all the characters from Num1.txt.
'Assuming that the result is always 3 lines, we can just do the following.
'Otherwise we would need to do a loop based of the size of big$.
 
print #2, left$(big$,40)
print #2, mid$(big$,41,40)
print #2, mid$(big$,81)
END
*****

Posted: Fri Aug 19, 2005 8:08 pm
by moneo
Quadricode,

So, what happened? Was my suggested solution good or bad?
*****