sizeof and character arrays?

Discussion in 'C Programming' started by lisp9000@gmail.com, Sep 13, 2007.

  1. Guest

    I was wondering the best way to define and loop through a character
    array. Most lines of the file I am processing are 80 characters long
    but when an error occurs in the client which created the log sometimes
    they can be much longer so what's the best way to determine the array
    and define it?

    char s[80];

    vs

    #define MAX 100
    char s[MAX];

    and

    int i;
    for (i=0; i < sizeof(s); i++) { printf("%s", s);)

    I can read each line of my log file using fgets, now should I use
    fixed arrays like:

    char s[80];
    FILE *fp;

    fp = fopen("foo.txt","r");
    fgets(s,sizeof(s),fp);

    Or would it be better assign a pointer value for the first char each
    line:

    char *s[80];
    fgets(*s,sizeof(s),fp);

    Considering that later on I will want to tokenize each line and send
    the values of the various tokens to different output files.

    Lisp 9000
     
    , Sep 13, 2007
    #1
    1. Advertising

  2. On Thu, 13 Sep 2007 22:54:02 -0000, ""
    <> wrote:

    >I was wondering the best way to define and loop through a character
    >array. Most lines of the file I am processing are 80 characters long
    >but when an error occurs in the client which created the log sometimes
    >they can be much longer so what's the best way to determine the array
    >and define it?
    >
    >char s[80];
    >
    >vs
    >
    >#define MAX 100
    >char s[MAX];
    >
    >and
    >
    >int i;
    >for (i=0; i < sizeof(s); i++) { printf("%s", s);)


    If you are going to read with fgets, then stop when s is '\0' or
    '\n'. Otherwise you will be printing residual characters from the
    previous lines.

    >
    >I can read each line of my log file using fgets, now should I use
    >fixed arrays like:
    >
    >char s[80];
    >FILE *fp;
    >
    >fp = fopen("foo.txt","r");
    >fgets(s,sizeof(s),fp);


    But you need to check to see if you got the entire line or fgets
    stopped because you ran out of room in the array.

    >
    >Or would it be better assign a pointer value for the first char each
    >line:
    >
    >char *s[80];


    This defines an array of 80 pointers, none of which point anywhere.

    >fgets(*s,sizeof(s),fp);


    *s is the first pointer in the array. It still doesn't point
    anywhere. This invokes undefined behavior.

    sizeof s is the size of the array of pointers, not the amount of space
    available for the next line. Using it in this fashion will probably
    cause a buffer overrun (unless you actually allocated 80*sizeof(char*)
    bytes, which makes no sense, for each line).

    >
    >Considering that later on I will want to tokenize each line and send
    >the values of the various tokens to different output files.


    As long as you finish with one line before reading the next, a single
    buffer should work fine.


    Remove del for email
     
    Barry Schwarz, Sep 14, 2007
    #2
    1. Advertising

  3. CBFalconer Guest

    "" wrote:
    >
    > I was wondering the best way to define and loop through a character
    > array. Most lines of the file I am processing are 80 characters
    > long but when an error occurs in the client which created the log
    > sometimes they can be much longer so what's the best way to
    > determine the array and define it?


    Just download and use (it's public domain, and portable standard C)
    ggets() (or fggets()). Then you can forget about the size of the
    buffer, etc. If you don't want the line after reading and testing
    it, just do:

    #include "ggets.h"
    char *line;
    ...
    while (0 == ggets(&line)) {
    /* process line */
    free(line);
    }

    See <http://cbfalconer.home.att.net/download/>

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Sep 14, 2007
    #3
  4. CBFalconer said:

    > "" wrote:
    >>
    >> I was wondering the best way to define and loop through a character
    >> array. Most lines of the file I am processing are 80 characters
    >> long but when an error occurs in the client which created the log
    >> sometimes they can be much longer so what's the best way to
    >> determine the array and define it?

    >
    > Just download and use (it's public domain, and portable standard C)
    > ggets() (or fggets()). Then you can forget about the size of the
    > buffer, etc. If you don't want the line after reading and testing
    > it, just do:
    >
    > #include "ggets.h"
    > char *line;
    > ...
    > while (0 == ggets(&line)) {
    > /* process line */
    > free(line);
    > }
    >
    > See <http://cbfalconer.home.att.net/download/>


    He asked for the best way. Your ggets function, whilst having the virtue
    of simplicity, suffers from drawbacks that have been discussed to death
    in this group and which might reasonably be considered to disqualify it
    from being "the best way", which is what was asked for. Of course, it
    all depends how one defines "best".

    The ggets approach is just one of several that have been considered
    here, and I wish you'd remember that instead of issuing blanket
    recommendations for a function that some consider fundamentally flawed,
    simply because you happened to be the one who wrote it.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Sep 14, 2007
    #4
    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. Derek
    Replies:
    7
    Views:
    24,422
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    664
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,507
    Lawrence Kirby
    May 12, 2005
  4. Alex Vinokur
    Replies:
    7
    Views:
    515
    Clark S. Cox III
    Aug 14, 2006
  5. Alex Vinokur

    sizeof (size_t) and sizeof (pointer)

    Alex Vinokur, Nov 12, 2007, in forum: C++
    Replies:
    19
    Views:
    814
    Ben Rudiak-Gould
    Nov 30, 2007
Loading...

Share This Page