12-01-2014 11:02 AM
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;
12-02-2014 03:40 AM - edited 12-02-2014 03:43 AM
I see a bit of issues in your code:
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);