wait until \n appears in string

Discussion in 'C Programming' started by Robert Mens, Oct 25, 2003.

  1. Robert Mens

    Robert Mens Guest

    Hi,

    how is it possible to check if an \n appears in a string to
    process the data before the \n and then stack up the following
    data until another \n appears?

    I need this for something with sockets to know when the data
    is complete

    example

    recieved data =

    login\nrobert\nme

    next recieved data
    ns\n

    that it'll become

    login -- this will be processed so the variable can be recycled
    robert -- ''
    mens -- ''

    I really don't have a clue to do something like this...
    I am used to visual basic where i could use a loop,
    but i can't seem to find a function which copy's a part
    of a string, from point a to point b

    only from start to point b

    Can anyone help me???

    Thanks in advance,

    Robert


    --
    ln[dot]tenalp[at]snem_trebor
    read reversed and fill dot&at for email
    Robert Mens, Oct 25, 2003
    #1
    1. Advertising

  2. Robert Mens <> wrote:
    > Hi,


    > how is it possible to check if an \n appears in a string to
    > process the data before the \n and then stack up the following
    > data until another \n appears?
    >
    > I need this for something with sockets to know when the data
    > is complete


    There are no sockets in Standard C, which is the sole topic discussed here.
    Standard C defines fgets(), which reads lines from a file pointer. In Unix
    you can finagle a socket into a file pointer using fdopen(3). Maybe there's
    an equivalent in Win32. You should post this question on a Win32 newsgroup.
    If you are in fact using Unix, checkout comp.unix.programmer.

    - Bill
    William Ahern, Oct 25, 2003
    #2
    1. Advertising

  3. in comp.lang.c i read:

    >how is it possible to check if an \n appears in a string to
    >process the data before the \n and then stack up the following
    >data until another \n appears?


    sounds like a job for fgets.

    --
    a signature
    those who know me have no need of my name, Oct 25, 2003
    #3
  4. Robert Mens

    Al Bowers Guest

    Robert Mens wrote:

    > Hi,
    >
    > how is it possible to check if an \n appears in a string to
    > process the data before the \n and then stack up the following
    > data until another \n appears?
    >
    > I need this for something with sockets to know when the data
    > is complete
    >
    > example
    >
    > recieved data =
    >
    > login\nrobert\nme
    >
    > next recieved data
    > ns\n
    >
    > that it'll become
    >
    > login -- this will be processed so the variable can be recycled
    > robert -- ''
    > mens -- ''
    >
    > I really don't have a clue to do something like this...
    > I am used to visual basic where i could use a loop,
    > but i can't seem to find a function which copy's a part
    > of a string, from point a to point b
    >


    If your received data is a rigid format as you described, you
    may be able to write a function that will use function sscanf
    to parse the two packets (strings).

    #include <stdio.h>
    #include <string.h>

    typedef char string[30];

    int ParseUser(const string str1, const string str2, string user,
    string passwd);

    int main(void)
    {
    string string1, string2, user,password;

    strcpy(string1,"login\nrobert\nme");
    strcpy(string2,"\nns");
    if(ParseUser(string1,string2,user,password))
    printf("User: %s\nPassword: %s\n",user,password);
    else puts("ERROR....");

    strcpy(string1,"login\nrwashington\nsecret");
    if(ParseUser(string1,NULL,user,password))
    printf("\nUser: %s\nPassword: %s\n",user,password);
    else puts("ERROR....");
    return 0;
    }

    int ParseUser(const string str1, const string str2, string user,
    string passwd)
    {
    string tmp;

    if(str1)
    {
    if(2 != sscanf(str1,"%*s\n%29s\n%29s",user,passwd))
    return 0;
    else
    if(str2)
    {
    if(1 != sscanf(str2,"\n%29s",tmp))
    return 0;
    else
    {
    strncat(passwd,tmp,30-strlen(tmp));
    passwd[29] = '\0';
    }
    }
    }
    return 1;
    }


    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
    Al Bowers, Oct 25, 2003
    #4
  5. William Ahern wrote:

    > Robert Mens <> wrote:
    >> Hi,

    >
    >> how is it possible to check if an \n appears in a string to
    >> process the data before the \n and then stack up the following
    >> data until another \n appears?
    >>
    >> I need this for something with sockets to know when the data
    >> is complete

    >
    > There are no sockets in Standard C, which is the sole topic discussed
    > here. Standard C defines fgets(), which reads lines from a file pointer.
    > In Unix you can finagle a socket into a file pointer using fdopen(3).
    > Maybe there's an equivalent in Win32. You should post this question on a
    > Win32 newsgroup. If you are in fact using Unix, checkout
    > comp.unix.programmer.


    I've never heard such nonsense. The question is clearly on-topic, and the
    answer is simple: strchr.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Oct 25, 2003
    #5
  6. Robert Mens wrote:

    > Hi,
    >
    > how is it possible to check if an \n appears in a string to
    > process the data before the \n and then stack up the following
    > data until another \n appears?


    strchr() can find '\n'. memcpy() and memmove() can copy a string fragment.
    (Don't forget to null-terminate such fragments correctly.) The rest is
    pointers, really.

    <snip>

    > I really don't have a clue to do something like this...
    > I am used to visual basic where i could use a loop,
    > but i can't seem to find a function which copy's a part
    > of a string, from point a to point b
    >
    > only from start to point b


    If point a and point b are within the same string, use memmove().

    memmove(s + b, s + a, length);

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Oct 25, 2003
    #6
  7. Robert Mens

    Micah Cowan Guest

    William Ahern <william@wilbur.25thandClement.com> writes:

    > Robert Mens <> wrote:
    > > Hi,

    >
    > > how is it possible to check if an \n appears in a string to
    > > process the data before the \n and then stack up the following
    > > data until another \n appears?
    > >
    > > I need this for something with sockets to know when the data
    > > is complete

    >
    > There are no sockets in Standard C, which is the sole topic discussed here.
    > Standard C defines fgets(), which reads lines from a file pointer. In Unix
    > you can finagle a socket into a file pointer using fdopen(3). Maybe there's
    > an equivalent in Win32. You should post this question on a Win32 newsgroup.
    > If you are in fact using Unix, checkout comp.unix.programmer.


    His question has absolutely nothing to do with sockets, however
    (he only mentions them as his data source), so he was in fact
    completely topical.

    --
    Micah J. Cowan
    Micah Cowan, Oct 25, 2003
    #7
  8. Robert Mens

    nobody Guest

    "Robert Mens" <> wrote in message
    news:bncd7t$a2g$...
    > Hi,
    >
    > how is it possible to check if an \n appears in a string to
    > process the data before the \n and then stack up the following
    > data until another \n appears?
    >
    > I need this for something with sockets to know when the data
    > is complete
    >
    > example
    >
    > recieved data =
    >
    > login\nrobert\nme
    >
    > next recieved data
    > ns\n
    >
    > that it'll become
    >
    > login -- this will be processed so the variable can be recycled
    > robert -- ''
    > mens -- ''
    >
    > I really don't have a clue to do something like this...
    > I am used to visual basic where i could use a loop,
    > but i can't seem to find a function which copy's a part
    > of a string, from point a to point b
    > only from start to point b
    >

    Not sure what are you asking. Sockets are OT here, and
    function copying string *not* from start is same as function
    coying it from start, namely strcpy(). IMHO "string" as such
    in C doesn't exist (I don't count string *literal*). So if you are
    asking is "how to get "robert" from "login\nrobert\n", one possible
    answer is e.g.

    char name[20];
    const char* s = "login\nrobert\n";
    const char* p = strchr(s, '\n');
    strcpy(name, p+1); /* this is your "from point a", if that was a question */
    *(strchr(name, '\n')) = 0;

    Of course, in real code you would use loop, strncpy(), and possibly
    strtok(), if you can alter data coming from socket. As to how to get one
    continuous string from multiple socket reads, I'm afraid it's OT,
    and I don't know answer from top of my head, and I think you don't
    need it anyway.
    nobody, Oct 25, 2003
    #8
  9. Richard Heathfield <> wrote:
    > William Ahern wrote:
    >> Robert Mens <> wrote:
    >>> Hi,

    >>
    >>> how is it possible to check if an \n appears in a string to
    >>> process the data before the \n and then stack up the following
    >>> data until another \n appears?
    >>>
    >>> I need this for something with sockets to know when the data
    >>> is complete

    >>
    >> There are no sockets in Standard C, which is the sole topic discussed
    >> here. Standard C defines fgets(), which reads lines from a file pointer.
    >> In Unix you can finagle a socket into a file pointer using fdopen(3).
    >> Maybe there's an equivalent in Win32. You should post this question on a
    >> Win32 newsgroup. If you are in fact using Unix, checkout
    >> comp.unix.programmer.


    > I've never heard such nonsense. The question is clearly on-topic, and the
    > answer is simple: strchr.


    The OP said in VB the solution involved a simple loop. Probably
    something like similar to:

    while (line = socket.readline())
    ...

    It seems to me the most analagous solution in C (and simplest overall),
    would be to do:

    char buf[1024];
    char *line;
    while ((line = fgets(buf,sizeof buf,fp)))
    ...

    And for the best and most complete answer, he should follow-up in another
    newsgroup. But maybe I was reading into the question too much. So be it.
    William Ahern, Oct 25, 2003
    #9
  10. Robert Mens

    Micah Cowan Guest

    "nobody" <> writes:

    >IMHO "string" as such
    > in C doesn't exist (I don't count string *literal*).


    YHO is wrong, according to the standard:

    char foo[] = "a string";

    foo[] is now a string.

    char bar[7] = "string?";

    bar[] is not (no terminating null character).

    bar[6] = '\0';

    Now it is.

    --
    Micah J. Cowan
    Micah Cowan, Oct 25, 2003
    #10
  11. Robert Mens

    nobody Guest

    "Micah Cowan" <> wrote in message
    news:...
    > "nobody" <> writes:
    >
    > >IMHO "string" as such
    > > in C doesn't exist (I don't count string *literal*).

    >
    > YHO is wrong, according to the standard:
    >
    > char foo[] = "a string";
    >
    > foo[] is now a string.
    >
    > char bar[7] = "string?";
    >
    > bar[] is not (no terminating null character).
    >
    > bar[6] = '\0';
    >
    > Now it is.
    >

    Yep. It's 7.1.1 in N869. What I had in mind (but failed to state) is that
    IMHO they don't exist as data types, only as data (contiguous sequence
    of chars terminated by "the first null character", and it's not even related
    to OP's question (it was just in back of my mind few previous posts with
    copying over not-allocated memory and/or via uninitialized pointer).
    nobody, Oct 25, 2003
    #11
  12. Robert Mens

    Dan Pop Guest

    In <bnd54t$95b$> Richard Heathfield <> writes:

    >William Ahern wrote:
    >
    >> Robert Mens <> wrote:
    >>> Hi,

    >>
    >>> how is it possible to check if an \n appears in a string to
    >>> process the data before the \n and then stack up the following
    >>> data until another \n appears?
    >>>
    >>> I need this for something with sockets to know when the data
    >>> is complete

    >>
    >> There are no sockets in Standard C, which is the sole topic discussed
    >> here. Standard C defines fgets(), which reads lines from a file pointer.
    >> In Unix you can finagle a socket into a file pointer using fdopen(3).
    >> Maybe there's an equivalent in Win32. You should post this question on a
    >> Win32 newsgroup. If you are in fact using Unix, checkout
    >> comp.unix.programmer.

    >
    >I've never heard such nonsense. The question is clearly on-topic, and the
    >answer is simple: strchr.


    It's a bit more complex, if you read OP's question carefully: the data
    to be processed is not (or may not be) entirely available in a memory
    buffer. To make the question topical, just assume that the data is
    coming from a stream instead of a socket. And then, the question becomes:
    how do I read lines of text from a stream? strchr is usually NOT the
    answer...

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Oct 27, 2003
    #12
  13. Dan Pop wrote:

    > In <bnd54t$95b$> Richard Heathfield
    > <> writes:
    >
    >>The question is clearly on-topic, and the
    >>answer is simple: strchr.

    >
    > It's a bit more complex, if you read OP's question carefully: the data
    > to be processed is not (or may not be) entirely available in a memory
    > buffer. To make the question topical, just assume that the data is
    > coming from a stream instead of a socket. And then, the question becomes:
    > how do I read lines of text from a stream? strchr is usually NOT the
    > answer...


    The answer I gave was to (the first part of) the question he actually asked,
    "how is it possible to check if an \n appears in a string" - strchr clearly
    being the Right Answer - BUT I agree with you that there was more to the
    question than that, which is why other replies elsethread were also
    valuable to the OP.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Oct 27, 2003
    #13
    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. Brad Smallridge

    wait until

    Brad Smallridge, Mar 23, 2005, in forum: VHDL
    Replies:
    2
    Views:
    599
    info_
    Mar 26, 2005
  2. TJS
    Replies:
    2
    Views:
    666
    Kevin Spencer
    Feb 11, 2004
  3. =?Utf-8?B?QnJpYW4gUnVwZXJ0?=

    Please wait until the debuggee has finished loading, then try aga

    =?Utf-8?B?QnJpYW4gUnVwZXJ0?=, Aug 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    565
    =?Utf-8?B?QnJpYW4gUnVwZXJ0?=
    Aug 13, 2004
  4. Allan Bruce

    Reworded: Wait until button pressed

    Allan Bruce, Jun 15, 2004, in forum: Java
    Replies:
    0
    Views:
    975
    Allan Bruce
    Jun 15, 2004
  5. Replies:
    2
    Views:
    843
    Andrew Thompson
    May 22, 2005
Loading...

Share This Page