[A89] Re: Another Stupid String Question


[Prev][Next][Index][Thread]

[A89] Re: Another Stupid String Question




Have you seen the strtok() function?

Assume we have three strings.

#define	DELIMITERS	"\\"	// how we are to delimit the string...

// these should probably NOT be global variables...
const char *path = "main\\testprog";	// gotta double that backslash since
it's escaped...

char pathName[9];	// 8 chars max + 1 for the terminator
char varName[9];	// ditto...

void separatePath(const char *path, char *pathName, char *varName) {
	char tpath[18];	// 8 for the path, 8 for the variable, 1 for the backslash,
and 1 for the terminator
	char *token;

	tpath[0] = 0;
	pathName[0] = 0;
	varName[0] = 0;	// erase the strings

	strncpy(tpath,path,17);	// copy the path string into the temp path string

	if ((token = strtok(tpath,DELIMITERS)) != NULL) {	// tokenize the string
		strncpy(pathName,token,8);	// put the path into the pathName variable...
	}

	if ((token = strtok(NULL,DELIMITERS)) != NULL) {	// get the next token
		strncpy(varName,token,8);			// copy the variable name
	}
}

That should work providing 1: the const char *path is a string separated by
a backslash with a string on either side. The path and variable name are 8
characters max, and you have pathName and varName defined properly.

I hope this helps,

John David Ratliff
jdratlif@cs.indiana.edu

-----Original Message-----
From: assembly-89-bounce@lists.ticalc.org
[mailto:assembly-89-bounce@lists.ticalc.org]On Behalf Of
Brett1228@aol.com
Sent: Thursday, June 28, 2001 10:50 PM
To: assembly-89@lists.ticalc.org
Subject: [A89] Another Stupid String Question


I have a string declared like this:

char *string;

This string will contain a pathName and variableName, such as
"main\testprog". I want to put the pathName in one string, and the
variableName in the other. I just can't figure this out! I've been trying to
use strncat and strncpy, but nothing I do works.

Can anyone come up with code to do this for me? Thanks a lot!






References: