Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Read strings from a one dimensional buffer...

Reply
Thread Tools

Read strings from a one dimensional buffer...

 
 
imma
Guest
Posts: n/a
 
      11-10-2005
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...

 
Reply With Quote
 
 
 
 
David Resnick
Guest
Posts: n/a
 
      11-10-2005

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

 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      11-10-2005
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.
 
Reply With Quote
 
imma
Guest
Posts: n/a
 
      11-10-2005
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..

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-10-2005
"imma" <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
sathyashrayan
Guest
Posts: n/a
 
      11-11-2005

imma <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To convert a one dimensional array into a two dimensional array using C amrutha0303 Software 0 08-03-2010 10:02 PM
How do copy Strings from a single dimensional array to double dimensional array Venkat C++ 4 12-05-2003 09:23 AM
Re: Two dimensional pointers and Two dimensional arrays Icosahedron C++ 8 08-21-2003 05:15 AM
Re: Two dimensional pointers and Two dimensional arrays John Harrison C++ 4 08-19-2003 04:00 PM
Re: Two dimensional pointers and Two dimensional arrays Alf P. Steinbach C++ 0 08-18-2003 08:25 AM



Advertisments