CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2000
    Posts
    18

    How would i read a specific line from a text file as a string?

    I would like to read configuration from a text file like config.txt
    each line would look like: variable value (for example: version "0.9")

    What code would i need to write if i only wanted to extract one variable and it's value from the config.txt?

    How would i find the variable and read it's value ????


    ---------------------
    ~:{Decayed}:~
    [email protected]
    http://www.q3seek.com

  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: How would i read a specific line from a text file as a string?

    You can use a .ini file. You can use the GetPrivateProfileString or GetPrivateProfileInt API to retrieve a value of such file. To write it, you need to use the API WritePrivateProfileString/WritePrivateProfileInt. It is written to file like this:

    [default]
    username=cakkie

    [custom]
    textcolor=red

    eg:

    ' write something to the ini file
    retval = WritePrivateProfileString("default","username","cakkie","c:\myIni.ini")
    ' read something from the ini file
    retval = GetPrivateProfileString("default","username","none",uName, 255, "c:\myIni.ini")




    For more info, and a complete example, check out http://www.vbapi.com/ref/funcc.html#inifiles

    Tom Cannaerts
    [email protected]

    The best way to escape a problem, is to solve it.
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: How would i read a specific line from a text file as a string?

    If you want to include the Double quotations also then you can use INput # statement, to read one line at a time into a string buffer, not reading the ""s

    Otherwise, for typical key=value pairs, WritePrivateProfilestring/int is best, as the other reply suggests

    RK

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

Featured