LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Data File Parsing in CVI

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

 


0 Kudos
Message 1 of 12
(5,577 Views)

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.

0 Kudos
Message 2 of 12
(5,573 Views)

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.



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 12
(5,573 Views)

Ok, perfect explanation... Thanks alot!

0 Kudos
Message 4 of 12
(5,556 Views)

What kind of error handling do you suggest?

0 Kudos
Message 5 of 12
(5,548 Views)

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;
}


0 Kudos
Message 6 of 12
(5,543 Views)

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 )

{

...

}

0 Kudos
Message 7 of 12
(5,538 Views)

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.



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 8 of 12
(5,531 Views)

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;
}


0 Kudos
Message 9 of 12
(5,523 Views)

... delete your second call of ReadLine and it should work Smiley Wink

 


@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;
}


 

0 Kudos
Message 10 of 12
(5,520 Views)