Re: Problem Debugging C Program



John Hanley <hanley@xxxxxxxxxxx> writes:

Ben Pfaff wrote:
John Hanley <hanley@xxxxxxxxxxx> writes:

Sounds good. In the meantime, here is the code if you have the time to
take a peek at it.

Here's one problem:

blp@blp:~(1)$ gcc -Wall foo.c
foo.c: In function 'extract_records':
foo.c:472: warning: unused variable 'max_fields'
foo.c: In function 'main':
foo.c:613: warning: format '%s' expects type 'char *', but argument 3 has type 'struct FILE *'


I cleared both of these out and still no dice.

First, the good news. I strongly suspect you are hitting this problem:

case 4: strcpy(et,field); /* get elapsed time as string */
break;

et has not been given anywhere to point. It is an uninitialised 'char
*'.

The bad news: you need to fix a lot of other stuff! I found it very
hard to coax you code to take any data without hitting buffer
overflows and so on. You need to find a more robust way to parse the
data.

Take, for example this:

char f[256]={0};
int i=0;
char c='\0';
int po;

po=*ptr_offset;

This is confusing to old-timers. =* used to be how we now write *=.

while(1)
{
sscanf(line+po,"%c",&c); /* read in a char from line */

This is a loooong way to write 'c = line[po];'

if((c==DELIMITER) || (c=='\n') || (c==EOF))

c is unlikely to be == EOF because EOF is a negative int and c is a
char. You probably mean to test for the end of the string here (c ==
'\0') rather than for EOF which either happened long ago when the line
was read or will happen some time in the future.

Also, DELIMITER suggests a macro name and it might be better to use
one here (you have DELIMITER as a char variable set to '\t').

break;
f[i]=c;
i++;
po++;
}

f[i]='\0'; /* null terminate string */

All this to put the first tab delimited field into f. It is usully
better to make ones loop explicit and up-font:

while ((c = line[po]) != '\t' && c != '\n' && c != '\0') {
if (i < sizeof f - 1)
f[i++] = c;
po++;
}
f[i] = '\0';

I probably would not write it like this, but I want to show an
incremental change you can make. Note that I check there is room for
the data in f;

Later, there is this:

char ampm[3];
....
sscanf(field+po, "%s",ampm);

if( (strcmp(ampm,"pm")==0) || (strcmp(ampm,"PM")==0) )
add=12;

This is dangerous (you get a buffer overrun in the input does not have
exactly a two-character string at this point) but also long winded.
You don't use 'ampm' later, so you can just test:

if ((strcmp(line+po, "pm") == 0) || strcmp(line+po, "PM") == 0))
add=12;

It would help if got familiar with what C provides for doing string
manipulation. One function you will want to learn is strtol -- it
converts to an integer (like the atoi you use) put it reports on error
and how much it "consumed" so you don't need to do stuff like this:

sscanf(field,"%s",date_str);
po=po+strlen(date_str)+1;

sscanf(field+po, "%s",time_str);
po=po+strlen(time_str)+1;

--
Ben.
.



Relevant Pages

  • Re: Newbie - stuck - need a small push on higher order procedures
    ... (convert a string into a list of words). ... ; DELIMITER? ... (if (char? ... ; NEXT-DELIMITER ...
    (comp.lang.scheme)
  • Re: whats wrong with this code
    ... fgetcreturns an int and that's for a good reason: EOF isn't a char ... And what happens if the string you wrote into the file was empty and the ... delimiter character is all there is for that string? ... to find that out because you don't look at the first character. ...
    (comp.unix.programmer)
  • Re: convert nulls to spaces in file
    ... how about read in the file to a string and do a split with the null ... char being the delimiter? ... >> seperated by nulls instead of spaces (a higher up company created it this ...
    (microsoft.public.dotnet.languages.vb)
  • Re: tolower conflict with iostream?
    ... a string will not contain EOF. ... Although the 'is*' and 'to*' account for EOF, ... for negative values of 'char' on platforms where 'char' is a signed ...
    (comp.lang.cpp)
  • Re: tokenizer
    ... I know that we can use strtok to tokenize a string but by using that ... we can only use 1 char as a delimiter. ... the problem solvers are busy doing their ...
    (comp.lang.c)