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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string manipulation

Status
Not open for further replies.

met

IS-IT--Management
Jul 1, 2000
13
US
As network admin I don't get into programming much but I have a project at work where I have to read in a text file formated as:10011234564371720<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10021234562200930<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10031234568499910<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;10041234566727890<br>and so on until....EOF<br>then write out to another file formated as:<br>&nbsp;1001,123456,,,<br>&nbsp;1002,123456,,,<br>&nbsp;1003,123456,,,<br>&nbsp;1004,123456,,,<br>The first 4 dig. of input file are machine id's the next six are readings, remainder not used. On the output side it looks exactly as above truncating the the numbers beyond where the &quot;6&quot;is( could be anything,) but the commas are required as placeholders(delimited).Am using vb6. Need help!!! thanks.......&nbsp;&nbsp;<br>
 
Hi Met,<br><br>Start a new VB standard project, double click on the form.<br><br>Copy the code below into the code window, it must replace all of the default code already there.<br><br>You'll need to change the file names in my example to match what your system.<br><br><FONT FACE=monospace><b><br>Option Explicit<br><br>Private Sub Form_Load()<br>Dim f1 As Long, f2 As Long, InputString As String, OutputLine As String<br><br>&nbsp;&nbsp;&nbsp;&nbsp;f1 = FreeFile<br>&nbsp;&nbsp;&nbsp;&nbsp;Open &quot;c:\test.txt&quot; For Input As #f1&nbsp;&nbsp;&nbsp;&nbsp;' open input file<br>&nbsp;&nbsp;&nbsp;&nbsp;f2 = FreeFile<br>&nbsp;&nbsp;&nbsp;&nbsp;Open &quot;c:\out.txt&quot; For Output As #f2&nbsp;&nbsp;&nbsp;&nbsp;' open output file<br>&nbsp;&nbsp;&nbsp;&nbsp;Do While Not EOF(f1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Loop until end of input file.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Line Input #1, InputString&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Read line into variable.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OutputLine = Mid(InputString, 1, 4) ' get first four characters<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OutputLine = OutputLine & &quot;,&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' add a comma<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OutputLine = OutputLine & Mid(InputString, 5, 6) ' get next six characters<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OutputLine = OutputLine & &quot;,,,&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' add three commas<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Print #f2, OutputLine<br>&nbsp;&nbsp;&nbsp;&nbsp;Loop<br>&nbsp;&nbsp;&nbsp;&nbsp;Close #f1<br>&nbsp;&nbsp;&nbsp;&nbsp;Close #f2<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>End Sub<br></font></b><br> <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Thanks this looks like it might work. I was trying to make it too dificult I think. I was trying to use a dynamic multi-dimensional array, and was having trouble inputing the two strings using left and mid functins in the subscript assignment. Just for s&g's what is the correct way to assign these arguments to a two dimensional array? Thanks again<br>Mike(met)&nbsp;&nbsp;&nbsp;
 
FYI the rountine you suggested didn't work. For one as written it gives me the error that the outputfile is already open and when I close before the open statement it doesn't do the loop.
 
I did test it before I posted it...<br><br>Email me your code if you want. <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
met -<br><br>Make sure you've got your output file as a different name than the input file.&nbsp;&nbsp;If you absolutely have to have them the same, then after the Close statements in Mike's code, insert the following:<br><br><b> Kill &quot;C:\test.txt&quot;<br>Name &quot;C:\out.txt&quot; as &quot;C:\test.txt&quot; </b><br><br><i>Important Note:</i> This will destroy your original input file, so make sure you've got another copy somewhere safe.<br><br>Chip H.<br><br>
 
Actually I did get it to work using a variation of your code. When I staticly defined the the code handles it didn't give me the file open error. I have since found that if you define multiple file handles as: f1=freefile,f2=f1+1 it will give you the next two free files. What was happening was f1=freefile, f2=freefile was assinging file number 1 to both f1 and f2. I also change some of the assignment structure but the idea of keeping it simple I got from your code. Thanks again........<br>P.S. Just out of curiosity how would you populate a two dimensional array in this same senario.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top