Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sizeof and character arrays?

Reply
Thread Tools

sizeof and character arrays?

 
 
lisp9000@gmail.com
Guest
Posts: n/a
 
      09-13-2007
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])

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

 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      09-14-2007
On Thu, 13 Sep 2007 22:54:02 -0000, "(E-Mail Removed)"
<(E-Mail Removed)> 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[i])


If you are going to read with fgets, then stop when s[i] 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
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      09-14-2007
"(E-Mail Removed)" 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

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-14-2007
CBFalconer said:

> "(E-Mail Removed)" 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
 
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
sizeof (size_t) and sizeof (pointer) Alex Vinokur C++ 19 11-30-2007 11:11 PM
sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) == 1?) Alex Vinokur C Programming 7 08-14-2006 04:57 PM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments