Hello,

This is an extension of this thread: http://www.daniweb.com/software-development/c/threads/357681/1525001#post1525001

My purpose is to create a class that handles a network message. Irrelevant, anyway.

CMyMessage:

#include "stdafx.h"
#pragma once
#include <conio.h>
#include <stdio.h>
#include <iostream>
#pragma once

using namespace std;
typedef unsigned char byte;


class CMyMessage {
	
protected:
	byte type;
	byte textlen;
	byte namelen;
	char *text;
	char *name;


	public:

	CMyMessage(byte* message)
	{
		
	}

	
	CMyMessage(byte Type, char *Name, byte Namelen, char *Text, byte Textlen){
	
		printf("\ns-a apelat constructorul\n");

		type= Type;

		name=( char*) malloc (Namelen+1);
		strcpy(name,Name);
		namelen=Namelen;
	
		text=( char* ) malloc (Textlen+1);
		strcpy(text, Text);
		textlen=Textlen;
		

		printf("\nType is: %d",type); 

		printf("\nName is:"); 

		while (*name)
			printf("%c",*name++); 

		printf("\nText is: ");

		while (*text){
			printf("%c",*text++);
		}


		printf("\nNamelen= %d",this->namelen);

		printf("\nTextlen= %d",this->textlen);
// The variables are stored correctly. Is it possible they are invalidated once the 
// function exists?

	}

	virtual ~CMyMessage(){
		free(text);
		free(name);
	}


	void GetMessageAsByteStream(byte *message, int *length){
				
		
		message[0]=GetMyMsgType();
		message[1]=namelen;
		message[2]=textlen;
		message[3]=(byte ) (this->GetMyMsgText());

		printf("\nmessage[0]= %d %c\n",message[0], message[0]);
		printf("\nmessage[1]= %d %c\n",message[1], message[1]);
		printf("\nmessage[2]= %d %c\n",message[2], message[2]);
		printf("\nmessage[3]= %d %c\n",message[3], message[3]);
		
		char *xxx=this->GetMyMsgText();

		char *yyy; yyy=this->GetMyMsgText();

		printf("\nName is :"); getch();

		while (*name)
			printf("%c",*name++); getch();

		if (xxx != NULL)
			printf("Msg is: %s\n", xxx);


		while (*yyy)
			printf("%c",*yyy++);

// there is absolutely NO WAY of accessing my NAME variable.. which is what I need to do.
		

		memcpy(message+3, this->GetMyMsgName(), this->namelen);
		
		printf("\n\nTEXTLEN= %d\n\n", this->textlen);

		memcpy(message+3+namelen, this->GetMyMsgText(), this->textlen);

		printf("Resulting message: ");

		//while (*message)
		//	printf("%c %d ",*message++, message);
	
		for (int i=0; i<55; i++)
			printf("%c", (char)message[i]);
		
	}


	char* GetMyMsgText() {return this->text;}
	char* GetMyMsgName() {return this->name;}
	byte  GetMyMsgType() {return this->tip;}





};

Main.cpp:

#include "stdafx.h"
#include <stdio.h>
#pragma once
#include "conio.h"
#include <string.h>
#include "CMyMessage.cpp"


void main()
{

	char *name="JOHN.";

	char *msg = "testing123.";

	byte *message= (byte *) malloc(256);

	int *ptrInt = (int *)malloc(sizeof(int));

	byte type=1;

	CMyMessage *mesaj=new CMyMessage(type, name, strlen(name), msg, strlen(msg));
	
	mesaj->GetMessageAsByteStream(message, ptrInt);
	
	printf("\ninside main(), type is: %d", *(message));
	printf("\ninside main(), name length is: %d", *(message+1));
	printf("\ninside main(), text length is: %d", *(message+2));

	getch();
	
}

How do I properly use GetMyMsgText() or GetMyMsgName() ?

Thank you.

Edited 5 Years Ago by Buffalo101: n/a

My guess is that you have not even attempted to compile that code. It contains errors that you need to fix. For example:

CMyMessage line 77: error -- you can not store a pointer in one byte of memory.

Line 77 is message[1]=namelen, which works (why wouldn't it?). I'm putting an unsigned char into an array of unsigned chars.

I have been playing on and off with this since friday. My teacher just gave us the function's header. Before you say something, look at: http://tinyurl.com/daniweb page 4.

I'm trying to implement that and use it into my SOCK_STREAM client+server.

The problem is I can't/don't know how to use the char * returned by the GetMyMsgName() method.

Edited 5 Years Ago by Buffalo101: n/a

line 77: what will that do if the value of namelen is greater than 255, the max value that can be put in a single character ?

You need to translate that link into English because I have no idea what it says, don't even know what language it is.

>>The problem is I can't/don't know how to use the char * returned by the GetMyMsgName() method

What do you want to use it for?

The value of namelen will never be greater than 255, it's 1-50.

The same goes for textlen: 1-200.

I can't and I won't translate all of that into English because it's irrelevant. If you look at the code, you can see I didn't just copy paste it.


I just need to access the char* that my GetMyMsgName() / GetMyMsgText() returns. Why do I need it?


Well, I have 5 protected variables in my class: type, namelen, name, textlen, text. I need to paste all their contents in one array of unsigned integers. Which I could simply do with memcpy.

BUT my problem is the GetMyMsgName() / GetMyMsgText() always return null or I don't know how to use them.

>>I can't and I won't translate all of that into English because it's irrelevant. I
Then why did you post the link if its not relevant information? But let's not get hung up on that.

>>Why do I need it?
I have no idea. Its your program not mine. Ask that of whoever designed the program requirements.

>>BUT my problem is the GetMyMsgName() / GetMyMsgText() always return null or I don't know how to use them.

Most likely because those two variables have never been initialized to anything. Check the class constructor.

>>I can't and I won't translate all of that into English because it's irrelevant. I
Then why did you post the link if its not relevant information? But let's not get hung up on that.

>>Why do I need it?
I have no idea. Its your program not mine. Ask that of whoever designed the program requirements.

>>BUT my problem is the GetMyMsgName() / GetMyMsgText() always return null or I don't know how to use them.

Most likely because those two variables have never been initialized to anything. Check the class constructor.

1. I pasted that because you accused me of not even compiling the code.

2.
You asked me why I needed the char* returned by my function. 2 lines below, I answered:

I have 5 protected variables in my class: type, namelen, name, textlen, text. I need to paste all their contents in one array of unsigned integers. Which I could simply do with memcpy.

The variables HAVE been properly initialized, because I print their values inside my constructor and they are perfect.

While "validating" (line 49 and below) you change name and text. They are not pointing to the correct places anymore.

49: 		while (*name)
			printf("%c",*name++);

Not that good....

while (*name)
    putchar(*name++);

Much better....

Why use all the overhead of printf() when putchar() is perfectly designed for the task?

Nezachem I answered to your post on last page, sorry. (Good to know, but this still doesn't solve my problem.)

What is name after *name++ ?

It points exactly to the first memory location AFTER my string. :D Thank you!

Now I just need to figure out how to store a char in my array of unsigned chars.

unsigned char *message= (byte *) malloc(256);

        char *ptrChar; ptrChar="E";

        message[0]= (unsigned char) ptrChar;

        printf("%c %d", message[0], message[0]);

My "old" approach doesn't work.

Edited 5 Years Ago by Buffalo101: n/a

Well, this is the working, trimmed version:

void GetMessageAsByteStream(byte *message, int *length){

		char *theName; theName = this->GetMyMsgName();
 
                for (int i=3; i<3+namelen; i++)    //0, 1, 2 indexes are already populated. 
                                                  //adding message[3]=name[0], message[4]=name[1] etc
		     message[i]=*theName++;   // I gave up on memcpy, this looks easier.
                                             // yes, it should be i<=3+namelen to include the \0 char, 
                                             //but this isn't the last char in my array. 

}

unsigned char *message= (byte *) malloc(256);
int *length;

GetMessageAsByteStream(message, *length);

while(*message)                // displays correctly.
    printf("%c",*message++);

Fix: as nezachem pointed out, I was going through the chars in the array inside my constructor by incrementing the actual pointer, not a temp one. By the time I was done, the pointer was "pointing" to a memory location right after my last element, instead of my first.

Thank you guys.

Edited 5 Years Ago by Buffalo101: n/a

This question has already been answered. Start a new discussion instead.