Help needed for strings

S

skumar434

Hi everybody,

I am faceing problem with strings.

The code is given bellow .In this program i am tring to copy data from
a file into structure .
I am able to copy the data ,but the dat is needed to run some commands
,as u can see i have a string which has the command.

The command is---- chicmd debug 0xB701 0x000000ce3 0xAABBCCDD
I have to repalce the AABBCCDD withe data which is fetched from the
file.
In my program I am tring to fetch one string from the array "cmdbuffer
" the string is "AABBCCDD" with the dat which is fetched form the file
, which is "info->homid".
by using strstr() I am able to fetch the string an replce with it with
the intended one.But its only one time .


I am getting this out put -------


chicmd debug 0xB701 0x000000ce3 0x00815000
00815002
00815002
Segmentation fault

When the progarm comes to 2 nd loop its throughing the segmentation
fault error.
I hope this due to the fact that the data in the array is modified
infirst loop and when strstr chels for the string it returns NULL for
failure .


Can any body suggest me how to over come this problem.


int main( int argc ,char *argv[])
{
int ret_code = 0;



//char l_msg[1024];
char * subptr = NULL;
//char cmdbuffer[150]="chicmd debug 0xB701 0x000000ce3
0xAABBCCDD";
char cmdbuffer[150];
char newcmd[512];
char homid[1024];
int8_t ret_c;
int8_t minimal_res = 1;
int8_t resource_no=4;
int8_t loop_size = 4;
//loop_size = Count_all();
//int8_t offset;
//int8_t str_len;
//resource_no = loop_size;
int counter = 0;
//int8_t res_count_deconfig = 0;

/*ret_c = Doreset();
if ( ret_c != 0)
{

//TCFCOMMENT("unable to reset");
}*/

ret_code = Gethom_id();

while(info->next!=NULL){
if(counter <= loop_size)
{
printf("%s\n",info->homid);
sprintf(homid,"%s",info->homid);
printf("%s\n",homid);
cmdbuffer[150]="chicmd debug 0xB701 0x000000ce3
0xAABBCCDD";
subptr = malloc(sizeof(char));
subptr = strstr(cmdbuffer,"0xAABBCCDD");

printf("hello");
subptr[2]=homid[0];
subptr[3]=homid[1];
subptr[4]=homid[2];
subptr[5]=homid[3];
subptr[6]=homid[4];
subptr[7]=homid[5];
subptr[8]=homid[6];
subptr[9]=homid[7];
sprintf(newcmd,"%s",cmdbuffer);

printf("%s\n",newcmd);
}
counter++;
info=info->next;
}

free(info);
return 0;
}

Thanks in advance
 
D

Default User

Hi everybody,

I am faceing problem with strings.

Can any body suggest me how to over come this problem.

This code is a huge mess. Why did you indent it like that? It's very
difficult to read.
int main( int argc ,char *argv[])
{
int ret_code = 0;



//char l_msg[1024];
char * subptr = NULL;
//char cmdbuffer[150]="chicmd debug 0xB701 0x000000ce3
0xAABBCCDD";
char cmdbuffer[150];
char newcmd[512];
char homid[1024];
int8_t ret_c;
int8_t minimal_res = 1;
int8_t resource_no=4;
int8_t loop_size = 4;
//loop_size = Count_all();
//int8_t offset;
//int8_t str_len;
//resource_no = loop_size;
int counter = 0;
//int8_t res_count_deconfig = 0;

/*ret_c = Doreset();
if ( ret_c != 0)
{

//TCFCOMMENT("unable to reset");
}*/

ret_code = Gethom_id();

while(info->next!=NULL){
if(counter <= loop_size)
{
printf("%s\n",info->homid);
sprintf(homid,"%s",info->homid);
printf("%s\n",homid);
cmdbuffer[150]="chicmd debug 0xB701
0x000000ce3 0xAABBCCDD";

This is illegal AND malformed. You assigned a pointer to char to a
char, that is if you had not written it outside the bounds of the
array. That's probably your crash right there.

I THINK you were trying to copy that string to the buffer.

strcpy(cmdbuffer, "chicmd debug . . . ");
subptr = malloc(sizeof(char));

You allocated a pointer to 1 char.
subptr = strstr(cmdbuffer,"0xAABBCCDD");

You then threw away that allocated memory by assigning the result from
strstr() to the same pointer. What was the purpose of the malloc(),
what did you think it does?
printf("hello");
subptr[2]=homid[0];
subptr[3]=homid[1];
subptr[4]=homid[2];
subptr[5]=homid[3];
subptr[6]=homid[4];
subptr[7]=homid[5];
subptr[8]=homid[6];
subptr[9]=homid[7];
sprintf(newcmd,"%s",cmdbuffer);

printf("%s\n",newcmd);
}
counter++;
info=info->next;
}

free(info);
return 0;
}

Thanks in advance




Brian
 
S

Stan Milam

Hi everybody,

I am faceing problem with strings.

The code is given bellow .In this program i am tring to copy data from
a file into structure .
I am able to copy the data ,but the dat is needed to run some commands
,as u can see i have a string which has the command.

The command is---- chicmd debug 0xB701 0x000000ce3 0xAABBCCDD
I have to repalce the AABBCCDD withe data which is fetched from the
file.
In my program I am tring to fetch one string from the array "cmdbuffer
" the string is "AABBCCDD" with the dat which is fetched form the file
, which is "info->homid".
by using strstr() I am able to fetch the string an replce with it with
the intended one.But its only one time .
Why don't you use sprintf() to build the command string? E.G.:

char cmdstr[100];
char format[] = "chicmd debug 0xB701 0x000000ce3 0x%s";

..
..
..
sprintf( cmdstr, format, "00815002" );

Replace the string constant above with your input from the file.
I am getting this out put -------


chicmd debug 0xB701 0x000000ce3 0x00815000
00815002
00815002
Segmentation fault

When the progarm comes to 2 nd loop its throughing the segmentation
fault error.
I hope this due to the fact that the data in the array is modified
infirst loop and when strstr chels for the string it returns NULL for
failure .


Can any body suggest me how to over come this problem.


int main( int argc ,char *argv[])
{
int ret_code = 0;



//char l_msg[1024];
char * subptr = NULL;
//char cmdbuffer[150]="chicmd debug 0xB701 0x000000ce3
0xAABBCCDD";
char cmdbuffer[150];
char newcmd[512];
char homid[1024];
int8_t ret_c;
int8_t minimal_res = 1;
int8_t resource_no=4;
int8_t loop_size = 4;
//loop_size = Count_all();
//int8_t offset;
//int8_t str_len;
//resource_no = loop_size;
int counter = 0;
//int8_t res_count_deconfig = 0;

/*ret_c = Doreset();
if ( ret_c != 0)
{

//TCFCOMMENT("unable to reset");
}*/

ret_code = Gethom_id();

while(info->next!=NULL){
if(counter <= loop_size)
{
printf("%s\n",info->homid);
sprintf(homid,"%s",info->homid);
printf("%s\n",homid);
cmdbuffer[150]="chicmd debug 0xB701 0x000000ce3
0xAABBCCDD";
subptr = malloc(sizeof(char));
subptr = strstr(cmdbuffer,"0xAABBCCDD");

printf("hello");
subptr[2]=homid[0];
subptr[3]=homid[1];
subptr[4]=homid[2];
subptr[5]=homid[3];
subptr[6]=homid[4];
subptr[7]=homid[5];
subptr[8]=homid[6];
subptr[9]=homid[7];
sprintf(newcmd,"%s",cmdbuffer);

printf("%s\n",newcmd);
}
counter++;
info=info->next;
}

free(info);
return 0;
}

Thanks in advance


--
Regards,
Stan Milam
=============================================================
Charter Member of The Society for Mediocre Guitar Playing on
Expensive Instruments, Ltd.
=============================================================
 
S

skumar434

Hi everybody ,

First of all thanks for the suggestion .I have one more question.

How to compile two .C files at once?
let me explain it ..
I have two C files,in the first one i have used the function and in the
second file i have defined the function.

Do i have to compile these files different way ?


thanks in advance







Stan said:
Hi everybody,

I am faceing problem with strings.

The code is given bellow .In this program i am tring to copy data from
a file into structure .
I am able to copy the data ,but the dat is needed to run some commands
,as u can see i have a string which has the command.

The command is---- chicmd debug 0xB701 0x000000ce3 0xAABBCCDD
I have to repalce the AABBCCDD withe data which is fetched from the
file.
In my program I am tring to fetch one string from the array "cmdbuffer
" the string is "AABBCCDD" with the dat which is fetched form the file
, which is "info->homid".
by using strstr() I am able to fetch the string an replce with it with
the intended one.But its only one time .
Why don't you use sprintf() to build the command string? E.G.:

char cmdstr[100];
char format[] = "chicmd debug 0xB701 0x000000ce3 0x%s";

.
.
.
sprintf( cmdstr, format, "00815002" );

Replace the string constant above with your input from the file.
I am getting this out put -------


chicmd debug 0xB701 0x000000ce3 0x00815000
00815002
00815002
Segmentation fault

When the progarm comes to 2 nd loop its throughing the segmentation
fault error.
I hope this due to the fact that the data in the array is modified
infirst loop and when strstr chels for the string it returns NULL for
failure .


Can any body suggest me how to over come this problem.


int main( int argc ,char *argv[])
{
int ret_code = 0;



//char l_msg[1024];
char * subptr = NULL;
//char cmdbuffer[150]="chicmd debug 0xB701 0x000000ce3
0xAABBCCDD";
char cmdbuffer[150];
char newcmd[512];
char homid[1024];
int8_t ret_c;
int8_t minimal_res = 1;
int8_t resource_no=4;
int8_t loop_size = 4;
//loop_size = Count_all();
//int8_t offset;
//int8_t str_len;
//resource_no = loop_size;
int counter = 0;
//int8_t res_count_deconfig = 0;

/*ret_c = Doreset();
if ( ret_c != 0)
{

//TCFCOMMENT("unable to reset");
}*/

ret_code = Gethom_id();

while(info->next!=NULL){
if(counter <= loop_size)
{
printf("%s\n",info->homid);
sprintf(homid,"%s",info->homid);
printf("%s\n",homid);
cmdbuffer[150]="chicmd debug 0xB701 0x000000ce3
0xAABBCCDD";
subptr = malloc(sizeof(char));
subptr = strstr(cmdbuffer,"0xAABBCCDD");

printf("hello");
subptr[2]=homid[0];
subptr[3]=homid[1];
subptr[4]=homid[2];
subptr[5]=homid[3];
subptr[6]=homid[4];
subptr[7]=homid[5];
subptr[8]=homid[6];
subptr[9]=homid[7];
sprintf(newcmd,"%s",cmdbuffer);

printf("%s\n",newcmd);
}
counter++;
info=info->next;
}

free(info);
return 0;
}

Thanks in advance


--
Regards,
Stan Milam
=============================================================
Charter Member of The Society for Mediocre Guitar Playing on
Expensive Instruments, Ltd.
=============================================================
 
J

jmcgill

Hi everybody ,

First of all thanks for the suggestion .I have one more question.

How to compile two .C files at once?
let me explain it ..
I have two C files,in the first one i have used the function and in the
second file i have defined the function.

Do i have to compile these files different way ?

There's actually two steps, hidden from you I suppose, when you only
compile one file. The steps are "compiling" and "linking".

You can compile each .c file into a .o (object) file as a separate step,
and then link them (and also link them to standard libraries, etc.).

Or you can compile and link two or more .c files into an executable.

On my platform it's simply a matter of specifying the output and the
source files:

$ cc -o runme source1.c source2.c



Now that you've gotten into the "more than one source file" territory,
it might be a good day for you to start learning how to use make. You
will be glad you did that sooner, not later.

Eventually you're going to grow a project to the point that you're
compiling separate units, making libraries, and linking things in
various ways. It's in your interest to really learn your tools well.
 
B

Ben Pfaff

How to compile two .C files at once?

It's best not to give a C file a capital ".C" suffix, if
possible, because some compilers interpret such an extension as
meaning the file contains C++. Use lowercase ".c" where
available.
let me explain it ..
I have two C files,in the first one i have used the function and in the
second file i have defined the function.

Do i have to compile these files different way ?

Not normally. Usually, you compile them separately in the same
ordinary way, and then you "link" them together, possibly in a
separate step. However, the details of how you compile and link
a program are outside the scope of comp.lang.c, so you're better
off asking for details in some newsgroup specific to your system
or your C implementation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top