Read strings from a one dimensional buffer...

I

imma

Hi Friends,

I am trying to create a log of some of the outputs. For this I am using
doing the following things...

char * buff = malloc(SIZE);

buff[0] = id_number
len = strlen(data);
memcpy(&buff[1], data, len);

and so on.....

So, in buffer, my data will be ...

1aaaaaaaaaaaa2bbbbbbbbbbbbbbbb3ccccccccccc....

For some reasons, I can not use the Files. So I have to stick to
malloced buffer.

Now, I want to read this data. For me, "1aaaaaaaaaaaa" is one set of
data wherein '1' is the id_number and "aaaaaaaaaaaa" is actual data.
Pls note that length of the actual data is unknown. So practically
there are no delimiters. I was counting on the id_number as the
delimiter. But when I use sscanf, it reads 1a instead of 1.

How can I read this ? Is there any better method?

Thanks in advance,
ImMa...
 
D

David Resnick

imma said:
Hi Friends,

I am trying to create a log of some of the outputs. For this I am using
doing the following things...

char * buff = malloc(SIZE);

buff[0] = id_number
len = strlen(data);
memcpy(&buff[1], data, len);

and so on.....

So, in buffer, my data will be ...

1aaaaaaaaaaaa2bbbbbbbbbbbbbbbb3ccccccccccc....

For some reasons, I can not use the Files. So I have to stick to
malloced buffer.

Now, I want to read this data. For me, "1aaaaaaaaaaaa" is one set of
data wherein '1' is the id_number and "aaaaaaaaaaaa" is actual data.
Pls note that length of the actual data is unknown. So practically
there are no delimiters. I was counting on the id_number as the
delimiter. But when I use sscanf, it reads 1a instead of 1.

How can I read this ? Is there any better method?

Thanks in advance,
ImMa...

Well, if there can be digits in the data or if you want more
than CHAR_MAX elements you are out of luck with the
format described.

Otherwise, you could use a combination of
strtol(buf, &endptr, 10);
and
strcspn(endptr, "0123456789");
to sort out your string.

-David
 
I

imma

I am going to use this code in real time environment. so i guess file
operation, malloc of buffers wont be feasible. Hence I will allocate
big chunk of memory during initialisation and use it for logging..
 
K

Keith Thompson

imma said:
I am going to use this code in real time environment. so i guess file
operation, malloc of buffers wont be feasible. Hence I will allocate
big chunk of memory during initialisation and use it for logging..

What code?

Don't assume we can see the article to which you're replying. You
need to provide some context so each followup can be read on its own.
See most of the followups posted to this newsgroup for examples.

Google makes this gratuitously difficult to do properly, but there
is a workaround:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

And please complain to Google about their broken interface.
 
S

sathyashrayan

imma said:
Hi Friends,

I am trying to create a log of some of the outputs. For this I am using
doing the following things...

char * buff = malloc(SIZE);

buff[0] = id_number
len = strlen(data);
memcpy(&buff[1], data, len);

and so on.....

So, in buffer, my data will be ...

1aaaaaaaaaaaa2bbbbbbbbbbbbbbbb3ccccccccccc....

For some reasons, I can not use the Files. So I have to stick to
malloced buffer.

Now, I want to read this data. For me, "1aaaaaaaaaaaa" is one set of
data wherein '1' is the id_number and "aaaaaaaaaaaa" is actual data.
Pls note that length of the actual data is unknown.
^^^^^^^^^
The strlen function takes care about that. The argument in strlen are passes
a pointer.

So practically
there are no delimiters. I was counting on the id_number as the
delimiter. But when I use sscanf, it reads 1a instead of 1.

How can I read this ? Is there any better method?
Looking at the section 7.21.5 of the 2005 std may help.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top