How to declare char variabel in .h file and on that use strcat operation.

Hello,

I am trying to do is declaring all my variables in .h file. The variables declared in .h file will be referred in .c file.

My code is as follows:

//headerFile.h
#include <iostream>
#include <string>

class headerFile {

public:
const char nameOfE[];
};

//mainFile.c

#include "headerFile.h"
#include<string>

void token2 () {
headerFile hf;

string nameOfEle = "ABC_def_xyz.txt";
char *cstr;
char *token[20];

cstr = new char [nameOfEle.size()+1];
strcpy (cstr, nameOfEle.c_str());

int count = 0;
token[0] = strtok(cstr, "_.");
while (token[count] != NULL)
{
count++;
token[count] = strtok (NULL, "_.");
}
for (int i = 0; i <= count -1 ; i++)
{
printf ("%s \n", token[i]);
}

char hf.nameOfE[] = token[0];
printf("\nThis is name of material= %s\n",hf.nameOfE);
strcat(hf.nameOfE, "_"); //HERE GIVES ERROR AS Parameter miss match
cout << "After adding :: " << hf.nameOfE<< endl;
}


I don't know how to do this. Please guide me.

Last edited on
Here's a version which will compile, but I made a few changes
1
2
3
4
5
6
7
8
9
//headerFile.h
#include <iostream>
#include <string>

class headerFile {

public:
    char * nameOfE;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//mainFile.cpp
#include "headerFile.h"
#include <string>
#include <cstdio>

    using std::string;
    using std::printf;
    using std::cout;
    using std::endl;

void token2();

int main()
{
    token2();
    return 0;
}



void token2 () {
    headerFile hf;

    string nameOfEle = "ABC_def_xyz.txt";
    char *cstr;
    char *token[20];

    cstr = new char [nameOfEle.size()+1];
    strcpy (cstr, nameOfEle.c_str());

    int count = 0;
    token[0] = strtok(cstr, "_.");
    while (token[count] != NULL)
    {
        count++;
        token[count] = strtok (NULL, "_.");
    }

    for (int i = 0; i <= count -1 ; i++)
    {
        printf ("%s \n", token[i]);
    }

    hf.nameOfE = token[0];
    printf("\nThis is name of material= %s\n",hf.nameOfE);
    strcat(hf.nameOfE, "_");
    cout << "After adding :: " << hf.nameOfE<< endl;
}
Thanks a lot for help. It works. But if I do strcat further then its not working. I don't know why? As per logic it should work.

I am trying to do :

hf.nameOfE = token[0];
printf("\nThis is name of material= %s\n",hf.nameOfE);
strcat(hf.nameOfE, "_");
cout << "After adding :: " << hf.nameOfE<< endl;
//This part I am trying to add
hf.secondElement = token[1];
strcat(hf.nameOfE,hf.secondElement); //Its not working
cout << "Further addition of strings:" << hf.nameOfE << endl; //The output is: ABC_. But I was expecting ABC_def.

Would you guide me further?

Thank you.
The problems arise because the class headerFile uses simple pointers.
Then when you do strcat() you are actually trying to change whatever the pointer is pointing to. In other words, it is trying to change the list of tokens.

I think you need to allocate some space so that headerFile will keep its own separate data.
1
2
3
4
5
6
7
8
class headerFile {

public:
//    char * nameOfE;
//    char * secondElement;
    char nameOfE[100];
    char secondElement[100];
};


Then instead of this:

    hf.nameOfE = token[0];

you should do this:
 
    strcpy(hf.nameOfE, token[0]);

and similarly with the second element.
Then the strcat() function should work.
Topic archived. No new replies allowed.