LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

FindPattern function and CSV files.

 Hi all!

 

I am working on a parser section of code, where we allow a user to open up a CSV file and we basically process data that is delimited by commas (as you'd expect). Anyways, I am trying to use the find patter function to stop at the next comma, then put the characters into a buffer that isn't a space, up until the next comma. I have a section of the code below to show you what I'm doing. i have a feeling this is the issue in all of my code. Does this syntax below do what I want above?

 

	intmon = FindPattern (linebuff, 0, 20, ",", 0, 0);

			for(int y = 0; y < intmon; y++)
			{

				TestSteps[index].test_num[y] = linebuff[y];
			}

			offset+=intmon;
			//Loop to place testname in test_name

			intmon = FindPattern (linebuff, offset, 20, ",", 0, 0);

			for(int y = offset; y < intmon; y++)
			{
				if (linebuff[y] != space[0])
				{
					TestSteps[index].test_name[y] = linebuff[y];
				}

			}

			offset= offset + intmon;

 

0 Kudos
Message 1 of 2
(4,659 Views)

I see a bit of issues in your code:

  • FindPattern () returns the index of the matching character, so for further searches you must increment that position by 1
  • you can use isspace () function to discriminate on spaces
  • you must have independent indexes for source and destination string when skipping spaces, otherwise you'll have characters not initalized within the target string
  • FindPatter () returns an offset from the origin of the string: you must not accumulate that value but rather replace 'offset' each time
  • You can use ANSI C strncpy () or Programmer's Toolbox StringCopyMax () function to operate the first copy

This code can be pasted and executed into the interactive execution window (Window >> Interactive Execution menu option):

 

#include <utility.h>
#include <formatio.h>
#include <ansi_c.h>
#include "toolbox.h"
static int x, y, intmon, offset; static char buf[128], test_num[16], test_name[16]; strcpy (buf, "123456,File name,Field 3"); // 'buf' is the string to parse
// Step #1: parsing first field (test number)
intmon = FindPattern (buf, 0, 20, ",", 0, 0); //for (y = 0; y < intmon; y++) { // test_num[y] = buf[y]; //}
StringCopyMax (test_num, buf, intmon + 1); offset = intmon + 1; // Skip the comma //Step #2: Loop to place testname in test_name intmon = FindPattern (buf, offset, 20, ",", 0, 0); for (x = 0, y = offset; y < intmon; y++) { if (!isspace (buf[y])) { test_name[x++] = buf[y]; } } offset = intmon + 1; DebugPrintf ("Buffer: %s\n", buf); DebugPrintf ("test_num: %s\n", test_num); DebugPrintf ("test_name: %s\n", test_name); DebugPrintf ("Final buffer position: %d\n", offset);

 

 



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 2
(4,639 Views)