Getting a line from a file - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Getting a line from a file

tj29tj29 Posts: 45Member
I have a file scan procedure that returns me a list of integer representing the position of all the times it found the word it was searching for. what i want to do is that base on that integer to extract the entire line where the word was found and copy it to another file.
for example
the house is blue.
the car is yellow.
the house is green.

if i search for house it would return 2 integers giving me the position of both "house" what i would like is to extract does two lines and copy them to another file.

any help will be appreciated. thank you

Comments

  • zibadianzibadian Posts: 6,349Member
    : I have a file scan procedure that returns me a list of integer representing the position of all the times it found the word it was searching for. what i want to do is that base on that integer to extract the entire line where the word was found and copy it to another file.
    : for example
    : the house is blue.
    : the car is yellow.
    : the house is green.
    :
    : if i search for house it would return 2 integers giving me the position of both "house" what i would like is to extract does two lines and copy them to another file.
    :
    : any help will be appreciated. thank you
    :
    You can best load the entire file into a stringlist and use it to get the lines. A second stringlist can represent the destination file, and can later be saved onto the disk.
  • tj29tj29 Posts: 45Member
    : : I have a file scan procedure that returns me a list of integer representing the position of all the times it found the word it was searching for. what i want to do is that base on that integer to extract the entire line where the word was found and copy it to another file.
    : : for example
    : : the house is blue.
    : : the car is yellow.
    : : the house is green.
    : :
    : : if i search for house it would return 2 integers giving me the position of both "house" what i would like is to extract does two lines and copy them to another file.
    : :
    : : any help will be appreciated. thank you
    : :
    : You can best load the entire file into a stringlist and use it to get the lines. A second stringlist can represent the destination file, and can later be saved onto the disk.
    :
    thanks.
    I try that but the problem is that using stringlist is to slow thats why i was using the scanfile procedure that is really fast but it only returns the positon of every time the it finds a word that you are looking for. the thing is that i need to look in about 100 files (each containing about 1000+ lines) for each line that starts with the word ERROR. for example

    ERROR - this is a test

    so every line that starts with ERROR i need to put in another file.
    i need something fast i was told that scanning was fast and it is the problem is getting the line out of the file.
    if you can help i will really appreciated
  • zibadianzibadian Posts: 6,349Member
    : : : I have a file scan procedure that returns me a list of integer representing the position of all the times it found the word it was searching for. what i want to do is that base on that integer to extract the entire line where the word was found and copy it to another file.
    : : : for example
    : : : the house is blue.
    : : : the car is yellow.
    : : : the house is green.
    : : :
    : : : if i search for house it would return 2 integers giving me the position of both "house" what i would like is to extract does two lines and copy them to another file.
    : : :
    : : : any help will be appreciated. thank you
    : : :
    : : You can best load the entire file into a stringlist and use it to get the lines. A second stringlist can represent the destination file, and can later be saved onto the disk.
    : :
    : thanks.
    : I try that but the problem is that using stringlist is to slow thats why i was using the scanfile procedure that is really fast but it only returns the positon of every time the it finds a word that you are looking for. the thing is that i need to look in about 100 files (each containing about 1000+ lines) for each line that starts with the word ERROR. for example
    :
    : ERROR - this is a test
    :
    : so every line that starts with ERROR i need to put in another file.
    : i need something fast i was told that scanning was fast and it is the problem is getting the line out of the file.
    : if you can help i will really appreciated
    :
    The following get procedure is quite slow, but it's easy to program. You open the file as a file of char. Then seek() to the first position of the word error and start to copy it char for char to the other file, until you encounter the #13#10 line ending code. Then you seek() the next position, and start copying. Repeat this seek-copy procedure until all the ERROR-lines are copied.
    Another method, which involves 2 scans of each file is making a map of all the ERROR positions and of all the #13#10 positions. Then you can calculate the length of each ERROR line, and use untyped files and BlockRead()/BlockWrite() to perform the copying. It is more error-prone, but could be faster than the previous method.
  • tj29tj29 Posts: 45Member
    : : : : I have a file scan procedure that returns me a list of integer representing the position of all the times it found the word it was searching for. what i want to do is that base on that integer to extract the entire line where the word was found and copy it to another file.
    : : : : for example
    : : : : the house is blue.
    : : : : the car is yellow.
    : : : : the house is green.
    : : : :
    : : : : if i search for house it would return 2 integers giving me the position of both "house" what i would like is to extract does two lines and copy them to another file.
    : : : :
    : : : : any help will be appreciated. thank you
    : : : :
    : : : You can best load the entire file into a stringlist and use it to get the lines. A second stringlist can represent the destination file, and can later be saved onto the disk.
    : : :
    : : thanks.
    : : I try that but the problem is that using stringlist is to slow thats why i was using the scanfile procedure that is really fast but it only returns the positon of every time the it finds a word that you are looking for. the thing is that i need to look in about 100 files (each containing about 1000+ lines) for each line that starts with the word ERROR. for example
    : :
    : : ERROR - this is a test
    : :
    : : so every line that starts with ERROR i need to put in another file.
    : : i need something fast i was told that scanning was fast and it is the problem is getting the line out of the file.
    : : if you can help i will really appreciated
    : :
    : The following get procedure is quite slow, but it's easy to program. You open the file as a file of char. Then seek() to the first position of the word error and start to copy it char for char to the other file, until you encounter the #13#10 line ending code. Then you seek() the next position, and start copying. Repeat this seek-copy procedure until all the ERROR-lines are copied.
    : Another method, which involves 2 scans of each file is making a map of all the ERROR positions and of all the #13#10 positions. Then you can calculate the length of each ERROR line, and use untyped files and BlockRead()/BlockWrite() to perform the copying. It is more error-prone, but could be faster than the previous method.
    :


    thanks that worked just fine thanks again
Sign In or Register to comment.