LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Open and read a .dat file

Hello, I have a problem. I will open a .dat file. This is the same as a .txt file. I have some informations in this file. For example *names...
*money...
The different things are pointed with *
I will read the informations from every * in the file.
Then I need this informations for example *rectangle(20,20,30,30) for drawing a rectagle on my CANVAS Panel.
Can s.b explain me, how I can do this?
This is my beginning:

file1 = OpenFile (file, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII); //File öffnen
Auslese = ReadFile (file1, Bufferdata, 50); //Auslesen
Close = CloseFile (file1);

Thank you
0 Kudos
Message 1 of 6
(4,689 Views)
I can imagine a couple of ways to do it.

First of all, ReadFile reads all the file contents in a single variable. Since your file is a text file, it can be read by lines with ReadLine: supposing your items are on different lines you will need to do nothing apart identify which item has been read. Your file coud read like this one:

*money(xxxxx)
*names(xxxxx)
*rectangle(20,20,30,30)

Second way: you can read the whole file in a string and break into individual components using strtok: look in the online help for this function to know how to use it. A little cumbersome but it works, with the only limitation that you cannot use the asterisk for other than separating items.

A simpler way to accomplish this task could be to use the .ini file inst
rument (inifile.fp) found in toolslib\toolbox directory. This instrument permits you to write and read files with this structure:

[General]
Names = "xxxx"
Money = "xxxxx"
Rectangle = "20,20,30,30"

This is useful because you directly red the item you want instead of reading a generic string and guessing its meaning via a sequence of "if" clauses. Obviously this way is practicable only if it's you that decide your file contents.

Hope this helps
Roberto


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 2 of 6
(4,689 Views)
Thank you Robert,
I think I will use the inifile.fp.
My text file has exactly this structure:
73501133
200 116
400 196
*Buttons
10 5
220 176 42
330 156 71
370 156 29
350 136 57
350 176 43
In a line there are sometimes 2 or 3 numbers.
I must not read all numbers together.
I must read each number alone.
If I wand to start reading on this point *,
can I start there with a special order?
Thank`s for the answer
0 Kudos
Message 3 of 6
(4,689 Views)
C.Tommy,

if you take a look into the inifile.fp, you can see that such an .ini-file as you mentioned above is not supported. Robert gave the exact layout for these .ini-files which are used with this toolbox-library.
So either you can change the file to fit the layout of the .ini-files or you have to handle the reading for yourself.
if you choose the second way, you have to read the numbers all in one into a string and parse through it extracting each number you need.

-Norbert B.
NI Germany
Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 4 of 6
(4,689 Views)
C.Tommy,

if you take a look into the inifile.fp, you can see that such an .ini-file as you mentioned above is not supported. Roberto gave the exact layout for these .ini-files which are used with this toolbox-library.
So either you can change the file to fit the layout of the .ini-files or you have to handle the reading for yourself.
if you choose the second way, you have to read the numbers all in one into a string and parse through it extracting each number you need.

-Norbert B.
NI Germany
Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 5 of 6
(4,689 Views)
As Norberto told you, the inifile instrument requires a fixed-structure file, so you cannot use it to read your file.

You could read the entire file in a buffer with ReadFile as you are already doing and search for the initial keyword with

idx = FindPattern (string, 0, -1, "*buttons", 0, 0);

which returns in the position of the first character in pattern (in your case, the *).

Roberto


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 6 of 6
(4,689 Views)