LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Finding a string into a file

Hi.

In my project I need to optimize the search of a string from a file, so I have to implement the

successive approximation method. How can I find the position of the beginning of any line from my

file? If I use fseek or getpos I obtain the position of the last character write in file. Is there

a function for the begining of a line?

Thanks.
0 Kudos
Message 1 of 6
(3,723 Views)
Hello tmaxial,

Can you further clarify what you are trying to do? You mentioned that you "can obtain the position of the last character write in file", are you trying to find the position of beginning of the last line?

Thanks.
Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 2 of 6
(3,697 Views)
Hi.

I am trying to find a method to obtain the position of the first character in line into a text file (or the number of line).
I need to implement a method of a fast search of a number from a text file.
I will try to explain this method now:

A have a text file who has each line starting with a number. The numbers from the begining of each line are in a right order.
First of all a thake the last line and a check the number. If it isn't the number I am looking for, I will divide the number of line, and I check the half that have my number( through comparisson). So I divide again the half that remain, that surely has my number, and so on until I find the number that I am searching for.

I don't know if I made myself understood. I need now a method to find the number of line in this text file.

Thanks.
0 Kudos
Message 3 of 6
(3,691 Views)
Hi tmaxial.

You can open the file in ASCII mode, and read it one line at a time, using the ReadLine() function. As you read each line, parse it using Scan(), and check the first value. (See the Formatting & I/O library)

If you need to do multiple searches on the data, I suggest you also use the List functions (from Programmer's Toolbox). Generate a list of structs, with each member of the list holding the data obtained from a single line (assuming each line has the same format). Use the ListInsertInOrder() and ListBinSearch() functions in your logic to store and retrieve the data.

Good luck,
Colin.
0 Kudos
Message 4 of 6
(3,685 Views)
Hello tmaxial,

If you don't mind parsing through the entire file, you might as well keep track of the number of lines you read instead of scanning through each line for the line number at the beginning. So you would call ReadLine and keep a running count until you reach the end of the file.

On the other hand, if you know that the length of the lines in the file are constant, then you can get the size of the file and divide by the length of each line to get how many lines are in the file.

Most likely your lines aren't going to be of constant length, so parsing through the entire file is probably going to be your best solution.

Hope that helps.
Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 5 of 6
(3,675 Views)
Hi tmaxial,

Supposing your file if made of variable-lenght rows (otherwise Wendy has alreaady pointed to the correct solution to your problem), the binary search you are speaking of can be impemented this way:

- Get the file lenght
- Move the file pointer to half the file lenght
- ReadLine () and discard the string
- The file pointer is now at the beginning of a new line: ReadLine and Scan the line row, next decide if and where to do the next jump

Now, the problem is that while it's easy to go forward (ReadLine helps a lot if it's an ASCII file) there is not a function to read the PRECEDING line in the file: this must be done by reading a character at a time...


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 6 of 6
(3,671 Views)