Umesh said:
I modified the program in this way for my understanding. It works but
displays "(null)" in every line it fails to find out abc*xyz. What
should i do to stop that?
// find a string starting with abc and ending with xyz
#define SIZE 1000
#include <stdio.h>
#include <string.h>
int main(void)
{
int status;
FILE *infp, *outfp;
char buf[SIZE+1];
char *abc, *xyz;
infp = fopen("c:/1.txt", "r");
outfp = fopen("c:/2.txt", "w");
while (fgets(buf,SIZE,infp))
{
abc = strstr(buf, "abc");
if (abc != NULL)
xyz = strstr(abc + 3, "xyz");
if (xyz != NULL)
fprintf (outfp,"%.*s\n",(int)(xyz + 3 - abc), abc);
}
return 0;
}
You posted the same article twice. I think Google Groups is having
some sort of problem that causes this kind of error. Please complain
to them.
The compiler ignores indentation; it's used only to make the code
clearer to a human reader, but if the indentation doesn't match the
actual structure of the code, it just causes confusion.
Consider the statements within the body of the while loop:
abc = strstr(buf, "abc");
if (abc != NULL)
xyz = strstr(abc + 3, "xyz");
if (xyz != NULL)
fprintf (outfp,"%.*s\n",(int)(xyz + 3 - abc), abc);
As far as the compiler is concerned, this is exactly equivalent to this:
abc = strstr(buf, "abc");
if (abc != NULL)
xyz = strstr(abc + 3, "xyz");
if (xyz != NULL)
fprintf (outfp,"%.*s\n",(int)(xyz + 3 - abc), abc);
which, if it's indented *properly*, looks like this:
abc = strstr(buf, "abc");
if (abc != NULL)
xyz = strstr(abc + 3, "xyz");
if (xyz != NULL)
fprintf (outfp,"%.*s\n",(int)(xyz + 3 - abc), abc);
With proper indentation, you can see that if abc is equal to NULL, and
xyz is not equal to NULL, you execute the fprintf statement. Passing
a null pointer to fprintf for a "%s" format invokes undefined
behavior; in your implementation, it happens to print "(null)", but it
could do anything.
What you *probably* want is something like this:
abc = strstr(buf, "abc");
if (abc != NULL)
{
xyz = strstr(abc + 3, "xyz");
if (xyz != NULL)
fprintf (outfp,"%.*s\n",(int)(xyz + 3 - abc), abc);
}
but I haven't studied your program's logic closely enough to be sure.
To avoid problems like this, you should consider using something that
formats and indents your code for you. The "indent" tool, if you have
it, is pretty good; I don't use it much myself, but the "-kr" option
gives reasonable output. There are also editors that will format your
code for you as you type it.
Also, I recommend *always* using braces for control structures (if,
while, for), even when they just control a single statement. For
example, rather than this:
if (abc != NULL)
xyz = strstr(abc + 3, "xyz");
I'd write this:
if (abc != NULL) {
xyz = strstr(abc + 3, "xyz");
}
(I picked up this habit from Perl, which requires the braces; C
doesn't, but I still find it helpful, especially if I want to add a
second statement.)
Plenty of knowledgeable people are going to disagree with this advice;
you'll have to decide for yourself whether to follow it. But you need
to do *something* to make sure that your indentation matches the
actual logic of your program.
--
Keith Thompson (The_Other_Keith) (e-mail address removed) <
http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <
http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"