06-26-2012 06:56 AM - edited 06-26-2012 06:57 AM
Hi guys,
I'm new in LabWindows. I have a simple project, which has to parse a .txt file. Only some data inside the txt-File is interesting (the middle part, segment # 1-3 -> see example). The upper and lower part differs in size and is not important. Are there predefined LabWindows function, which I may use, or do I have to code it in C?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Program Run: xxxxxxxxxxxxxxxxxxxxxx Model: xxxxxxxxxxx
Description: xxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxx
Seg- Segm. Run | Mix | RMV GasVol Max Max CNS CPTD END END
ment Time Time | Used| liter Segm. A B % % N2 N2+O2
# (min) (min) | # | /min liters (mswg) Segm. Segm. Segm. (mswg) (mswg)
----- ----- ----- | ------ | ---- ------ ------ ----- ------ ----- ------ ------
1 0.1 0.1 | 1 |20.00 4.0 20. 0.62 0.0% 0.0 20. 20.
2 19.9 20.0 | 1 |20.00 1178.4 20. 0.62 2.9% 6.1 20. 20.
3 2.2 22.2 | 1 |20.00 87.7 20. 0.62 0.1% 0.1 20. 20.
------ -----
3.0% 6.2
Total Total
with 1.5
xxxxxxxxxxxxxxxxxxxxx: liters safety factor
Mix # 1 1270.1 1905.1
06-26-2012 07:22 AM - edited 06-26-2012 07:22 AM
Hi,
there is the class of CVI's scanning functions such as ScanFile (, );, which you can find in the Formatting and I/O library; similarly (different, but neither better nor worse) you can use the standard, ANSI C functions such as fscanf (, );
Note that the format strings are different for the two libraries.
06-26-2012 07:24 AM
Since it is a text file, you could read it in lines, skip the lines you don't need and parse the lines of interest.
The easiest way is probably to use the following Formatting & I/O Library functions:
OpenFile to open up the file in ascii mode
ReadLine to read the file line by line
CloseFile to close the file after use
You will need to iterate on ReadLine in a loop until EOF (return code = -2) and on every line test if you want to parse it or not.
Based on the file contents you showed us it is probably enough an initial loop that simply scans lines until the dashed line is found, next start a second loop that reads and parses the following lines and exits on the first line beginning with 5+ spaces.
To parse the lines you can either use standard C functions like sscanf or CVI native functions like Scan. The online help for the latter offers a good description and several examples for the most common types of scans, while for the first one you can look for help in a good C manual.
Don't forget to close the file after you have finished reading it and to add good error checking to prevent unexpected behaviour in case of I/O errors.
06-26-2012 10:20 AM
Ok, perfect explanation... Thanks alot!
06-26-2012 11:35 AM
What kind of error handling do you suggest?
06-26-2012 01:05 PM - edited 06-26-2012 01:06 PM
Ok, now I tried to build an Callback-Button, which should open a file and print it in a text box. Unfortunatly it does not work. What did I wrong...
Sorry for my stupid questions...
static int filehandle;
int CVICALLBACK AddCallback (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
char str[150];
if (event == EVENT_COMMIT)
{
filehandle = OpenFile ("test.dat", VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
while(!filehandle) {
ReadLine(filehandle, str, 150);
SetCtrlVal (panel, PANEL_ACTIONBOX, str);
}
CloseFile (filehandle);
}
return 0;
}
06-26-2012 01:41 PM
I would assume that your while - condition never changes and thus you end up in an infinite loop...
You should test for the return value of ReadLine:
status = ReadLine ();
while (status > -1 )
{
...
}
06-27-2012 12:24 AM - edited 06-27-2012 12:27 AM
wolfgang is right in pointing you to the while condition error: you actually never enter the while since OpenFile either returns a true handle (>0) or -1 in case of errors!
Going a little bit further, you can test both EOF end error indicator this way:
int error = 0;
while (1) {
status = ReadLine (...);
if (status == -2) break; // end of file reached
if (status == -1) { // Error found
error = GetFmtIOErrr ( );
break;
}
}
CloseFile (...);
Next you can warn the operator of any error found using GetFmtIOErrorString () to get a short description of the error.
06-27-2012 03:50 AM - edited 06-27-2012 03:51 AM
It works very nice...
The only problem I currently have is, that it only prints out every second line????????
int CVICALLBACK AddCallback (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
int error = 0;
int status = 0;
int filehandle = 0;
char str[351];
if (event == EVENT_COMMIT) {
filehandle = OpenFile("test.dat", VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
while(1) {
status = ReadLine(filehandle, str, 350);
// end of file reached
if(status == -2)
break;
// Error found
else if(status == -1) {
//error = GetFmtIOErrr ( );
break;
}
else {
ReadLine(filehandle, str, 350);
strcat(str, "\n");
SetCtrlVal (panel, PANEL_ACTIONBOX, str);
}
}
CloseFile (filehandle);
}
return 0;
}
06-27-2012 04:02 AM
... delete your second call of ReadLine and it should work ![]()
@HansiWWW wrote:
It works very nice...
The only problem I currently have is, that it only prints out every second line????????
int CVICALLBACK AddCallback (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
int error = 0;
int status = 0;
int filehandle = 0;
char str[351];
if (event == EVENT_COMMIT) {
filehandle = OpenFile("test.dat", VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
while(1) {
status = ReadLine(filehandle, str, 350);
// end of file reached
if(status == -2)
break;
// Error found
else if(status == -1) {
//error = GetFmtIOErrr ( );
break;
}
else {ReadLine(filehandle, str, 350);
strcat(str, "\n");
SetCtrlVal (panel, PANEL_ACTIONBOX, str);
}
}
CloseFile (filehandle);
}
return 0;
}