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/