sizeof and character arrays?

L

lisp9000

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
 
B

Barry Schwarz

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
 
C

CBFalconer

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/>
 
R

Richard Heathfield

CBFalconer said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,214
Latest member
JFrancisDavis

Latest Threads

Top