By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,498 Members | 1,713 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,498 IT Pros & Developers. It's quick & easy.

Read strings from a one dimensional buffer...

P: n/a
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...

Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

imma wrote:
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

Nov 15 '05 #2

P: n/a
imma wrote:

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


What do you mean by this?
Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Nov 15 '05 #3

P: n/a
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..

Nov 15 '05 #4

P: n/a
"imma" <ma******@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5

P: n/a

imma <ma******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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.


Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.