Results 1 to 18 of 18

Thread: Newbie Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    15

    Question 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

  2. #2
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    258
    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.

  3. #3
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456

    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 ???


  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    15
    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

  5. #5
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    258
    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.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989
    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.

  7. #7
    PowerPoster
    Join Date
    Jan 2001
    Location
    Florida
    Posts
    3,216

    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!

  8. #8
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    I guess, FilesystemObject is easy and good for newbie's.


  9. #9
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    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.

    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    15
    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

  11. #11
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    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..

  12. #12
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    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)
    <removed by admin>

  13. #13

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    15
    Thanks for your help! You guys are great.

    GNIKR

  14. #14
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Hey Matt (or anyone): can you give me a FSO example. For I/O I'd do something like:
    VB Code:
    1. sFirstName = "Bob"
    2.  
    3. ' Input text.
    4. hFile = FreeFile
    5. Open "c:\textfile.txt" For Input As #hFile
    6.     sTextFileText = Input(LOF(hFile), #hFile)
    7. Close #hFile
    8.  
    9. ' Change text.
    10. sTextFileText = Replace(sTextFileText, "[First_Name]", sFirstName)
    11.  
    12. ' Output text.
    13. hFile = FreeFile
    14. Open "c:\name.txt" For Output As #hFile
    15.     Print #hFile, sTextFileText
    16. Close #hFile
    How would you do the same thing with FSO?

  15. #15
    New Member
    Join Date
    Sep 2002
    Location
    Sydney
    Posts
    12
    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

  16. #16
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    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.

  17. #17

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    15
    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

  18. #18
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Check out the
    VB Code:
    1. Instr
    function
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width