LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Labwindows/cvi中如何显示TXT文件

Solved!
Go to solution

在Labwindows/cvi中怎样把一个TXT文件显示出来让我们可以看到其中的内容,同时还可以在程序里往TXT文件添加数据,并可以实时看到添加的数据?

 

期待高人指点。

0 Kudos
Message 1 of 6
(8,107 Views)

你好,

 

获取文件的方法可以作如下参考:

FILE *fp;
char line [256];

fp = fopen ("TextFile.txt", "rt");
while (fgets (line, 256, fp))
    SetCtrlVal (panel, PANEL_TEXTBOX, line);
fclose (fp);

 

写入数据的参考:

OpenFile (source, open for reading in ascii mode)

OpenFile (destination, open for writing in ascii mode)

while (1) {

   result = ReadLine (from source)

   if (result == -2) break         // End-of-file reached

   Scan the buffer

   Create the new buffer with selected fields using sprintf or Fmt functions

    WriteLine (to destination)

}

CloseFile (source)

CloseFile (destination)

 

如果需要看到是什么数据的话,将待写入的数据同时显示在控件中就可以了。

希望能帮上忙

0 Kudos
Message 2 of 6
(8,076 Views)

感谢晓建的详细解答。

 

后来发现创建了TXT文件后,在CVI中可以用LaunchExecutable ("notepad.exe datalog");(datalog为我创建的文本文件名)显示;

但是实时显示程序中写入TXT文件的数据还是做不到。

 

LaunchExecutable ("notepad.exe datalog");只会显示在调该指令前已写入datalog的数据;调用后写入的显示不出来。

0 Kudos
Message 3 of 6
(8,011 Views)
Solution
Accepted by topic author jl.yu

这个问题很有意思,调用LaunchExecutable ("notepad.exe datalog");之后,相当于你在WINDOWS文件夹里面对着这个TXT文件双击打开。这样就有一个问题,在Windows中,是不允许你写入一个正在被打开的文件的,你试试看WORD等就知道。但是TXT特殊,WINDOWS只是打开了他的一个副本,也就是说将该文件的所有内容读到内存里面来就关掉该文件了。

 

你可以试试看,在打开一个TXT文件后,你可以将这个文件删除掉,而其格式的文件可不行。

 

那么,如果这样的话,你要刷新就再调用该语句一次,再打开一个副本就刷新了。

0 Kudos
Message 4 of 6
(7,992 Views)

Hi!

 

Windows notepad is not designed to do what you've asked for.

You should use standard I/O window to display what you're writing to

the txt file simultaneously for that purpose.

 

Alternatively, download the real-time text/log viewer like

"Tail for Win32" http://tailforwin32.sourceforge.net/ 

or "Tail XP" http://www.freedownloadmanager.org/downloads/Tail_XP_23837_p/

for your application.

Message Edited by dcl9000 on 01-06-2010 05:59 PM
Message 5 of 6
(7,989 Views)

That is right.  dcl9000's reply is closer to what you want!

Message Edited by JunXiang on 01-06-2010 06:06 PM
Message Edited by JunXiang on 01-06-2010 06:07 PM
0 Kudos
Message 6 of 6
(7,984 Views)