From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

read a particular line from text file

Hi,

 

I want read a particular line in a text file till the end of the line(only end of the particulat line not end of the file). say for example I am reading 3rd line starting from a particulat start  point. So what i did is

 

I used read from text file fn and I gave '-1' to read entire file then

I use string subset and gave offset value to tell where I want to start and for the length I dont want to specify how many characters to read instaed I want to give fetch till end of the line.

 

Can any one tell if u know

 

Thanks in advance

 

--saamy

0 Kudos
Message 1 of 10
(4,543 Views)

In the other thread, I have already suggested using CopyString (, , "", , ) ...

 

As I understand you have read one line from a text file into a buffer and now want to extract only part of it, starting at a given offset, and the end given by the end of the line. In my opinion, you could use the above mentioned function...

 

The last parameter of CopyString, Maximum # Bytes, specifies the maximum number of bytes, excluding the ASCII NUL, to copy into the target. If you do not want to specify a maximum number of bytes to copy, use –1.

0 Kudos
Message 2 of 10
(4,535 Views)

There are a couple of ways to do this.

 

The easiest is to open the file and read it line-by-line with OpenFile + ReadLine in a loop; once the desired line has been reached, extract the relevant portion of it.

This method performs a disk access for each line read: for this reason it can be slow in case of large files with several lines.

 

Another way is to read the entire file in memory and scan it, as you are doing now. This method is a little bit more complicated but is could be very fast; in case of huge files you may have problems in reading the entire file in memory. If you want to go this way, I suggest you take a look at the code posted by Nick B in this thread, which can simplify the task of splitting file content in lines.



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 3 of 10
(4,530 Views)

Thanks Wolfgang, Yes In that thread itself I replied you. What I did so far is using "read from text file" function I am reading the entire file by using "-1" ang by using string subset I have given my starting position of the string to read (say for example my offset no is 108 so now my starting position is 3rd row and starting some where at the middle) Now my doubt is how to read till end of that particular line(as per our example - till 3rd row).

0 Kudos
Message 4 of 10
(4,528 Views)

Thanks a lot Roberto. Yes, 2nd method is apt for my application. I go through the link and ask u if I face any problem. Once again thanks 🙂

0 Kudos
Message 5 of 10
(4,524 Views)

Hi Wolfgang, saw that thread which you shared previously. I could not undertand clearly. I clearly explained what i did so far. If you dont mind can u plz  tell me how to proceed further steps to solve this matter

0 Kudos
Message 6 of 10
(4,516 Views)

Hi saamy,

 

I was assuming that you open the file and read it line-by-line in a loop, i.e. Roberto's method 1 (that's what I usuallly do but my files aren't that long). As this is not the case, you may politely ignore my contribution and follow the link provided by Roberto instead Smiley Wink  

0 Kudos
Message 7 of 10
(4,487 Views)

Hi Roberto & Wolfgang,

 

Thanks a lot for your support. I solved that problem, I can read line by line and fetch particular portion of data.

 

--

saamy

0 Kudos
Message 8 of 10
(4,474 Views)

RobertoBozzolo wrote:

The easiest is to open the file and read it line-by-line with OpenFile + ReadLine in a loop; once the desired line has been reached, extract the relevant portion of it.


I seem to missing a detail here.  Where in OpenFile or ReadLine is there an option to specify the line?  I don't see that as an option.

0 Kudos
Message 9 of 10
(4,244 Views)

ElectroLund ha scritto:

I seem to missing a detail here.  Where in OpenFile or ReadLine is there an option to specify the line?  I don't see that as an option.


That's because there isn't! Smiley Wink

The underlining idea was to use ReadLine in a loop until desired line has been reached. If you know the line number to reach you can simply use a loop that reads and discards n-1 lines, next you read and manage the line you want. If you don't know the line number, in the loop you read a line and parse its content looking for the informations you are searching.

 

Provided the file can fit in memory, you may speed up the process by reading the whole file at once and parsing its content in memory in a similar way: look at nickb posts in this thread which offer a very good solution to this problem.



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?
0 Kudos
Message 10 of 10
(4,242 Views)