slurping in binary data

G

George

George said:
On Fri, 28 Nov 2008 17:23:53 -0800, Barry Schwarz wrote:

#include <stdio.h>
#include <stdlib.h>

#define PATH "george.txt"
#define NUMBER 100
#define MAXFMTLEN 2000

int main(void)
{
FILE *fp;
char pattern[MAXFMTLEN];
char lbin[NUMBER];
char line[MAXFMTLEN];

if ((fp = fopen(PATH, "r")) == NULL ) {
fprintf(stderr, "can't open file\n");
exit(1);
}

sprintf(pattern, "%*s %40s", NUMBER-1);
This code has been severely mangled. As it stands now, you are
missing two arguments and invoke undefined behavior. But this is NOT
what you want.

Look at your previous code for constructing the format string in
pattern. (Or make life easy on yourself and initialize pattern with a
string literal.)

I want to ignore the first number in each line.

Then go back to something approaching what you had in previous versions
of this code:

sprintf(pattern, "%%*s %%%ds", NUMBER-1);

...
error? In general, step through the code with a debugger or add
debugging printf statements so you can determine exactly where the
error occurs.

Can't do it. Gdb is my only tool right now for debugging, and it is a
hardship.

Then insert debugging printf() calls.

...
One other question here:

if ((fp = fopen(PATH, "r")) == NULL ) {
fprintf(stderr, "can't open file\n");
exit(1);
}

if(ferror(fp))
perror(PATH);

If there is an error in opening the file, will execution go through both of
these?
Look at the code that executes if fopen fails. Something very special
happens on the second statement (ignoring the portability issue).

I see nothing remarkable in returning one. Under what circumstances does
ferror do something?

The program exit()s immediately if fopen() returns a null pointer.
Therefore, the stream is guaranteed to not be in an error state by the
time that ferror() is called.. As a result, ferror() can never return a
non-zero value, and therefore perror() never gets executed. I doubt that
this is what you intended.

Do to George's excessive trimming, some intervening code was omitted.
His call to ferror followed a call to fgets. The point I was trying
to get him to realize was that if fopen failed his program called exit
and consequently the call to ferror would never be executed.

That's what I thought, which is why I brought it up, and I apologize for
any mis-edits. I get confused easily lately.
--
George

Everywhere that freedom stirs, let tyrants fear.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
G

George

Then insert debugging printf() calls.

What event would I be testing for? If C calls windows for a file on this
my only machine, it's gonna get it. Would these events be more appropriate
to the circumstances of a sysadmin?
--
George

The legislature's job is to write law. It's the executive branch's job to
interpret law.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
G

George

Have you considered just not babbling at all? Surely that would be
better than a "balance" between sense and nonsense.

Usenet, for me, is a multicultural and multilingual Erscheinung.

The above sentence is not comprehensible to some. Oh well.

I think a legitimate criticism of clc for the last few years is that it has
been "fair and balanced," in particular, where *you* tip the scales.
--
George

Free nations are peaceful nations. Free nations don't attack each other.
Free nations don't develop weapons of mass destruction.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
B

Barry Schwarz

What event would I be testing for? If C calls windows for a file on this
my only machine, it's gonna get it. Would these events be more appropriate
to the circumstances of a sysadmin?

You are not testing for an event. You are indicating that your
program has reached a certain point.

Your original complaint was that the program had a runtime error and
you didn't know where it was. By inserting frequent printf
statements, even if all they say is "step 1", "step 2", etc, you will
have an execution log. When your program aborts after a particular
printf you will know that the error occurred between that printf and
the next one.
 
K

Keith Thompson

George said:
Usenet, for me, is a multicultural and multilingual Erscheinung.

The above sentence is not comprehensible to some. Oh well.

The above sentence is, I suspect, not comprehensible to most.

If you're not interested in being understood, why post?
I think a legitimate criticism of clc for the last few years is that it has
been "fair and balanced," in particular, where *you* tip the scales.

Please feel free to explain what that's supposed to mean.
Or don't, and I'll feel free to ignore it.
 
J

James Kuyper

George said:
What event would I be testing for? If C calls windows for a file on this
my only machine, it's gonna get it. Would these events be more appropriate
to the circumstances of a sysadmin?

For most of my career, include all of the last 12 years, I've written
programs that run in an xterm command line window (comparable to a
console window in Windows), so there's no problem using printf() to
display message. As a result, I tend to forget the needs of other types
of programs. Sorry!

If I understand you correctly (I'm not sure that I do), you're not
working in such an environment, and there's some reason why you can't
open a log file on your machine. In that case, the corresponding GUI
solution would be pop up a message box. The key point is to get the
information about what's going on in your program out to where you can
see it; choosing the appropriate medium to use for this communication is
just a matter of convenience.

What events should you look for? That's where your knowledge of the
program comes in. My own most fundamental rule for debugging programs is
that I must figure out precisely why it is that I don't expect the bug
to occur. I must have an argument, with a series of premises assumed to
be true, that reaches the conclusion that the bug should not be
happening. Since the bug IS happening (and I don't even reach this point
in debugging until after I've verified the existence of the bug), then
here's only two possibilities: either the argument is invalid, or at
least one of the premises is false. I need to determine which.

I review the argument's validity carefully; if it's not valid, then the
bug apparently exploited a hole in that argument, this will often tell
me what I need to do to find the bug.

If the argument appears valid, then I test each of the premises for the
argument. That's where a code review, use of a debugger, or use of
debugging printf()s or fprintf()s or message boxes comes in. I place
them to give me information about whether my assumptions about the
behavior of the code are correct.

I usually find that one of the premises is false. Sometimes that
immediately tells me what's wrong. More often, I need to apply this
procedure recursively: I now need to determine why I incorrectly thought
that the premise should be true. However, the recursion always
terminates eventually.
 
G

Guest

It was a dutch matter.

so if I emailed this to my Dutch-speaking relatives they'd
be able to decode it for me? I Strongly suspect not.
 When I'm given to be cryptic, I don't make much
sense to most people.

this should tell you something
 It's not unlike one of my expressions for necessary
success, where I tell people, "well, let's return zero and get out of
here."

soulds like pretentious rubbish to me. Most of the people I know
socially aren't into computer science. This is why I try to use
normal english when communicating with them.

I've never had someone say, "are you alluding to C?", but they do register
that I want to leave pronto.

You can say that babbling is OT, but I think to balance it by categorically
addressing C in other posts.  

--
Nick Keighley

De maan likt niet hoog The moon doesn't look high
Maar het is niet zo But it is not so
De maan is wel hoog The moon is very high
Of niet sams? Or is it?
 
G

George

For most of my career, include all of the last 12 years, I've written
programs that run in an xterm command line window (comparable to a
console window in Windows), so there's no problem using printf() to
display message. As a result, I tend to forget the needs of other types
of programs. Sorry!

If I understand you correctly (I'm not sure that I do), you're not
working in such an environment, and there's some reason why you can't
open a log file on your machine. In that case, the corresponding GUI
solution would be pop up a message box. The key point is to get the
information about what's going on in your program out to where you can
see it; choosing the appropriate medium to use for this communication is
just a matter of convenience.

What events should you look for? That's where your knowledge of the
program comes in. My own most fundamental rule for debugging programs is
that I must figure out precisely why it is that I don't expect the bug
to occur. I must have an argument, with a series of premises assumed to
be true, that reaches the conclusion that the bug should not be
happening. Since the bug IS happening (and I don't even reach this point
in debugging until after I've verified the existence of the bug), then
here's only two possibilities: either the argument is invalid, or at
least one of the premises is false. I need to determine which.

I review the argument's validity carefully; if it's not valid, then the
bug apparently exploited a hole in that argument, this will often tell
me what I need to do to find the bug.

If the argument appears valid, then I test each of the premises for the
argument. That's where a code review, use of a debugger, or use of
debugging printf()s or fprintf()s or message boxes comes in. I place
them to give me information about whether my assumptions about the
behavior of the code are correct.

I usually find that one of the premises is false. Sometimes that
immediately tells me what's wrong. More often, I need to apply this
procedure recursively: I now need to determine why I incorrectly thought
that the premise should be true. However, the recursion always
terminates eventually.

Thanks for your comment. It seems relevant to the fella who's asking about
fclose elsethread. Making a log makes sense. I think a lot of C
programming overtests for failure, in particular, when the platform and
target is one's own PC as opposed to a memory-mapped device.

The execution in fortran programs is discussed in terms of recursion, which
I didn't read in yesterday's reading of chp 5 of 1256.pdf. Of course,
you're describing the thought process as it works for you.

gdb.exe has been very disapointing for me as a tool as compared to the
visual debuggers I grew accustomed to back when I could afford M$. Now I
call printf a poor man's debugger.

I've heard rumors that they're trying to make a visual version of gdb with
the bells, whistles and ability to step into/over. I won't hold my breath,
though.
--
George

We will build new ships to carry man forward into the universe, to gain a
new foothold on the moon and to prepare for new journeys to the worlds
beyond our own.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
C

Chad

Thanks for your comment. It seems relevant to the fella who's asking about
fclose elsethread. Making a log makes sense. I think a lot of C
programming overtests for failure, in particular, when the platform and
target is one's own PC as opposed to a memory-mapped device.

The execution in fortran programs is discussed in terms of recursion, which
I didn't read in yesterday's reading of chp 5 of 1256.pdf. Of course,
you're describing the thought process as it works for you.

gdb.exe has been very disapointing for me as a tool as compared to the
visual debuggers I grew accustomed to back when I could afford M$. Now I
call printf a poor man's debugger.

I've heard rumors that they're trying to make a visual version of gdb with
the bells, whistles and ability to step into/over. I won't hold my breath,
though.
--

If you are running Linux, then you can use DDD, which is a visual
version of gdb.


Chad
 
C

Chad

If you are running Linux, then you can use DDD, which is a visual
version of gdb.


And now, before the regulars interject, I would like to point out that
debuggers are off topic in this forum.

Chad
 
G

George

And now, before the regulars interject, I would like to point out that
debuggers are off topic in this forum.

Chad

That's just silly.

I've taken the program you posted and adapted it for my purpose. Since
this thread is old and fragmented, I'll make a new post to get to the next
goal.

Thanks for your comment, Chad.
--
George

The resolve of our great nation is being tested. But make no mistake, we
will show the world that we will pass the test.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
G

Guest

I think you're talking at cross purposes. James Kuyper is saying
IF you have a bug AND gdb is no help to you THEN consider using
printf(). You seem to doubt you have a bug.
Thanks for your comment.  It seems relevant to the fella who's asking about
fclose elsethread.  Making a log makes sense.  I think a lot of C
programming overtests for failure, in particular, when the platform and
target is one's own PC as opposed to a memory-mapped device.

in my experience most C programmers don't make enough checks for
failure. What do you mean "memory-mapped device"? Why don't
you care if the programs you run on your PC are correct or not?!
The execution in fortran programs is discussed in terms of recursion, which
I didn't read in yesterday's reading of chp 5 of 1256.pdf.  Of course,
you're describing the thought process as it works for you.

I've no idea what that means. His description of the basic
debugging process seemed exactly spot on to me.
gdb.exe has been very disapointing for me as a tool as compared to the
visual debuggers I grew accustomed to back when I could afford M$.

I've just recently used a line based debugger (as opposed to an IDE
based one) I was quite agreeably surprised how easy to use it was.
 Now I call printf a poor man's debugger.

whatever. I consider a log of program activity pretty
well indespensible.
I've heard rumors that they're trying to make a visual version of gdb with
the bells, whistles and ability to step into/over.  

I'm *really* surprised gdb can't do this already.
I've been using an ancient Sun line based debugger
and *that* did step into and step over.
I won't hold my breath though.

read the documentation
 
J

James Kuyper

George said:
Thanks for your comment. It seems relevant to the fella who's asking about
fclose elsethread. Making a log makes sense. I think a lot of C
programming overtests for failure, in particular, when the platform and
target is one's own PC as opposed to a memory-mapped device.

The execution in fortran programs is discussed in terms of recursion, which
I didn't read in yesterday's reading of chp 5 of 1256.pdf. Of course,
you're describing the thought process as it works for you.

I'm not sure what that comment means, since I'm not sure what it is that
Fortran describes in term of recursion. n1256.pdf uses the concept of
recursion in many places, but there's only one use of the term in the
first five sections: 5.1.1.2p4.

I suspect that you're thinking in terms of recursive function calls,
which section 6.5.2.2p11 requires to be supported in C. But I was using
the term in a broader sense, like most of the other uses of that term in
the standard, few of which involve function calls.

Let me give a simplified example. I recently investigated a bug where
there were missing packets from the output files of one of my programs.
I started by constructing an argument why the packets should not be
missing. Several of the key premises of that argument were based upon
certain assumptions about the contents of the input files. Because those
were the easiest things to check, they were also the first things I
checked (despite direct orders from upper management that I was not
supposed to "waste" any time investigating the "impossibility" that
those input files were defective). What I found was that the packets
were missing from my program's output for the simple reason that they
were missing from it's inputs; we knew from an alternative source that
those packets should have been there.

At this point, recursion kicks in. Having established that my original
argument was incorrect because one of it's premises was false, my next
step was to consider why I had expected those files to have those two
files to contain the same packets. It was because I had been told,
third-hand, that the alternative source of those packets was created by
processing the primary source. Therefore, there shouldn't be any packets
in the alternative source that weren't in the primary source. I decided
that I needed first hand information, rather than third-hand. It turns
out that neither of the two different sources we were looking at were
produced from the other. Both were produced from the same input files by
different programs.

Once again, recursion kicked in. Having established that the two
different programs were producing inconsistent outputs from the same
input files, it was time to consider why those programs were expected to
produce consistent outputs. I didn't handle that part of the
investigation, and don't know any details about it, because those
programs were someone else's responsibility. However, at my request that
person did investigate, and has reported discovering a bug which should
be fixed sometime in January.

I hope that makes it clearer what I meant about the debugging process
being recursive.
 
G

George

I think you're talking at cross purposes. James Kuyper is saying
IF you have a bug AND gdb is no help to you THEN consider using
printf(). You seem to doubt you have a bug.

Not every intermediate stage of a program is bug-infested.
in my experience most C programmers don't make enough checks for
failure. What do you mean "memory-mapped device"? Why don't
you care if the programs you run on your PC are correct or not?!

f = fopen( filename, "r" );
if( f == NULL )
if( f == NULL && f == NULL )
if( f == NULL )
if( really_really_NULL( f )) {
perror( filename );
exit( EXIT_FAILURE );
}
I've just recently used a line based debugger (as opposed to an IDE
based one) I was quite agreeably surprised how easy to use it was.


whatever. I consider a log of program activity pretty
well indespensible.

I'm gonna try to use one in my next thread.
I'm *really* surprised gdb can't do this already.
I've been using an ancient Sun line based debugger
and *that* did step into and step over.


read the documentation

Can't do it. The words sting like "read the Book of Mormon."
--
George

I just want you to know that, when we talk about war, we're really talking
about peace.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
G

George

I'm not sure what that comment means, since I'm not sure what it is that
Fortran describes in term of recursion. n1256.pdf uses the concept of
recursion in many places, but there's only one use of the term in the
first five sections: 5.1.1.2p4.

I suspect that you're thinking in terms of recursive function calls,
which section 6.5.2.2p11 requires to be supported in C. But I was using
the term in a broader sense, like most of the other uses of that term in
the standard, few of which involve function calls.

Let me give a simplified example. I recently investigated a bug where
there were missing packets from the output files of one of my programs.
I started by constructing an argument why the packets should not be
missing. Several of the key premises of that argument were based upon
certain assumptions about the contents of the input files. Because those
were the easiest things to check, they were also the first things I
checked (despite direct orders from upper management that I was not
supposed to "waste" any time investigating the "impossibility" that
those input files were defective). What I found was that the packets
were missing from my program's output for the simple reason that they
were missing from it's inputs; we knew from an alternative source that
those packets should have been there.

At this point, recursion kicks in. Having established that my original
argument was incorrect because one of it's premises was false, my next
step was to consider why I had expected those files to have those two
files to contain the same packets. It was because I had been told,
third-hand, that the alternative source of those packets was created by
processing the primary source. Therefore, there shouldn't be any packets
in the alternative source that weren't in the primary source. I decided
that I needed first hand information, rather than third-hand. It turns
out that neither of the two different sources we were looking at were
produced from the other. Both were produced from the same input files by
different programs.

Once again, recursion kicked in. Having established that the two
different programs were producing inconsistent outputs from the same
input files, it was time to consider why those programs were expected to
produce consistent outputs. I didn't handle that part of the
investigation, and don't know any details about it, because those
programs were someone else's responsibility. However, at my request that
person did investigate, and has reported discovering a bug which should
be fixed sometime in January.

I hope that makes it clearer what I meant about the debugging process
being recursive.

Thanks for your comment and the enlightening narrative. It reminds of
Hofstadter's amusing narratives that explore the nature of "if false."

My comment was meant to compare your thought process, the fortran standard,
and the C standard. I"ve only read the chapters on execution in both
syntaxes for the first time just this last week, so I make no pretense to
experience.

My impression is that fortran achieves through recursion what C does with
sequence points, as far as how the behavior in the abstract machine is
specified.

James, thanks for your comment throughout this thread. I see that my next
reply ends with an insult, so I'll declare abandon thread now.

Andere Mal,
--
George

The deliberate and deadly attacks which were carried out yesterday against
our country were more than acts of terror. They were acts of war.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
G

George

/* An example of overtesting for failure */

f = fopen( filename, "r" );
if( f == NULL )
if( f == NULL && f == NULL )
if( f == NULL )
if( really_really_NULL( f )) {
perror( filename );
exit( EXIT_FAILURE );
}

Although I've never actually seen that in practice.


I wouldn't doubt that you've never seen beautiful source, except that the
above is absurd and has abstract beauty.

<dripping sarcasm>I agree that people overtest for failure.
</dripping sarcasm> After all, it's not humanly possible to
inadvertently invoke "my-prog fooo" when you know that
the file foo exists and is the file which doesn't contain
important information while the file fooo sitting in the
same directory contains vital data that cannot be modified
by my-prog. Oops, is that a typo? Did I add an extra 'o' and
fopen failed and the program segfaulted, but only after
destroying months of work. Well, it's a good thing I
saved 10 seconds of time when I was writing the code and
didn't bother checking the return value of a system call!
Phew, 10 seconds of precious programmer time saved!


It goes to legibility, something that gets short schrift in C.
Checking the return value of a system call is not just
a good idea...it's the law! It doesn't matter if it's
your own PC or someone else's. It doesn't matter if
you "know" that fclose will always succeed (because
you're wrong). I don't know exactly what you mean by
"overtesting for failure", but I suspect it means
that you are a complete idiot.

You can think what you like. My name's George.
--
George

This was not an act of terrorism, but it was an act of war.
George W. Bush

Picture of the Day http://apod.nasa.gov/apod/
 
J

James Kuyper

George said:
Not every intermediate stage of a program is bug-infested.

My advice was, ultimately, in response to your statement in the message
with the header

Date: Fri, 28 Nov 2008 16:45:58 -0700

that "This compiles but gives me a runtime." That statement doesn't make
any sense. Barry Schwarz responded by suggesting that what you actually
meant was that "This compiles but gives me a runtime error." My advice,
and many of the comments by other people on this thread since then has
all been based upon the assumption that Barry was correct about that.

If you didn't have a runtime-error, the meaning of your comment is
obscure, to say the least. If you did have a runtime error, then I think
the code clearly was "bug-infested".
 

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

No members online now.

Forum statistics

Threads
473,794
Messages
2,569,641
Members
45,354
Latest member
OrenKrause

Latest Threads

Top