 |
 |

10-22-2001, 09:21 AM
|
|
|
Anyone help?
|
Well I really need to figure out how to make a program to do:
Load up a file (extension mpr or mmr)
Add certain text to the file in certain places
Save the file
Since I'm fairly new at VB i have no idea how to do most of this. I was wondering if someone could point me to some source code or something to help me out.
|
|

10-22-2001, 09:36 AM
|
 |
Regular
|
|
Join Date: Oct 2001
Location: Italy
Posts: 57
|
|
Re: Anyone help?
I'll tell you everything you need cause I was a VB beginner and understand your problems. Just be patient for my english
Tell me this things just to help you
- Can you make a new standard exe project ?
- are your .mmr files like text file or are binary ones? (it's very different)
- How can you determine the position where you need to write?
try this code in an event button
Open "C:\test.txt" for output as #1
print #1, "This is a string"
Close #1
this code writes a little string in a file that you'll find in your "c:\"
Use this e-mail: giovannigranata@katamail.com or send private message to me to continue
BYE
|
|

10-22-2001, 09:39 AM
|
|
Iron-Fisted Programmer
Retired Moderator * Guru *
|
|
Join Date: Jul 2001
Location: Fayetteville Arkansas USA
Posts: 18,127
|
|
Re: Anyone help?
|
There is no way to directly insert text into an existing file. By opening the
file in binary, it would be possible to overwrite existing bytes in the file
with text. To insert text would require opening a second file, reading the
whole first file and writing it all to the second file, inserting the next text at
the appropriate spots. You will probably need to give a lot more
information before anyone here can really help you much more. For
instance, what is the format and purpose of these files? Do you need to
insert new text, or just replace text that is already there? Do you know the
exact spot(s) in the file where the new text needs to go?
|
|

10-22-2001, 09:47 AM
|
|
|
Re: Anyone help?
|
Well the files (.mpr and .mmr) are map files for a game. They can be opened in wordpad as text files.
Yes I can make a exe project (but thats about it)
For the position, I'm not sure right now,but I could figure out.
|
|

10-22-2001, 11:27 AM
|
|
|
Re: Anyone help?
|
Basically, I can combine 2 text files to make 1 output file right? And the text I need to insert does have to be in a certain place
|
|

10-22-2001, 11:40 AM
|
 |
Dead dog's ghost
Forum Leader * Expert *
|
|
Join Date: Feb 2001
Location: Celje, Slovenia, Europe
Posts: 2,601
|
|
Re: Anyone help?
|
Hi!
Basicly you can do it like this (note:BASICLY!!)
1. Open your file
Dim MyArray() As Variant 'suppose there are alfanumeric characters in the file
dim n% 'counter for an array
Dim strFile
dim strInput$
strFile=FreeFile 'ensure that the filenumber is available
Open MyFile For Input As strFile
2. Loop through it and load data into the array
n%=0
While Not EOF(1) 'do it until it riches the end of the file
Input strFile, strInput$
ReDim Preserve MyArray(n%)=strInput$
n%=n%+1
Wend
3. Close the file
Close strFile
4. Loop over an array and seek for your string and replace it
for n% =LBound(MyArray) To UBound(MyArray)
if MyArray(n%)="BILLY" Then MyArray(n%)="Johny"
Next n%
5. Save an array into the file
Open MyFile For Output As strFile
For n%=LBound(MyArray) To UBound(MyArray)
Print strFile,MyArray(n%)
Next n%
Close strFile
Hope it'll help
Regards!
Ales Zigon
|
__________________
Yes, MSDN comes with VB! Yes, you must have at least 25 post to have an avatar! No, you cant write your OS in VB! and NO, YOU CAN NOT DECOMPILE IT!
I'm sure there are things that are more important than me - I just can't thing of any...
|

10-22-2001, 04:24 PM
|
|
|
Re: Anyone help?
|
Well seeing how im a complete moron,can someone help me understand that?
|
|

10-22-2001, 09:58 PM
|
|
Senior Contributor
* Guru *
|
|
Join Date: Mar 2000
Location: Christchurch, New Zealand
Posts: 470
|
|
Re: Anyone help?
|
You ask a big question, so you may have to break it down and tackle it piece by piece.
Here is a rough list of functions you need and what they do. Look up the help for further explanation and examples.
It sounds like this is what you want to do, Open two text files, insert data and manipulate the data in the files and then combine them into one text file.
Basically, you should open the two files and load their data into strings, then do the manipulation in memory and finally write the concatenated (joined together) strings back to a new file.
1) To open a text file use the Open statement. In your case it is probably best to open the file as Binary and load all the contents into a string variable.
2) To find an occurrence of a string and change it, you can use Instr (to find the position) or Replace which replaces the word within something else. Also check out Mid$ function.
Here is an example to show you how it can hang together:
<pre><font color=green><font color=blue>Option Explicit</font color=blue>
<font color=black>' General declarations section of your form</font color=black>
<font color=blue>Private</font color=blue> mstrBuffer1 <font color=blue>As String</font color=blue>, mstrBuffer2 <font color=blue>As String</font color=blue>
<font color=blue>Private Sub</font color=blue> cmdLoad_Click()
<font color=black>'------------------------------------------------------------------------</font color=black>
<font color=black>' This command button is used to test the the function works and display</font color=black>
<font color=black>' the data.</font color=black>
<font color=black>' In your case you would do this in your main processing</font color=black>
<font color=black>'------------------------------------------------------------------------</font color=black>
<font color=blue>Dim</font color=blue> strCombinedData <font color=blue>As String</font color=blue>
<font color=black>' Load the data</font color=black>
<font color=black>'--------------</font color=black>
XLoadTextFileData "C:\Temp\Data1.txt", mstrBuffer1
XLoadTextFileData "C:\Temp\Data2.txt", mstrBuffer2
<font color=black>' Put the data together (add a new line between them) and display</font color=black>
<font color=black>'----------------------------------------------------------------</font color=black>
strCombinedData = mstrBuffer1 & vbNewLine & mstrBuffer2
<font color=black>' Replace all occurrences of the word "the" with "<<the>>"</font color=black>
strCombinedData = Replace(strCombinedData, "the", "<<the>>")
MsgBox strCombinedData, vbInformation
<font color=blue>End Sub</font color=blue>
<font color=blue>Private Sub</font color=blue> XLoadTextFileData(TextFileName <font color=blue>As String</font color=blue>, ByRef BufferRef <font color=blue>As String</font color=blue>)
<font color=blue>Dim</font color=blue> hFile <font color=blue>As Long</font color=blue>
<font color=black>' Get a free file handle and load the data into the appropriate variable</font color=black>
<font color=black>'-----------------------------------------------------------------------</font color=black>
hFile = FreeFile
<font color=blue>Open</font color=blue> TextFileName <font color=blue>For Binary</font color=blue> <font color=blue>As</font color=blue> hFile
<font color=black>' Make sure the buffer is big enough to hold the data</font color=black>
BufferRef = Space$(LOF(hFile) )
<font color=blue>Get</font color=blue> #hFile, , BufferRef
<font color=blue>Close</font color=blue> hFile
<font color=blue>End Sub</font color=blue>
</font color=green></pre>
I have also attached the project. Copy the two text data files into the folder "C:\Temp" and away you go.
Also check out the debugging tutorial on the Tutors forum.
HTH
|
|

10-22-2001, 09:59 PM
|
|
Senior Contributor
* Guru *
|
|
Join Date: Mar 2000
Location: Christchurch, New Zealand
Posts: 470
|
|
Re: Anyone help?
|
Sorry, here's the demo as promised
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|