473,765 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help needed for strings

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_decon fig = 0;

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

//TCFCOMMENT("una ble to reset");
}*/

ret_code = Gethom_id();

while(info->next!=NULL){
if(counter <= loop_size)
{
printf("%s\n",i nfo->homid);
sprintf(homid," %s",info->homid);
printf("%s\n",h omid);
cmdbuffer[150]="chicmd debug 0xB701 0x000000ce3
0xAABBCCDD";
subptr = malloc(sizeof(c har));
subptr = strstr(cmdbuffe r,"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",n ewcmd);
}
counter++;
info=info->next;
}

free(info);
return 0;
}

Thanks in advance

Aug 17 '06 #1
5 1915
sk*******@gmail .com wrote:
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_decon fig = 0;

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

//TCFCOMMENT("una ble to reset");
}*/

ret_code = Gethom_id();

while(info->next!=NULL){
if(counter <= loop_size)
{
printf("%s\n",i nfo->homid);
sprintf(homid," %s",info->homid);
printf("%s\n",h omid);
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(cmdbuffe r, "chicmd debug . . . ");
subptr = malloc(sizeof(c har));
You allocated a pointer to 1 char.
subptr = strstr(cmdbuffe r,"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",n ewcmd);
}
counter++;
info=info->next;
}

free(info);
return 0;
}

Thanks in advance

Brian
--
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html >
Aug 17 '06 #2
sk*******@gmail .com wrote:
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_decon fig = 0;

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

//TCFCOMMENT("una ble to reset");
}*/

ret_code = Gethom_id();

while(info->next!=NULL){
if(counter <= loop_size)
{
printf("%s\n",i nfo->homid);
sprintf(homid," %s",info->homid);
printf("%s\n",h omid);
cmdbuffer[150]="chicmd debug 0xB701 0x000000ce3
0xAABBCCDD";
subptr = malloc(sizeof(c har));
subptr = strstr(cmdbuffe r,"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",n ewcmd);
}
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.
=============== =============== =============== =============== =
Aug 18 '06 #3
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 Milam wrote:
sk*******@gmail .com wrote:
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_decon fig = 0;

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

//TCFCOMMENT("una ble to reset");
}*/

ret_code = Gethom_id();

while(info->next!=NULL){
if(counter <= loop_size)
{
printf("%s\n",i nfo->homid);
sprintf(homid," %s",info->homid);
printf("%s\n",h omid);
cmdbuffer[150]="chicmd debug 0xB701 0x000000ce3
0xAABBCCDD";
subptr = malloc(sizeof(c har));
subptr = strstr(cmdbuffe r,"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",n ewcmd);
}
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.
=============== =============== =============== =============== =
Aug 18 '06 #4
sk*******@gmail .com wrote:
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.
Aug 18 '06 #5
sk*******@gmail .com writes:
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.
--
"It wouldn't be a new C standard if it didn't give a
new meaning to the word `static'."
--Peter Seebach on C99
Aug 18 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2972
by: Sims | last post by:
Hi, Given a string $txt and an array of strings $txt_array what would be the best/fastest way to search in _insensitive_ case if $txt is in $text_array and, if it is, where is it? Because I want to use the array with an ID Something like,
4
2212
by: pekka niiranen | last post by:
Hi there, I have perl script that uses dynamically constructed regular in this way: ------perl code starts ---- $result ""; $key = AAA\?01; $key = quotemeta $key; $line = " s^\?AAA\?01^BBB^g; #Comment "
40
3253
by: Peter Row | last post by:
Hi all, Here is my problem: I have a SQL Server 2000 DB with various NVarChar, NText fields in its tables. For some stupid reason the data was inserted into these fields in UTF8 encoding. However when you retrieve these values into a dataset and ToString() them
8
1664
by: SpOiLeR | last post by:
Hi there... I have something like this: #include <string> #include <list> #include <algorithm> using namespace std; typdef list<string> ls;
0
986
by: Jon | last post by:
We have a asp.net app that runs like the following in a single-site scenario. Directory structure: c:\inetpub\wwwroot\myapp - aspx files, global.asax and web.config c:\inetpub\wwwroot\myapp\bin - myapp.dll, strings.dll (localized strings), library.dll c:\inetpub\wwwroot\myapp\UserControls - ascx files c:\inetpub\wwwroot\myapp\TextBlocks - large localized text blocks in html format
8
1685
by: rh0dium | last post by:
Hi all, I am using python to drive another tool using pexpect. The values which I get back I would like to automatically put into a list if there is more than one return value. They provide me a way to see that the data is in set by parenthesising it. This is all generated as I said using pexpect - Here is how I use it.. child = pexpect.spawn( _buildCadenceExe(), timeout=timeout) child.sendline("somefunction()")
3
1088
by: lory88 | last post by:
Hi I hope someone can help me out with a very SIMPLE program about whole string permutations. That is: given a list of strings, the required outcome is a complete set of all their possible permutations. It's like character permutations of a string, but this time it is whole strings instead of single characters that have to be permuted. I need this because I don't remember exactly the password to open
9
3228
by: quyvle | last post by:
I can't seem to get this function to work correctly. I'm wondering if anyone could help me out with this. So I'm using the fscanf function to read the input stream and store each string in the appropriate variables. Here's what I'm reading from another file: "# Number of power catergories: 9"
5
1846
by: Kelth.Raptor | last post by:
Im having some difficulty with strings here, I hope someone is kind enough to help, I do appreciate it. Im working on a grade point average calculator for my intro to programming class and I thought I would go a bit above and beyond the scope of the class and use strings. But I ran into a snag with my getgrades function. The compiler gives me the error: "81 ISO C++ forbids comparison between pointer and integer" here is the code for the...
7
1892
by: DJ Dharme | last post by:
Hi, I really like to use stl as much as possible in my code. But I found it really hard to understand by looking into there source code. I have no idea about what iterator traits, heaps and allocators are. So I started to write my own container class to learn templates. I thought about a sorted vector class which have a additional two methods sort and insert sorted. The usage of this class is like this. 1. We can reserve some space and...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10164
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.