LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to parse a string into a dynamic allocated array?

Solved!
Go to solution

Hi, I wish to take a string such as "RHO .121,.042,.026,.016,.011,.010,.011,.011,.012,.016,.014,.005,.008,.037,.021,.021,.018" and parse it into a double array. The thing is, I won't know how many numbers are going to be in this array, because it will be read off of a file that I don't write. I have something similar to start off with posted below. I think it was Robert B. who had helped me with that one previously. Anyway, if there isn't enough room in the array, I'd like to make it bigger to fit it all. The reason I don't want to assign the array as rhoArray[100] is that after running this function, I'd be able to use "(sizeof(rhoArray)/sizeof(rhoArray[0])" to get how many elements are in this array. The new function prototype would look like "int readAndParse(char *dataString, double dArray[],char *delimiter);"

Thanks in advance!

 

int readAndParse(char *dataString, double dArray[], int *numParams){
	char dataStringCopy[256];
	char *tempPointer;
	int paramCount,returnCount,doublesFound=0;
	//initiallize the return value to No Error
	int statusReturned=0;
	//copy string before using strtok which replaces delimiters with NULLs
	strcpy(dataStringCopy,dataString);
	//DebugPrintf ("  Parsing string %s...\n", dataString);
	
	if(tempPointer=strtok(dataString," "))
		//first get the number of doubles to be read
		if(sscanf(tempPointer,"%d",&paramCount)<=0){
			DebugPrintf("  Error reading number of parameters from string.\n  Possible format error in string.\n");
			statusReturned=-1;
		}
		else if(paramCount>*numParams){
			DebugPrintf("  Parameter count too high for array size.\n  %d parameters requested, %d elements available in array.\n",paramCount,numParams);
			statusReturned=-4;
		}
		else{
			//read the next parameter
			tempPointer=strtok(NULL," ");
			while(tempPointer && doublesFound < paramCount){
				//DebugPrintf("%s\n",tempPointer);
				if(sscanf(tempPointer,"%lf",&dArray[doublesFound])<=0){
					DebugPrintf("  Error reading parameter %s.\n  Possible format error in string.\n",tempPointer);
					statusReturned=-2;
				}
				else
					doublesFound++;
					
				//read the next parameter
				tempPointer=strtok(NULL," ");
			}
			if(doublesFound!=paramCount){
				DebugPrintf("  Missing parameter: expected %d parameters, found %d.\n",paramCount,doublesFound);
				statusReturned=-3;
			}
			else if (tempPointer){
				DebugPrintf("  Format error in string: expected %d parameters.\n  Found additional parameter %s.\n",paramCount,tempPointer);
				statusReturned=1;
			}
		}
	*numParams=doublesFound;
	return statusReturned;
}

 Also, I saw these posts, but wasn't quite sure how to implement malloc into this.. one, two

0 Kudos
Message 1 of 4
(5,273 Views)
Solution
Accepted by topic author TurboMetrologist

Hi TurboMetrologi,

 

I have created a simple example that shows you how to read in a string with numbers and delimiters and then put the nymbers in a double array. I used a string control and a call back function to get my string and rund the code. I hope that this helps.

int CVICALLBACK INPUT (int panel, int control, int event,
		void *callbackData, int eventData1, int eventData2)
{

	char *tok = NULL;
	int num  = 1; 
	int a = 0;
	
	switch (event)
	{
		case EVENT_COMMIT:
			
			GetCtrlVal (panelHandle, PANEL_STRING, input);
			tok = strtok (input, " \\"); 
		    while ((tok = strtok (NULL, " \\")) != NULL) 
			{	  
				num  = num+1;
				
			}  
			tokens = (double *)malloc(num * sizeof(double));
			GetCtrlVal (panelHandle, PANEL_STRING, input);
			tok = strtok (input, " \\");  
			tokens[a] = atof(tok);  
			a++; 
		    while ((tok = strtok (NULL, " \\")) != NULL)  
			{
				tokens[a] = atof(tok); 
				a++;   
			} 
			
			break;
	}
	return 0;
}

 

Applications Engineer
National Instruments
Message 2 of 4
(5,246 Views)

Works brilliantly, thanks!

0 Kudos
Message 3 of 4
(5,233 Views)

I edited it into a function you can just drop into any code,

cheers!

//Function Prototype

double *readAndParse(char *string,char *delim);

 

//Example of a call

...

static double* myArray;

myArray=readAndParse(dataBuffer," ");

...

 

//Function

//-------------------------------------------------------------------------------------------
// Read a String and Parse it to a Double Array
//-------------------------------------------------------------------------------------------
double *readAndParse(char *string,char *delim){
    static double* tokens;
    int num=1;
    int a=0;
    sprintf(tempString,"%s",string);
    pointer=NULL;
    pointer = strtok (string," ");
    while((pointer=strtok(NULL," "))!=NULL){
        num=num+1;
    }
    tokens=(double *)malloc(num * sizeof(double));
    pointer = strtok (tempString, " ");
    tokens[a]=atof(pointer);
    a++;
    while((pointer=strtok(NULL," "))!=NULL){
        tokens[a]=atof(pointer);
        a++;
    }
return tokens;
}

0 Kudos
Message 4 of 4
(5,227 Views)