Open a text file, search for place holder, then write data to file 
Author Message
 Open a text file, search for place holder, then write data to file

Hello All:

Can anyone give me a shove in the right direction on how to open a text
file, search the text file for a Header of sorts then once the header
position is found within the file the print data to that file after the
header in sequential order?

Text file:

{Header}
data1
data2
data3

I know how to open a file for output or input. But am unsure how to use a
loop to check each line of the file until it finds the match to the header
name. Then once the header position is found within the file, I do not know
how to print the data to the file after the Header is found.

I have tried opening the file for output doing a search, but if the search
matches, How can I do a Input function while a output function already has
the file open?

Thanks for the help!!!



Sun, 10 Jul 2005 00:47:01 GMT  
 Open a text file, search for place holder, then write data to file
On Tue, 21 Jan 2003 10:47:01 -0600, "Brian Ranstead"

Quote:

>Hello All:

>Can anyone give me a shove in the right direction on how to open a text
>file, search the text file for a Header of sorts then once the header
>position is found within the file the print data to that file after the
>header in sequential order?

>Text file:

>{Header}
>data1
>data2
>data3

>I know how to open a file for output or input. But am unsure how to use a
>loop to check each line of the file until it finds the match to the header
>name. Then once the header position is found within the file, I do not know
>how to print the data to the file after the Header is found.

>I have tried opening the file for output doing a search, but if the search
>matches, How can I do a Input function while a output function already has
>the file open?

>Thanks for the help!!!

If each "record" is the same length, what you have is a random access
file which you can open using the Random keyword in your Open
statement.  If the data is of variable length, what you will need to
do is create a new file and write the data that comes before the
position in which you want to insert the new data, then write the new
data, followed by any data that comes after the data you wanted to
replace in the original file.  Once you have the new file written as
desired, you can kill the original file and rename the new file with
the original file name.

In any case, you can find more info by looking at the "Open" topic in
your VB help file.

HTH,
Bryan
____________________________________________________________
New Vision Software            "When the going gets weird,"
Bryan Stafford                   "the weird turn pro."

Microsoft MVP-Visual Basic     Fear and Loathing in LasVegas



Sun, 10 Jul 2005 02:00:58 GMT  
 Open a text file, search for place holder, then write data to file

Thanks for the nudge Bryan, I think I need a little more help though..

Here is an Example of what I want to do.

in a file called !Blah.txt I will have stored text data.

[Architecture]
Office=Chicago
Path=C:\projects
Employee=Brian
[Pluming]
Office=SanFran
Path=C:\projects
Employee=Bill
[Electrical]
Office=Denver
Path=C:\projects
Employee=George

I have created a program that will write, put or print data to a file. BUT I
am wanting to go a step further in the aspect of searching through the text
file for a particular header, then under that header look for a particular
subject then retrieve that value that the subject is equal to. As in I want
the routine to open the text file search for the header of Pluming and find
the value for the employee which in the above example would be Bill.

I have written some rough code on this but I do not know where to go from
here.

Open filespec For Input Access Read As #File
    Do While Not EOF(File)
        Line Input #1, TextLine1
        If TextLine1 = "[Pluming]" Then
            WRITE CODE HERE TO APPEND TO THE NEXT LINE AFTER HEADER
"Pluming"
    Loop
Close #File

I know in the above example the TextLine1 is a integer and I am comparing it
to a string. This is where I am confused on how to compare the position of
the word [Pluming] in relation to the end of a file. I think I need to find
the position of the text in relation from the top of the page down. Like in
above text file. the word [Pluming] is in a counter = to 5 places from the
top of the text file. BUT how do you read what is at the intcounter position
5 of a text file?

I am sorry of this is confusing, BUT I have seen this application used
before by programs and like the organization it gives.

Thank you.


Quote:
> On Tue, 21 Jan 2003 10:47:01 -0600, "Brian Ranstead"

> >Hello All:

> >Can anyone give me a shove in the right direction on how to open a text
> >file, search the text file for a Header of sorts then once the header
> >position is found within the file the print data to that file after the
> >header in sequential order?

> >Text file:

> >{Header}
> >data1
> >data2
> >data3

> >I know how to open a file for output or input. But am unsure how to use a
> >loop to check each line of the file until it finds the match to the
header
> >name. Then once the header position is found within the file, I do not
know
> >how to print the data to the file after the Header is found.

> >I have tried opening the file for output doing a search, but if the
search
> >matches, How can I do a Input function while a output function already
has
> >the file open?

> >Thanks for the help!!!

> If each "record" is the same length, what you have is a random access
> file which you can open using the Random keyword in your Open
> statement.  If the data is of variable length, what you will need to
> do is create a new file and write the data that comes before the
> position in which you want to insert the new data, then write the new
> data, followed by any data that comes after the data you wanted to
> replace in the original file.  Once you have the new file written as
> desired, you can kill the original file and rename the new file with
> the original file name.

> In any case, you can find more info by looking at the "Open" topic in
> your VB help file.

> HTH,
> Bryan
> ____________________________________________________________
> New Vision Software            "When the going gets weird,"
> Bryan Stafford          "the weird turn pro."

> Microsoft MVP-Visual Basic     Fear and Loathing in LasVegas



Sun, 10 Jul 2005 03:36:31 GMT  
 Open a text file, search for place holder, then write data to file
What you have there is an INI file!  (why didn't you say so in the
first place?!?!)  ;-)

You can make this a WHOLE lot easier on yourself by just using the INI
file API functions to access/modify your data.  Let's see.......   I
think I remember that Klaus Probst has some good INI management class
objects on his site (http://www.vbbox.com/).  You should also be able
to do a Google search for other INI file example code.

HTH,
Bryan
____________________________________________________________
New Vision Software            "When the going gets weird,"
Bryan Stafford                   "the weird turn pro."

Microsoft MVP-Visual Basic     Fear and Loathing in LasVegas

On Tue, 21 Jan 2003 13:36:31 -0600, "Brian Ranstead"

Quote:

>Thanks for the nudge Bryan, I think I need a little more help though..

>Here is an Example of what I want to do.

>in a file called !Blah.txt I will have stored text data.

>[Architecture]
>Office=Chicago
>Path=C:\projects
>Employee=Brian
>[Pluming]
>Office=SanFran
>Path=C:\projects
>Employee=Bill
>[Electrical]
>Office=Denver
>Path=C:\projects
>Employee=George

>I have created a program that will write, put or print data to a file. BUT I
>am wanting to go a step further in the aspect of searching through the text
>file for a particular header, then under that header look for a particular
>subject then retrieve that value that the subject is equal to. As in I want
>the routine to open the text file search for the header of Pluming and find
>the value for the employee which in the above example would be Bill.

>I have written some rough code on this but I do not know where to go from
>here.

>Open filespec For Input Access Read As #File
>    Do While Not EOF(File)
>        Line Input #1, TextLine1
>        If TextLine1 = "[Pluming]" Then
>            WRITE CODE HERE TO APPEND TO THE NEXT LINE AFTER HEADER
>"Pluming"
>    Loop
>Close #File

>I know in the above example the TextLine1 is a integer and I am comparing it
>to a string. This is where I am confused on how to compare the position of
>the word [Pluming] in relation to the end of a file. I think I need to find
>the position of the text in relation from the top of the page down. Like in
>above text file. the word [Pluming] is in a counter = to 5 places from the
>top of the text file. BUT how do you read what is at the intcounter position
>5 of a text file?

>I am sorry of this is confusing, BUT I have seen this application used
>before by programs and like the organization it gives.

>Thank you.



>> On Tue, 21 Jan 2003 10:47:01 -0600, "Brian Ranstead"

>> >Hello All:

>> >Can anyone give me a shove in the right direction on how to open a text
>> >file, search the text file for a Header of sorts then once the header
>> >position is found within the file the print data to that file after the
>> >header in sequential order?

>> >Text file:

>> >{Header}
>> >data1
>> >data2
>> >data3

>> >I know how to open a file for output or input. But am unsure how to use a
>> >loop to check each line of the file until it finds the match to the
>header
>> >name. Then once the header position is found within the file, I do not
>know
>> >how to print the data to the file after the Header is found.

>> >I have tried opening the file for output doing a search, but if the
>search
>> >matches, How can I do a Input function while a output function already
>has
>> >the file open?

>> >Thanks for the help!!!

>> If each "record" is the same length, what you have is a random access
>> file which you can open using the Random keyword in your Open
>> statement.  If the data is of variable length, what you will need to
>> do is create a new file and write the data that comes before the
>> position in which you want to insert the new data, then write the new
>> data, followed by any data that comes after the data you wanted to
>> replace in the original file.  Once you have the new file written as
>> desired, you can kill the original file and rename the new file with
>> the original file name.

>> In any case, you can find more info by looking at the "Open" topic in
>> your VB help file.

>> HTH,
>> Bryan
>> ____________________________________________________________
>> New Vision Software            "When the going gets weird,"
>> Bryan Stafford          "the weird turn pro."

>> Microsoft MVP-Visual Basic     Fear and Loathing in LasVegas



Sun, 10 Jul 2005 03:55:57 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Writing data into a file placed on the server

2. Reading from a text file and writing toa text file from Vis Bas 6.0

3. Write contents of listbox to text file/populate listbox from text file

4. opening / saving files with MS rich text box control (viewing open files)

5. search file and write to file

6. Help !! Writing data string to a Text file

7. writing data to a text file

8. Sample Text file - Retrieve data elements and write to SQL Server DB

9. Working with Text Files - Searching for strings and then exporting to new file

10. Searching a text file for input to a database file

11. Help with object editing (how to use place holders)

12. Question about writing a string to a text file (file.txt)

 

 
Powered by phpBB® Forum Software