how do i concatenate a an array.

This is the code.

char data [128];
char c, s, l;
char *cmd_array [128];
count = 0;	
char * pch_W;
pch_W = strtok ( inbuf, " " );
	while ( pch_W != NULL ) {
		cmd_array_W [ count ] = pch_W;
		pch_W = strtok ( NULL, " " );
	        count ++;
  	}
	c = atoi ( cmd_array_W [ 1 ] );
	s = atoi ( cmd_array_W [ 2 ] );		
	l = atoi ( cmd_array_W [ 3 ] );
        strcpy(data, cmd_array[4]);

this piece if code works, but the problem is if data is more that 1 character.

basically, inbuf would be something like w 1 1 12 hello world --> this actually dont work

I slit inbuf by spaces and store them in respective variables. if data is 1 character, my programs works fine, but if its something like the above one its doesnt work.

how do i concatenate the char array from a certain position, or how do i split inbuf into 2 and store in data the 2nd half?

You don't initialize cmd_array at all (reread you source) then you are trying to "strcpy" uninitialized cmd_array[4] pointer.

Where is cmd_array_W declaration?
Where is count value test (cout > 4)?
Are you sure that inbuf contains more than 4 tokens? Why?
...

i changed the source code a bit when posting. i dont have a problem with the syntax, just a way to concatenate a bunch of characters or spliting a string into 2.

I don't understand the question. Perhaps if you describe what you are trying to do. In general, people seem to be overusing strtok. Often it's not the way to go. Your's looks like a job for sscanf or fscanf, but it is hard to tell without more info.

I used that before, but ran into many problems.

suppose there is an input entered by the user in the form
W 1 1 12 hello world
a variable char mode = w
a variable int c = 1
a variable int s = 1
a variable int l = 12
a variable string data = hello world

so i use strtok and split the string by spaces. now i want to put hello world back together.

how do i do it?

drjay

You just want to know how to concatenate two strings in C?

strcat(my_buffer, my_other_buffer);

Although if you're going to placing your new tokens in a completely new area via strcpy and then a strcat you might as well sscanf as I believe it'll be more efficient.

> I used that before, but ran into many problems.

It's still the way to go. Try out this program:

#include <stdio.h>

#define SIZE 100

int main()
{
    char inbuf[] = "w 1 1 12 hello world";
    char c, s[ SIZE ];
    int d1, d2, d3;
    sscanf( inbuf, "%c %d %d %d %[^\n]", &c, &d1, &d2, &d3, s );
    printf( "%c\n%d\n%d\n%d\n%s", c, d1, d2, d3, s );
}

this what i ended up doing. pretty inefficient i guess.

string tempbuf;
						stringstream ss ( inBuf );
						vector < string > tokens;
						while ( ss >> tempbuf ) {
							tokens.push_back ( tempbuf );
						}
						c = atoi ( tokens.at ( 1 ).c_str() );
						s = atoi ( tokens.at ( 2 ).c_str() );
						l = atoi ( tokens.at ( 3 ).c_str() );
						data = "";
						for ( int i = 4; i < tokens.size(); i ++ ) {
							data.append ( tokens.at ( i ) );
							data.append ( " " );
						}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.