Read strings from a one dimensional buffer...

Discussion in 'C Programming' started by imma, Nov 10, 2005.

  1. imma

    imma Guest

    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...
    imma, Nov 10, 2005
    #1
    1. Advertising

  2. 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
    David Resnick, Nov 10, 2005
    #2
    1. Advertising

  3. imma

    Default User Guest

    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.
    Default User, Nov 10, 2005
    #3
  4. imma

    imma Guest

    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..
    imma, Nov 10, 2005
    #4
  5. "imma" <> 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.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Nov 10, 2005
    #5
  6. imma <> wrote in message
    news:...
    > 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.
    sathyashrayan, Nov 11, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    420
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,911
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    638
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    958
    Venkat
    Dec 5, 2003
  5. Neal Becker

    buffer creates only read-only buffer?

    Neal Becker, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    400
    Neal Becker
    Jan 8, 2009
Loading...

Share This Page