LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

read an ascii file line by line

Solved!
Go to solution

I am new with CVI, and I need to read an ascii file with multiple lines in the following format:

 

at_test_2_to_13=0.861000
at_testabcd_9_to_21=0.712000
at_abdgx_5_to_8=0.892000

 

here is the code for writing in the ascii file:

 

sprintf(ctResult, "%s=%f\n", TestName1,result1);
WriteFile (fileHdl,ctResult ,StringLength(ctResult) );

 

 

At a later time, I need to read the text file, find the test name (for example "at_testabcd_9_to_21") and record the value after the "=" sign (in this case 0.712000). The number of lines in the text file is not fixed.

 

I am writing the data to the file with no problems, but I am stack in reading the data.

I would appreciate any help on how to write this code in CVI.

 

Thank you in advance

0 Kudos
Message 1 of 6
(4,997 Views)

Welcome to CVI.

 

First note that CVI is a regular C compiler, so you should be familiar with C in general. For your case,you might want to have a look at the Formatting and IO Library, in particular the function ScanFile. Or you could stick to the ANSI C functions such as fread() or gets ();

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

May be there is another possibility: If the format of your file is not strictly fixed, i.e. if you can work with lines of the type

 

Sampling Frequency = 1000

then you might want to consider the instrument driver for reading/writing ini files; this is quite convenient as it does the sorting and searching for you;

 

the commands are of the type

 

Ini_PutDouble (handle, "section", "tag", value);

Ini_GetDouble (handle, "section", "tag", &value);

 

you can find this library in toolslib\toolbox\inifile.fp

 

 

 

0 Kudos
Message 3 of 6
(4,988 Views)

As Wolfgang already proposed, you can use the ini-file library.

With this library, you can write the results in a structured manner and access an individual measurement by its name, without the need of reading every line above.

 

I also attached a sample project for what you asked.

 

Check the ini-file library help and samples for the other option.

S. Eren BALCI
IMESTEK
0 Kudos
Message 4 of 6
(4,985 Views)

if you do not want use ini style file. there is another way.

 

first you should read a line from file then use below function to find out the name and data.

 

example

 

char Txt = "VOLTS = 1.2";

 Scan(Txt,"%s[xt61]%f", tag,&val);

 

result tag = "VOLTS", val = 1.2.

 

then you are able to compare string to find the line you want.  by the way, sicne tag may contant white space. you better to call below function before compareing.

 

RemoveSurroundingWhiteSpace (tag);

 

hope can help !

 

B.R

Gerry

 

 

0 Kudos
Message 5 of 6
(4,949 Views)
Solution
Accepted by topic author Ellas1

Thank you very much for all the help!

The INI file was the way to go on this case.

With the help of a collegue, I used the following to create and read from the ini:

 

 #define DllExport __declspec(dllexport) __stdcall

 

int DllExport WriteToIniFile(char *Section,char *Key,char *StringVal,char *IniFileName)
{ int Err=0;
  WritePrivateProfileString(Section,Key,StringVal,IniFileName);
  return(Err);
}
int DllExport ReadFromIniFile(char *Section,char *Key,char *ReadStr,char *IniFileName)
{ int Err=0;
  DWORD CharCount;
  CharCount = GetPrivateProfileString(Section,Key,"",ReadStr,sizeof(ReadStr),IniFileName);
return(Err);
}

 

Also thank you very much for all the help with the C code. I've been using VB most of my career, and since I am labwindows user now I need to catch up on my C !!!

 

Thank you again

0 Kudos
Message 6 of 6
(4,933 Views)