How to write to a specific line


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: How to write to a specific line

Hybrid View

  1. #1
    Join Date
    Oct 2004
    Posts
    57

    How to write to a specific line

    In a program I am writing I need to be able to write to a specific line in a textfile. I don't know of anyway to do this except just rewriting the whole textfile which would be a pain to do. Does anyone know how to do this? To clear up any confusion that may have been caused I'll try to show what needs to be done.

    This is the textfile I have

    [UTFolder]
    I need to write a specific string into here
    [Extensions]
    ut2=C:\UT2004\Maps

    The extension list could go on for a while and would take a lot more coding to rewrite the file.

  2. #2
    Join Date
    Dec 2003
    Posts
    2,750
    Since your text file appears to have an INI file structure you can probably use the WritePrivateProfileString API function call:

    http://vbnet.mvps.org/code/file/pprofilebasic.htm
    Paul
    ~~~~
    Microsoft MVP (Visual Basic)

  3. #3
    Join Date
    Oct 2004
    Posts
    57

    Re:

    *sigh* I REALLY wish I had known about this earlier. This would have made this so much easier sheesh. Well thanks a lot for telling me about this. I suppose i better go update some of my code now hehe.

  4. #4
    Join Date
    Oct 2004
    Posts
    57
    So far I'm loving the simplicity of this, but I need help with reading an entire section. I need to get all of the contents, which are unknown, and then add them to a listbox. When I use GetPrivateProfileSection it returns the whole section, which is what I want, in one variable with a lot of characters that I don't understand. How am I to split the string to single every item from the ini out.

  5. #5
    Join Date
    Aug 2005
    Location
    Kent, England
    Posts
    29
    Using this example function
    Code:
    Public Function ReadProfileSection(ByVal SectionName As String, _
                                       ByVal FileName As String) As String
    
    Const MaxSectionLen As Integer = 6120 'Read Up To 5kB From Section
    
    Dim strSectionData As String * MaxSectionLen 'Define buffer to receive section data
    Dim lngApiFnVal As Long
    
    '***********************************************************
    '    Returns vbNullChar delimited string of KeyNames and
    '    values within specified section
    '    else returns empty string
    '***********************************************************
    
    lngApiFnVal = GetPrivateProfileSection(SectionName, strSectionData, _
                                           MaxSectionLen, FileName)
    
    If lngApiFnVal Then
       ReadProfileSection = Left(strSectionData, _
                                 InStr(strSectionData, vbNullChar + vbNullChar) - 1)
    End If
    
    End Function
    In your own code do something like
    Code:
    Dim strText As String, strItem() As String
    
    strText = ReadProfileSection("MYSECTION", "C:\MyFolder\MyApp.Ini")
    strItem = Split(strText, vbNullChar)
    Trevor

Similar Threads

  1. Command line parameter checking?
    By Volker Held in forum Java
    Replies: 4
    Last Post: 07-05-2002, 09:28 AM
  2. Open Database from Command line
    By John in forum VB Classic
    Replies: 4
    Last Post: 08-08-2000, 11:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center