Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with text files 1

Status
Not open for further replies.

kylep

Programmer
Jul 21, 1999
2
CA
I need to know how to work with text files. I am writing a<br>
program that i need to write information on to the hard drive but i need to know how to access certain lines of the text file and how to write to certain lines. If anybody understands what i need and can help me it would be greatly appreciated
 
Text files are a little bit of a pain to work with in the way you want.<br>
<br>
It's generally easier to modify a text file by:<br>
1) rename it<br>
2) open the renamed version - and a new file with the old name<br>
3) read through the renamed version, copy everything you read to the new file until...<br>
4) you get to the line you want to change - write the changed version of that line out to the new file<br>
5) write the rest of the renamed file out to the new file.<br>
<br>
This will work "ok" until the file gets "big".<br>
<br>
There are several problems with text files for this kind of work. When the file gets big - and varies depending upon the speed of the computer system manipulating it - changes will be slow. It's difficult to change data in a text file because the lines are all different lengths<br>
<br>
It's often easier to read the text files into a database of some sort and manipulate the data there.<br>
<br>
Hope I understood your query correctly, let us know how you get on.<br>
<br>
Mike<br>

 
Ok, let´s see:<br>
<br>
Put a Textbox control in your form, change the multiline property to true, also change the scroll property to Both.<br>
<br>
To read a text file into the textbox:<br>
<br>
Private Sub read_file()<br>
dim txtpath as string<br>
dim txtin as string<br>
<br>
' Set variable to hold the location<br>
' of the text file<br>
txtPath = App.Path & "\test.txt"<br>
' error handle the variable<br>
If InStr(thefile, "\\") Then _<br>
txtpath = App.Path & "test.txt"<br>
' open the text file to be read from<br>
Open txtPath For Input As #1<br>
' set the input variable to the contence<br>
' of the text file<br>
txtIn = Input$(LOF(1), #1)<br>
<br>
' set the text box text to the variable<br>
Text1.Text = txtIn<br>
' close the file<br>
Close #1<br>
End Sub<br>
<br>
Now you can make the changes to the textbox (text1).<br>
<br>
Now you want to put the content of the textbox into a textfile.<br>
<br>
Private Sub CmdSave_Click()<br>
dim txtpath as string<br>
dim txtout as string<br>
<br>
' Set variable to hold the location<br>
' of the text file<br>
txtPath = App.Path & "\test.txt"<br>
' error handle file location<br>
If InStr(thefile, "\\") Then _<br>
txtpath = App.Path & "test.txt"<br>
' set variable as the contence of the<br>
' text box<br>
txtOut = Text1.Text<br>
' open the text file to be<br>
' writen to<br>
Open txtPath For Output As #1<br>
' write the contence of the variable to<br>
' the text file<br>
Print #1, txtOut<br>
' close the text file<br>
Close #1<br>
' clear the text box<br>
Text1.Text = ""<br>
End Sub<br>
<br>
Hope this help you to understand simple text file manipulation.<br>
<br>
You have a 64K limit, for the textbox, so be carefull.<br>

 
I thankyou for the reply, but I already know how to do simple text file manipulation. What I need to know how to do is access specific lines of textfiles and write to those lines without changing the other lines.<br>
<br>
If you know who to do this, it would be greatly appreciated.<br>
<br>
<br>
Kyle P
 
Try a binary file with a data structure.<br>
In that case you can go to any part of the file, with a record reference.<br>
Make the data structure a 250 characters long string<br>

 
VB supports text file IO but it is not documented very well. Following are some exaples:<br>
<br>
<br>
' read the file into an array<br>
<br>
gstrDOSFilename = gstrPathName & gstrBillCntlFilename<br>
nRecLen = Len(ControlRec(1))<br>
<br>
Open gstrDOSFilename For Random Access Read Shared As #1 Len = nRecLen<br>
<br>
nMaxRecs = ((LOF(1) / nRecLen))<br>
<br>
For i = 1 To nMaxRecs<br>
Get #1, i, ControlRec(i)<br>
Next i<br>
Close #1<br>
<br>
<br>
<br>
' write records to temp file<br>
Open strTempFile For Random Shared As #1 Len = nRecLen<br>
<br>
For i = 1 To intRecIdCount<br>
Put #1, , ControlRec(intRecordIds(i))<br>
intRecsWritten = intRecsWritten + 1<br>
Next i<br>
<br>
Close #1<br>
<br>
<br>
In Help, look-up Open/Get/Put/Close. Hope this helps, respond if you need more info.
 
Folk,<br>
<br>
VB and Windows support initialization files (.ini), which allow almost exactly what [I think] has been requested.<br>
<br>
With just two or three API declarations, you may access any part of an ASCII file (in the .ini format) so long as the file size is less than 64K.<br>
<br>
The format is pretty simple, as it is a section header with key/value data pairs below.<br>
<br>
For instance, the following is an example.<br>
<br>
[section name one]<br>
key01 = value (think this is good for a K or so of text)<br>
key02 = value (can be any data type)<br>
key03 = value (will be treated as string)<br>
key04 = value (int is special case)<br>
<br>
[section number two]<br>
. <br>
. <br>
. <br>
[section n]<br>
.<br>
.<br>
.<br>
<br>
This may not fill the bill for the original request, but it does fit the criteria of the request as I read them.<br>
<br>
kylep,<br>
You would address this file by specifying the appropriate headder, then the key below that. Then whatever change you make would be assigned to the value associated with that header and key. If this looks attractive for your needs, say so and I'll post specific code for it. This can be either 16-bit or 32-bit, so mention what flavour of VB you are using. The code doesn't change, but the API declarations are a little different from 16-bit to 32-bit.<br>
<br>
Make a good day . . .<br>
. . . barn<br>
<br>
<br>
<br>

 
There are a lot of good suggestions up there for you Kyle but we all need to know:<br>
<br>
Is each line the same length? If they were then you would have a way of knowing the byte position of the beginning and end of each line(use binary access &gt;&gt; Get #filenum, byte#, StringtoFill &lt;&lt;) . How are you going to know what line you are going to access. You could read line by line looking for the carriage return. If you are going to change the line when you find it...you are going to have to write to that line completely...so once again you need to know the length of the line. Sequential access allows line to line access...but I don't think you can go to a specific line. I don't know...anybody know about this?
 
positran,<br>
<br>
About the only way you can go to a specific line with sequential access is by looping through the file and applying some sort of search or match criteria to each line until you find the one you [think] you want. Sequential access does not allow for specifying a specific offset: you have to use some kind of workaround. However, you can 'index' the file after having read through it once - but the index gets bigger than the file, and is expensive in both memory and disk I/O.<br>
<br>
kylep,<br>
<br>
positran has a point about the file. What format does it have/need, and do the file elements need to be contiguous, or can they be just congruent? If congruent is ok, then the .ini file approach will work. If not, then you'll need to set up a routine for handling binary file access (or sequential, perhaps). If you go the binary route, you'll need to do an instr() search for a unique string or a search for a known instance of a non-unique string. Then, to make changes, unless the changes are the exact length of the original string, you'll have to rewrite the entire file.<br>
<br>
(Hmm-m-m. Can you write changes to the file w/o rewriting the file if the changed string is same length as the original? Anyoone?)<br>
<br>
<br>
Make a good day . . .<br>
. . . barn<br>

 
D. if you create a binary file and define the struc to be 255 you HAVE a 255 limit on each line.<br>
And you can write any line w/o rewrite the whole file.<br>
You still have to read line by line to find what you want.<br>
<br>
The .ini structure can be a solution.<br>

 
How do I to manipulate that following MIF (text) file from format A to format B

A
Version 300
Charset &quot;WindowsLatin1&quot;
Delimiter &quot;,&quot;
CoordSys NonEarth Units &quot;m&quot; Bounds (1837078.087, 5698821.102) (3394395.121, 7522516.836)
Columns 1
ID Integer
Data

Pline 21
2628498.718203998 6612852.52640656
2628509.279149464 6612865.550329644
2628532.958933625 6612894.755904976
2628552.001806316 6612993.734255396
Pen (1,2,0)
Pline 3
2628594.068054039 6612435.989741675
2628623.059846602 6612381.050907688
2628673.672650207 6612307.309770684
Pen (1,2,0)


B

21 1001 1002 2628498 6612852
22 1003 1003 2628509 6612865
22 1004 1005 2628532 6612894
23 1006 1007 2628552 6612993

21 1008 1009 2628594 6612435
22 1010 1011 2628623 6612381
23 1012 1013 2628673 6612307

Where 21-23 are Interger 2, denoting start, middle and end of a line

1000 + are the &quot;x&quot; node and &quot;y&quot; nodes (Interger 6)

&quot;xxxxxxx&quot; and &quot;xxxxxxx&quot; are fixed length coordinates

The files are gnerally over 64k, and up to 600k.

How do you sugest I best go about this [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top