LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

read from file continuoulsy

Hi, I am trying to read from a file. But I want it to be continuously in such a way that any time something is written on that file, my cvi should be able to read it right awy. here is what I tried in my main. I try to use a for ever loop but I don't get what I want.

 

Any help?

 

 

 

 

.

.

.

.

 

#define FOREVER while(1)     

 

 

int main (int argc, char *argv[])
{
 if (InitCVIRTE (0, argv, 0) == 0)
  return -1; /* out of memory */
 if ((bringUp = LoadPanel (0, "Bring-Up Server.uir", BRING_UP)) < 0)
  return -1;
 DisplayPanel (bringUp);
 
 

 FOREVER
  {
 FileToOpen = OpenFile ("Writefile.txt", VAL_READ_WRITE, VAL_OPEN_AS_IS, VAL_ASCII);
   //Read Line at a time
 //WriteToFile;
 count=ReadLine (FileToOpen, linebuffer, BYTES_TO_READ); 
// Read = ReadFile (FileToOpen, ReadBuff, 260);
 SetTableCellVal (bringUp, BRING_UP_TABLE, MakePoint(2,1), linebuffer);
 
 RunUserInterface ();
  }
 DiscardPanel (bringUp);
 return 0;
}

0 Kudos
Message 1 of 15
(4,464 Views)

After the file write you need to move the filepointer to where you want the file read to commence. The function is SetFilePtr.

 

For the NI people reading this, a comprehensive comparison of the NI and ANSI file I/O and formatting functions would be of great use.

0 Kudos
Message 2 of 15
(4,456 Views)

Hi agyna,

what you are trying to do cannot be done the way you have coded it!

 

Inside your neverended loop you are opening the file and calling RunUserInterface on every iteration! At least these two operations must be moved before the loop, so that your file is opened one time only and RunUserInterface is called once to permit handling user events. Additionally you never close the file and there is no way to exit the loop.

 

Another possible problem is that you are opening the file in read/write mode: this can be problematic since you want to concurrently read while other users / programs are writing to the file; try opening it in read mode only, so that no errors for concurrent acccess can arise.

 

I cannot test at the moment but I personally would install a timer callback firing 3-4 times per second; in the callback I could test file position with respect to file size and read the content added after last read. I will try to prepare a little sample tomorrow in the morning.



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 3 of 15
(4,448 Views)

thanks ,

this works, but  it is keeping the all thing busy. Is there a way to just watch the file and read only when it is updated instead of using this "for ever loop" that is keeping things busy?

 

thanks.

0 Kudos
Message 4 of 15
(4,448 Views)

Sorry I didnt' see the last message.

0 Kudos
Message 5 of 15
(4,449 Views)

Hi,

 

I don't get this right, could you help a bit more with some specific codes?

0 Kudos
Message 6 of 15
(4,432 Views)

If you are trying to reduce CPU use you will need to SetSleepPolicy () to allow the program to release the CPU when "idle". There advantages and disadvantages to this.

0 Kudos
Message 7 of 15
(4,419 Views)

The idea is to read continuously from a file that is being continously updated by another program. It is giving me some trouble.

0 Kudos
Message 8 of 15
(4,417 Views)

Attached a small sample project that operates on a text file using a timer callback. Overhead of CPU is reduced as no operation is made if file size does not change.

 

To test this sample, run the project and choose a file: the program will read file content line by line and display it on a listbox; whith the program running, open the file with notepad and write some text, then close the file or simply save it and the program will update the listbox.

 

Program developed in CVI 2009. It can be downgraded if you are using previous release.



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 9 of 15
(4,406 Views)

Thanks a lot for taking all your time to help me with this. My solution is in here.

Thank you.

0 Kudos
Message 10 of 15
(4,399 Views)