-
Sep 16th, 2002, 01:03 PM
#1
Thread Starter
New Member
Newbie Help
I'm fairly new to VB and need help using file I/O. I am trying to open a file on my c:\ with a standard command button, read the file, insert data using variables, then write the file out to a new text file. So far I have tried using a textstream and have had success opening and writing the file, but I can't insert the variable data. Am I going about this the correct way or should I be using some other way to read in the data? Any help would be appreciated.
Thanks
GNIKR
-
Sep 16th, 2002, 01:23 PM
#2
Hyperactive Member
The best thing in this circumstance is to store the data in a temporary location (say an array of strings) insert the data into the element of your choosing and then write the array back out to another file.
If you simply want to add the data to the end of the file you read then you could append it.
-
Sep 16th, 2002, 01:26 PM
#3
Let me in ..
Re: Newbie Help
Originally posted by gnikr
I'm fairly new to VB and need help using file I/O. I am trying to open a file on my c:\ with a standard command button, read the file, insert data using variables, then write the file out to a new text file. So far I have tried using a textstream and have had success opening and writing the file, but I can't insert the variable data. Am I going about this the correct way or should I be using some other way to read in the data? Any help would be appreciated.
Thanks
GNIKR
Hey newbie, you got me pretty confused. Can you simplify your problem in simple words ???
-
Sep 16th, 2002, 01:36 PM
#4
Thread Starter
New Member
I have a text file that has VB variables already included into it. I would like to read in the file and set the VB variables to some predefined value then write out the file.
Ex: Textfile.txt
Hello my name is [First_Name].
In VB Code
First_Name = "Bob"
Click a button
Read c:\textfile.txt
Change [First_Name] to Bob
Create file c:\name.txt with "Hello my name is Bob." in it.
This is pretty much what 'm trying to do and I'm not sure how to explain it......
GNIKR
-
Sep 16th, 2002, 01:39 PM
#5
Hyperactive Member
Simple amigo...
Load the contents of your Read File into a Text Box (Or simpler yet, Load it straight into a RichTextBox) then replace the fields
[First_Name] for example (Using the replace function) and replace it with the variable name.
Then save the TextBox as your Write file (Or if using the RichTextBox save it as rtfText)
Of course, that's the basic concept, but there are many other ways too, but if you're ok with using the RichTextBox (Components) then use that as it has the Load and Save functions for writing out to RichText or Plain Text files.
-
Sep 16th, 2002, 02:09 PM
#6
How you put the data into the string depends on what the string looks like now. If you have nice placeholders (like the brackets in your example), it would be easy to replace through a variety of methods (e.g. Mid, Left, Right, InStr, or a more direct replace function). If you don't have readily identifiable placeholders, you will need to parse the string somehow, which will be a little slower and less tidy. However, either situation pretty much requires that you first move the text from the file into either a string, or an array of strings.
Don't worry too much about all the excess work of reading in lines that you don't intend to change, then writing all the lines back out. It is far easier, and acceptably fast to read it all, make the changes to the stings when they are held in variables, then write it all back out. I have done this in several programs, and it is painless. This also allows you to separate the read/write code from the code needed to make the changes, hence making your program more modular and maintainable.
-
Sep 16th, 2002, 02:12 PM
#7
PowerPoster
Re: Newbie Help
Originally posted by gnikr
I'm fairly new to VB and need help using file I/O. I am trying to open a file on my c:\ with a standard command button, read the file, insert data using variables, then write the file out to a new text file. So far I have tried using a textstream and have had success opening and writing the file, but I can't insert the variable data. Am I going about this the correct way or should I be using some other way to read in the data? Any help would be appreciated.
Thanks
GNIKR
woah hello? Using I/O is fast, grant it that, BUT if you want RAD then use the FileSystemObject especially if you are a Newbie!
-
Sep 16th, 2002, 02:26 PM
#8
Let me in ..
I guess, FilesystemObject is easy and good for newbie's.
-
Sep 16th, 2002, 02:43 PM
#9
Re: Re: Newbie Help
Originally posted by jesus4u
woah hello? Using I/O is fast, grant it that, BUT if you want RAD then use the FileSystemObject especially if you are a Newbie!
I've been using the Open FileName for Inourt AS #1 for so long it's the only way I know......I've never mastered the use of FSO....
Old dog... new tricks..... that sort of thing.
-
Sep 16th, 2002, 02:45 PM
#10
Thread Starter
New Member
Actually, I am using FileSystemObject. I don't know the names or how to refrer to methods/objects etc.... yet.....
I was able to get the RTF box to work in VB 6. However I am trying to do this in Access 2K and the find did not work. The error I get is "Method or Data Member not found". I assumed the version of VB Access comes with would be the same as VB 6 and I could copy the code over. Guess I was wrong.....
GNIKR
-
Sep 16th, 2002, 02:49 PM
#11
the fso is sweet to work with.. i use it all the time... the dll is only 144kbs so it is not like it is a huge file to include with an app... people are still so concerned with space these days.. i dunno why... a vb app with the runtimes doesn't even fit on a floppy anyway... and who uses them anymore (yeah yeah i know people still use floppies) but less and less since you can burn a cd for just as cheap or upload files to the net and download them on another PC since most are connected with high speed these days... i say load your app with those HUGE runtimes... why the hell not.. whats the point of having 120 gbs of space and 512 mbs of ram if your still going to try to write your app like your on a 286???
this is not directed at anyone imparticular... no one take offense... just my opinion..
-
Sep 16th, 2002, 02:54 PM
#12
PowerPoster
Originally posted by kleinma
the fso is sweet to work with.. i use it all the time... the dll is only 144kbs so it is not like it is a huge file to include with an app... people are still so concerned with space these days.. i dunno why... a vb app with the runtimes doesn't even fit on a floppy anyway... and who uses them anymore (yeah yeah i know people still use floppies) but less and less since you can burn a cd for just as cheap or upload files to the net and download them on another PC since most are connected with high speed these days... i say load your app with those HUGE runtimes... why the hell not.. whats the point of having 120 gbs of space and 512 mbs of ram if your still going to try to write your app like your on a 286???
this is not directed at anyone imparticular... no one take offense... just my opinion..
Hear hear!!! I just don't like the part about people downloading from the net... It's a pain the ass when people include the runtime files with their programs because I don't need them. I appreciate when people release two different installers. One with the VB runtime files, and one without. (Like I do on my website (http://haztek.d2g.com) <~shameless plug)
-
Sep 16th, 2002, 04:01 PM
#13
Thread Starter
New Member
Thanks for your help! You guys are great.
GNIKR
-
Sep 16th, 2002, 08:50 PM
#14
Fanatic Member
Hey Matt (or anyone): can you give me a FSO example. For I/O I'd do something like:
VB Code:
sFirstName = "Bob"
' Input text.
hFile = FreeFile
Open "c:\textfile.txt" For Input As #hFile
sTextFileText = Input(LOF(hFile), #hFile)
Close #hFile
' Change text.
sTextFileText = Replace(sTextFileText, "[First_Name]", sFirstName)
' Output text.
hFile = FreeFile
Open "c:\name.txt" For Output As #hFile
Print #hFile, sTextFileText
Close #hFile
How would you do the same thing with FSO?
-
Sep 16th, 2002, 09:14 PM
#15
New Member
Sorry to add to the list of posibilities but if your trying to create something similar to an .ini file there are some good API calls. Try http://216.26.168.92/vbapi/ref/funcc.html#inifiles They look a bit complicated but just cut and paste. You would need WritePrivateProfileString and GetPrivateProfileString.
Or store these values in the registry, more API calls.
Hope that helps
Stuart
-
Sep 16th, 2002, 09:20 PM
#16
Fanatic Member
I'd still like an FSO example for just plain old text (not ini) files. I've always just used I/O, and never really caught on to what FSO can/can't do. Any FSO tutorial links would be nice just so I can see what all the hub-bub is about.
-
Sep 18th, 2002, 02:15 PM
#17
Thread Starter
New Member
Is there any way to find a string in a text file using I/O that is similar to Replace?
sTextFileText = Replace(sTextFileText, "[First_Name]", sFirstName)
GNIKR
-
Sep 18th, 2002, 03:22 PM
#18
Frenzied Member
Check out the
function
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
|