Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using input to read files

Posted on 1999-07-28
8
Medium Priority
?
145 Views
Last Modified: 2010-04-30
I am using input to read an ini file.  
Open "C:\test.ini" for input as #1

My ini file has a line that has comma's in it.  It seems that when using the input it is using the comma's to as delimiters.  Is there a way to turn this off.  I need it to read the whole line and ignore comma's.  Thanks
0
Comment
Question by:strongd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 7

Expert Comment

by:kamall
ID: 1528085
Use
    Line Input #1, MyString

This will read an entire line from the file. Then you can check for the comma's in the string using the InStr function, to find if any comma's exist and at what character positions.
Hope that helps.

Regards.
0
 

Author Comment

by:strongd
ID: 1528086
This works for 1 variable but I am reading in over 20 variables into VB.   How do I sparate them.  I tried using commas but I keep gettting an error.  


Line Input #1, MyString, Mystring2, mystring3, mystring4, mystring5, mystring6,.....etc...
This will not work.   What do I use to sparate them??

0
 
LVL 1

Expert Comment

by:Kurvy
ID: 1528087
Minor Point first

when declaring the file to open do not hard code the file number use a proxy
ie Dim intFreeFile as integer

intFreeFile=FreeFile
open myDoc for input as intFreeFile

Use Kamall's code to input the line into a string variable

then as he suggests break up the line using instr() and read in into an array

As a Starter here's some code to help

dim MyString() as string
dim intPos as integer
dim intOldPos as integer
dim intCounter as integer
dim intNewPos as integer
lets assume we've got to the line with commas we can check for this by looking at each line inputted  with:

If instr(strInputLine,",")>0 then
intOldPos = 1
intPos = 1
intNewPos = 1
intPos = InStr(strInputLine, ",")
intCounter = 1
If intPos > 1 Then
    ReDim MyString(intCounter)
    MyString(intCounter - 1) = Mid(strInputLine, intNewPos, intPos - 1)
    intNewPos = intPos + 1
    Do While intPos > 0
        intPos = InStr(intNewPos, strInputLine, ",")
        If intPos > 0 Then
            intCounter = intCounter + 1
            ReDim Preserve MyString(intCounter)
            MyString(intCounter - 1) = Mid(strInputLine, intNewPos, intPos - intNewPos)
            intNewPos = intPos + 1
        Else
            intCounter = intCounter + 1
            ReDim Preserve MyString(intCounter)
            MyString(intCounter - 1) = Mid(strInputLine, intNewPos)
        End If
    Loop
End If


Once you have got your array then you can assign the values as you wish:

ie  MyVar1=Mystring(7)
    MyVar2=MyString(4) etc
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:strongd
ID: 1528088
Thats sucks!   I have to do all this just so it will put the commas with the string...  I hate Microsoft.   Line input can only read one variible at a time is really really lame.  
I'll try it.   But I am thinking about read the ini file in as a binary file.   Thanks for your help.
0
 

Author Comment

by:strongd
ID: 1528089
I got it to work.

Open "C:\test.ini" for input as #1
Input #1, mystring, mystring2, mystring3, mystring4
Line Input #1, Mystring5        ' line with comma's
Input #1, mystring6, mystring7, mystring8.....etc.....


This worked great!  Kamall, answer this again and i'll give you the points.  Thanks

0
 
LVL 7

Accepted Solution

by:
kamall earned 80 total points
ID: 1528090
Oh...., you got it to work... thats great.
Thanks for your comment anyway.
Regards.
0
 

Author Comment

by:strongd
ID: 1528091
Thanks
0
 
LVL 7

Expert Comment

by:kamall
ID: 1528092
You are welcome.
Regards.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question