|
-
December 20th, 2003, 10:13 AM
#1
Writing to a certain line in text file?
Hi all,
I was just wondering, is there a way you can write data to a specific line in a text file rather than from the top.
E.g.
In my VB Prog I got 3 text boxes:
TxtQNum
TxtQuestion
TxtAnswer
The text file has data like this:
Q.1
<Q> blah blah blah
<A> blah blah blah
Q.2
<Q> ....
<A> .....
I want to say if QNum is 2 for example, then overwrite the <Q> and <A> bits only in that section.
Any info...
Thanks!
Mark
-
December 20th, 2003, 03:50 PM
#2
there's no magic bullet on this. you have to read in the entire file, writing it out line by line and inserting the new line where you want it. that's just the way files work. they're "sequential".
phinds
vs2008, 3.5SP1 Version 9.0.21022.8 RTM
-
December 20th, 2003, 06:36 PM
#3
If you know the line number, you can do it this way.
Code:
Dim A$, S$()
Open "C:\test.txt" For Binary Access Read As #1
A = String(LOF(1), 0)
Get #1, , A
Close #1
S = Split(A, vbCrLf)
S(5) = "new line here" 'replace the sixth line of the file (the array starts with zero)
A = Join(S, vbCrLf)
Open "C:\test.txt" For Binary Access Write As #1
Put #1, , A
Close #1
You can also iterate through the array if you need to do a search.
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
-
December 20th, 2003, 11:41 PM
#4
Why not use random access files, with a UDT?
Of course you would have to redesign
some of your program to use them, but they will let you write
anywhere in the file based on what record number you give it.
Code:
'Global
Private Type Question
strQuestion as string * 256
strAnswer1 as string * 128
strAnswer2 as string * 128
strAnswer3 as string * 128
strAnswer4 as string * 128
intCorrectAnswer as integer
End Type
Dim Q as Question
'To Open The File
Open "Sample.txt" For Random As #1 Len = LenB(Q)
'To Get Question Number 2
Get #1, 2, Q
'To Write The Changes To Question 2 (Will Overwrite)
Q.strQuestion = "Bla"
Q.strAnswer1 = "Bla"
Q.strAnswer2 = "Bla"
Q.strAnswer3 = "Bla"
Q.strAnswer4 = "Bla"
intCorrectAnswer = 3
Put #1, 2, Q
'Now Get Record One And Display The Question
Get #1, 1, Q
msgbox Trim(Q.strQuestion) 'this is "basically" all that needs to
'be done with
'the stinking fix-length string.
'Then Finally When Your Done Close It
Close #1
Last edited by Jinto; December 21st, 2003 at 01:42 AM.
-
December 21st, 2003, 12:59 AM
#5
Interesting, although numerics will not be stored as readable text, and it also requires fixed length strings. Non the less an interesting solution.
Please remember to rate the posts and threads that you find useful.
How can something be both new and improved at the same time?
-
December 21st, 2003, 01:24 AM
#6
Yes I agree those are the only two draw backs, I think.
Anyway if he is making an actuall editor for the questions then
who would need to see the numeric value?
In fact it will prevent people taking the test from seeing the correct
answer.
As for the fixed-length strings, thats not really a problem anymore with the size of hd's and the amount of memory computers have. Hey that up there is only around 0.7kb
Last edited by Jinto; December 21st, 2003 at 01:39 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|