473,486 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to extract a string starting with 'abc' & ending with 'xyz' ?

I want to extract a string abc*xyz from a text file.
* indicates arbitrary no. of characters.

I'm only able to do it when the string has definite no. of characters
or the string length is constant: i.e. five or the string is abc?????
xyz
How can i generalize it for any length of the string?

May 26 '07 #1
34 2691
Umesh wrote:
I want to extract a string abc*xyz from a text file.
* indicates arbitrary no. of characters.

I'm only able to do it when the string has definite no. of characters
or the string length is constant: i.e. five or the string is abc?????
xyz
How can i generalize it for any length of the string?
Have you looked up regular expressions yet?

--
Ian Collins.
May 26 '07 #2
"Umesh" <fr****************@gmail.comschrieb im Newsbeitrag
news:11*********************@g37g2000prf.googlegro ups.com...
>I want to extract a string abc*xyz from a text file.
* indicates arbitrary no. of characters.

I'm only able to do it when the string has definite no. of characters
or the string length is constant: i.e. five or the string is abc?????
xyz
How can i generalize it for any length of the string?
You may want to look for regex, a librarx for regular expressions.
http://directory.fsf.org/regex.html

Bye, Jojo
May 26 '07 #3

"Umesh" <fr****************@gmail.comwrote in message
news:11*********************@g37g2000prf.googlegro ups.com...
>I want to extract a string abc*xyz from a text file.
* indicates arbitrary no. of characters.

I'm only able to do it when the string has definite no. of characters
or the string length is constant: i.e. five or the string is abc?????
xyz
How can i generalize it for any length of the string?
As others have pointed out, if you want to handle the geneneral problem of
"how can I match a string with charcteristics such and such" you want
regular expressions.
However for a one off simple pattern regular expressions are overkill.

Use strncmp() to compare the first three character to "abc". Call strlen()
to find the end of the string, move back three places (make sure you don't
move into memory you don't own, place minus one), the call strcmp() with
"xyz".

Warp it all up in a little function of its own
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

May 26 '07 #4
Umesh wrote:
I want to extract a string abc*xyz from a text file.
* indicates arbitrary no. of characters.

I'm only able to do it when the string has definite no. of characters
or the string length is constant: i.e. five or the string is abc?????
xyz
How can i generalize it for any length of the string?
I don't know how you can generalise it, because I don't
know what your specifically doing.

But, assuming you don't just want to fall back to using
regular expressions, I don't see what your problem is.

Find `abc` (easy-peasy strstr-squeezy). Then find `xyz`
starting from after the `b`. Viola.

Perhaps you haven't described your full problem?

--
Shakespeare Hedgehog
"Based on their behaviour so far -- I have no idea" /Sahara/

May 26 '07 #5
/* This is what I can do. If the length of the string starting with
abc & ending with xyz is known(11 in this case), I can program it as
follows. But if the length of the string varies between 10 to 50* what
should I do? Thanks. /

//abc?????xyz
#include<stdio.h>
#include<stdlib.h>
int main()
{

FILE *f,*fp;
f=fopen("c:/1.txt","r");
if(f==NULL)
{
puts("Error opening file");
exit(0);
}
fp=fopen("c:/2.txt","w");

char c[12];
while((c[0]=getc(f))!=EOF)
if(c[0]=='a' && (c[1]=getc(f))!=EOF && c[1]=='b' && (c[2]=getc(f))!
=EOF && c[2]=='c'&& (c[3]=getc(f))!=EOF && c[3]!=' ' && (c[4]=getc(f))!
=EOF && c[4]!=' ' && (c[5]=getc(f))!=EOF && c[5]!=' ' &&
(c[6]=getc(f))!=EOF && c[6]!=' ' && (c[7]=getc(f))!=EOF && c[7]!=' '
&& (c[8]=getc(f))!=EOF && c[8]=='x'&& (c[9]=getc(f))!=EOF &&
c[9]=='y' && (c[10]=getc(f))!=EOF && c[10]=='z')
{
c[11]='\0';
fprintf(fp,"%s\n",c);

}
fclose(f);
fclose(fp);
return 0;

}

May 26 '07 #6
Umesh wrote:
/* This is what I can do. If the length of the string starting with
abc & ending with xyz is known(11 in this case), I can program it as
follows. But if the length of the string varies between 10 to 50* what
should I do? Thanks. /
1) learn how to post.
2) find a regular expression library.

--
Ian Collins.
May 26 '07 #7

"Umesh" <fr****************@gmail.comha scritto nel messaggio
news:11**********************@g37g2000prf.googlegr oups.com...
/* This is what I can do. If the length of the string starting with
abc & ending with xyz is known(11 in this case), I can program it as
follows. But if the length of the string varies between 10 to 50* what
should I do? Thanks. /
No, it doesn't. "abc45678xyz" doesn't work.
You are only comparing the string with "abc xyz".
>
//abc?????xyz
#include<stdio.h>
#include<stdlib.h>
int main()
{

FILE *f,*fp;
f=fopen("c:/1.txt","r");
if(f==NULL)
{
puts("Error opening file");
Write that to stderr, not to stdout...
exit(0);
Use EXIT_FAILURE, not 0 which means 'success'...
}
fp=fopen("c:/2.txt","w");
Check it for NULL, too.
>
char c[12];
while((c[0]=getc(f))!=EOF)
EOF doesn't fit in a unsigned char and might compare equal to a
valid signed char.
see www.c-faq.com, question 12.1.
if(c[0]=='a' && (c[1]=getc(f))!=EOF && c[1]=='b' && (c[2]=getc(f))!
=EOF && c[2]=='c'&& (c[3]=getc(f))!=EOF && c[3]!=' ' && (c[4]=getc(f))!
=EOF && c[4]!=' ' && (c[5]=getc(f))!=EOF && c[5]!=' ' &&
(c[6]=getc(f))!=EOF && c[6]!=' ' && (c[7]=getc(f))!=EOF && c[7]!=' '
&& (c[8]=getc(f))!=EOF && c[8]=='x'&& (c[9]=getc(f))!=EOF &&
c[9]=='y' && (c[10]=getc(f))!=EOF && c[10]=='z')
What a mess...
What's wrong with
scanf("%11c", c);
if (!strcmp(c, "abc xyz"))...
{
c[11]='\0';
fprintf(fp,"%s\n",c);

}
fclose(f);
fclose(fp);
Check wheter these succeeded.
return 0;

}
Try:
/*not compiled, not tested*/
#define MAXLINE 16383
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int status;
FILE *infp, *outfp;
char buf[MAXLINE+1];
char *str, *endstr = NULL;
infp = fopen("c:/1.txt", "r");
if (infp == NULL) {
perror("Unable to open input file");
exit(EXIT_FAILURE);
}
outfp = fopen("c:/2.txt", "r");
if (outfp == NULL) {
perror("Unable to open or create output file");
fclose(infp);
exit(EXIT_FAILURE);
}
while (fgets(buf, MAXLINE, infp)) {
endstr = NULL;
(str = strstr(buf, "abc")) && (endstr = strstr(str,"xyz"));
if (endstr != NULL)
fprintf(outfp, "%*s", endstr-str+3, str);
}
status = ferror(infp) || ferror(infp);
if (fclose(infp)) {
perror("Unable to close input file");
status++;
}
if (fclose(outfp)) {
perror("Unable to close output file");
status++;
}
return status ? EXIT_FAILURE : 0;
}
May 26 '07 #8

"Army1987" <pl********@for.itha scritto nel messaggio
news:f3**********@tdi.cu.mi.it...
>
"Umesh" <fr****************@gmail.comha scritto nel messaggio
news:11**********************@g37g2000prf.googlegr oups.com...
>/* This is what I can do. If the length of the string starting with
abc & ending with xyz is known(11 in this case), I can program it as
follows. But if the length of the string varies between 10 to 50* what
should I do? Thanks. /
No, it doesn't. "abc45678xyz" doesn't work.
You are only comparing the string with "abc xyz".
>>
//abc?????xyz
#include<stdio.h>
#include<stdlib.h>
int main()
{

FILE *f,*fp;
f=fopen("c:/1.txt","r");
if(f==NULL)
{
puts("Error opening file");
Write that to stderr, not to stdout...
> exit(0);
Use EXIT_FAILURE, not 0 which means 'success'...
>}
fp=fopen("c:/2.txt","w");
Check it for NULL, too.
>>
char c[12];
while((c[0]=getc(f))!=EOF)
EOF doesn't fit in a unsigned char and might compare equal to a
valid signed char.
see www.c-faq.com, question 12.1.
> if(c[0]=='a' && (c[1]=getc(f))!=EOF && c[1]=='b' && (c[2]=getc(f))!
=EOF && c[2]=='c'&& (c[3]=getc(f))!=EOF && c[3]!=' ' && (c[4]=getc(f))!
=EOF && c[4]!=' ' && (c[5]=getc(f))!=EOF && c[5]!=' ' &&
(c[6]=getc(f))!=EOF && c[6]!=' ' && (c[7]=getc(f))!=EOF && c[7]!=' '
&& (c[8]=getc(f))!=EOF && c[8]=='x'&& (c[9]=getc(f))!=EOF &&
c[9]=='y' && (c[10]=getc(f))!=EOF && c[10]=='z')
What a mess...
What's wrong with
scanf("%11c", c);
if (!strcmp(c, "abc xyz"))...
> {
c[11]='\0';
fprintf(fp,"%s\n",c);

}
fclose(f);
fclose(fp);
Check wheter these succeeded.
>return 0;

}

Try:
/*not compiled, not tested*/
#define MAXLINE 16383
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int status;
FILE *infp, *outfp;
char buf[MAXLINE+1];
char *str, *endstr = NULL;
infp = fopen("c:/1.txt", "r");
if (infp == NULL) {
perror("Unable to open input file");
exit(EXIT_FAILURE);
}
outfp = fopen("c:/2.txt", "r");
if (outfp == NULL) {
perror("Unable to open or create output file");
fclose(infp);
exit(EXIT_FAILURE);
}
while (fgets(buf, MAXLINE, infp)) {
endstr = NULL;
(str = strstr(buf, "abc")) && (endstr = strstr(str,"xyz"));
if (endstr != NULL)
fprintf(outfp, "%*s", endstr-str+3, str);
fprintf(outfp, "%.*s\n", endstr-str+3, str);
}
status = ferror(infp) || ferror(infp);
status = ferror(infp) || ferror(outfp);
if (fclose(infp)) {
perror("Unable to close input file");
status++;
}
if (fclose(outfp)) {
perror("Unable to close output file");
status++;
}
return status ? EXIT_FAILURE : 0;
}

May 26 '07 #9
Umesh wrote:
I want to extract a string abc*xyz from a text file.
* indicates arbitrary no. of characters.

I'm only able to do it when the string has definite no. of characters
or the string length is constant: i.e. five or the string is abc?????
xyz
How can i generalize it for any length of the string?
Using what the Standard library provides, you can do

const char *the_string = ...;
const char *abc, *xyz;

abc = strstr(the_string, "abc");
if (abc != NULL) {
xyz = strstr(abc + 3, "xyz");
if (xyz != NULL) {
printf ("Found \"%.*s\"\n",
(int)(xyz + 3 - abc), abc);

--
Eric Sosman
es*****@acm-dot-org.invalid
May 26 '07 #10
"Umesh" wrote:
/* This is what I can do. If the length of the string starting with
abc & ending with xyz is known(11 in this case), I can program it as
follows. But if the length of the string varies between 10 to 50* what
should I do? Thanks. /
The first thing you should do is choose a language to learn and then focus
on *that* language. You have posted questions to the C++ group and it is a
more powerful language than C. (Note that "more powerful" does not mean the
same thing as "better".) So I suggest this:

o decide to learn C++
o post to the C++ group only.

OR (Big Google type OR)

o decide to learn C
o post to the C group only

Learning a language is difficult enough. Trying to learn two at the same
time simply compounds the problems.
May 26 '07 #11
Ian Collins wrote:
Umesh wrote:
>/* This is what I can do. If the length of the string starting
with abc & ending with xyz is known(11 in this case), I can
program it as follows. But if the length of the string varies
between 10 to 50* what should I do? Thanks. /

1) learn how to post.
2) find a regular expression library.
Also learn that a char cannot hold an EOF marker.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 26 '07 #12
Umesh wrote:
/* This is what I can do. If the length of the string starting with
abc & ending with xyz is known(11 in this case), I can program it as
follows. But if the length of the string varies between 10 to 50* what
should I do? Thanks. /

//abc?????xyz
#include<stdio.h>
#include<stdlib.h>
int main()
{

FILE *f,*fp;
f=fopen("c:/1.txt","r");
if(f==NULL)
{
puts("Error opening file");
exit(0);
}
fp=fopen("c:/2.txt","w");

char c[12];
while((c[0]=getc(f))!=EOF)
if(c[0]=='a' && (c[1]=getc(f))!=EOF && c[1]=='b' && (c[2]=getc(f))!
=EOF && c[2]=='c'&& (c[3]=getc(f))!=EOF && c[3]!=' ' && (c[4]=getc(f))!
=EOF && c[4]!=' ' && (c[5]=getc(f))!=EOF && c[5]!=' ' &&
(c[6]=getc(f))!=EOF && c[6]!=' ' && (c[7]=getc(f))!=EOF && c[7]!=' '
&& (c[8]=getc(f))!=EOF && c[8]=='x'&& (c[9]=getc(f))!=EOF &&
c[9]=='y' && (c[10]=getc(f))!=EOF && c[10]=='z')
{
c[11]='\0';
fprintf(fp,"%s\n",c);
Hell's Freelling Teeth, no /wonder/ you're having trouble if
you're trying to write it like that.

Ignoring the deeply relevant fact that you can't portably expect
EOF to be storable in a `char`, read an /entire line/ into a
string and look in /that/. `fgets` is your friend.

Yes, you pay in store. Yes, it's /possible/ to do the job
without having to store entire lines. But if you really
really want to go there, you don't have a C problem -- you
have an algorithms problem.
>
}
fclose(f);
fclose(fp);
return 0;

}
--
Far-Fetched Hedgehog
"The path to the web becomes deeper and wider" - October Project

May 26 '07 #13
I actually want to find words starting with a and ending with b in a
text file and put the output in a file. So there will be no spaces
between the words. .

May 26 '07 #14
"Umesh" wrote:
/* This is what I can do. If the length of the string starting with
abc & ending with xyz is known(11 in this case), I can program it as
follows. But if the length of the string varies between 10 to 50* what
should I do? Thanks. /

//abc?????xyz
#include<stdio.h>
#include<stdlib.h>
int main()
{

FILE *f,*fp;
f=fopen("c:/1.txt","r");
if(f==NULL)
{
puts("Error opening file");
exit(0);
}
fp=fopen("c:/2.txt","w");

char c[12];
while((c[0]=getc(f))!=EOF)
if(c[0]=='a' && (c[1]=getc(f))!=EOF && c[1]=='b' && (c[2]=getc(f))!
=EOF && c[2]=='c'&& (c[3]=getc(f))!=EOF && c[3]!=' ' && (c[4]=getc(f))!
=EOF && c[4]!=' ' && (c[5]=getc(f))!=EOF && c[5]!=' ' &&
(c[6]=getc(f))!=EOF && c[6]!=' ' && (c[7]=getc(f))!=EOF && c[7]!=' '
&& (c[8]=getc(f))!=EOF && c[8]=='x'&& (c[9]=getc(f))!=EOF &&
c[9]=='y' && (c[10]=getc(f))!=EOF && c[10]=='z')
{
c[11]='\0';
fprintf(fp,"%s\n",c);

}
fclose(f);
fclose(fp);
return 0;

}
You would be well served by trying something simpler first. Then you can
build on the knowledge you get

Do this: Write a C program that uses getc() to read a file, print the
output, and then prints "EOF received" after the last char in the file.

Note that, despite the message linking, I have read the post you made at
9:34 AM.
May 26 '07 #15

"Umesh" <fr****************@gmail.comwrote in message
news:11**********************@d30g2000prg.googlegr oups.com...
>I actually want to find words starting with a and ending with b in a
text file and put the output in a file. So there will be no spaces
between the words. .
In that case a regular expression type library might make more sense.

You might want to shift your definition of "word" to include or exclude
hyphens, forms with apostrophes, acronyms in upper case, and the like.
That's a lot easier if you have a regular expression rather than going the
hardcoding route.

Having extracted a word you should be able to examine the firstr character

str[0]

and the last

str[strlen(str)-1]

without any problem. Or having acquired a regular expression library you can
use that and do the job properly.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

May 26 '07 #16
Umesh wrote:
>
I actually want to find words starting with a and ending with b
in a text file and put the output in a file. So there will be no
spaces between the words. .
So why have you been wasting our time with all this nonsense?
Write a routine to "getword", which skips initial blank space
(including tabs, end-of-lines, etc.). Test the result, and either
output it or discard it.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 26 '07 #17
In article <11**********************@d30g2000prg.googlegroups .com>,
Umesh <fr****************@gmail.comwrote:
>I actually want to find words starting with a and ending with b in a
text file and put the output in a file. So there will be no spaces
between the words. .
All of these text searches you have asked about can be solved
by a simple technique called a "state machine".

set state to 0
while (inchar = getc()) != EOF { /* beginning of a line */
if machinestate is 0 {
if inchar is 'a' {
store inchar for later output
set machinestate to 1;
} else if inchar is '\n'
set machinestate to 0;
else
set machinestate to 99;
} else if machinestate is 1 { /* 'a' detected at beginning of line */
if inchar is '\n' {
discard saved input characters
set machinestate to 0;
} else if inchar is 'b' {
store inchar for later output
set machinestate to 2;
} else {
discard saved input characters
set machinestate to 99;
}
} else if machinestate is 2 { /* 'ab' detected at beginning of line */
if inchar is '\n' {
discard saved input characters
set machinestate to 0;
} else if inchar is 'c' {
store inchar for later output
set machinestate to 3;
} else {
discard saved input characters
set machinestate to 99;
}
} else if machinestate is 3 { /* 'abc' detected at beginning of line */
if inchar is '\n' {
discard saved input characters
set machinestate to 0;
} else if inchar is 'x' {
store inchar for later output
set machinestate to 4;
} else if inchar is a word character {
store inchar for later output
set machinestate to 3;
} else {
discard saved input characters
set machinestate to 99;
}
} else if machinestate is 4 { /* 'abc'*'x' detected */
if inchar is '\n' {
discard saved input characters
set machinestate to 0;
} else if inchar is 'y' {
store inchar for later output
set machinestate to 5;
} else if inchar is 'x' {
store inchar for later output
set machinestate to 4;
} else if inchar is a word character {
store inchar for later output
set machinestate to 3;
} else {
discard saved input characters
set machinestate to 99;
}
} else if machinestate is 5 { /* 'abc'*'xy' detected */
if inchar is '\n' {
discard saved input characters
set machinestate to 0;
} else if inchar is 'z' {
save inchar for later output
set machinestate to 6;
} else if inchar is 'x' {
save inchar for later output
set machinestate to 4;
} else if inchar is a word character {
store inchar for later output
set machinestate to 3;
} else {
discard saved input characters
set machinestate to 99;
}
} else if machinestate is 6 { /* 'abc'*'xyz' detected */
if inchar is '\n' {
output saved input characters
discard saved input characters
set machinestate to 0;
} else if inchar is 'x' {
save inchar for later output
set machinestate to 4;
} else if inchar is a word character {
machinestate inchar for later output
set machinestate to 3;
} else {
discard saved input characters
set machinestate to 99;
}
} else if inchar is '\n' /* machinestate must be 99 here. */
set machinestate to 0;
else
set machinestate to 99;
}

/* EOF reached */

if machinestate is 6 {
output saved input characters
discard saved input characters
} if machinestate is not 0 {
discard saved input characters
}
--
All is vanity. -- Ecclesiastes
May 27 '07 #18
"Walter Roberson" wrote:
In article <11**********************@d30g2000prg.googlegroups .com>,
Umesh <fr****************@gmail.comwrote:
>>I actually want to find words starting with a and ending with b in a
text file and put the output in a file. So there will be no spaces
between the words. .

All of these text searches you have asked about can be solved
by a simple technique called a "state machine".
Yes. But the OP still doesn't understand something as fundamental as
detecting EOF.

This despite the fact that he has been posting since December 2006.
May 27 '07 #19
osmium wrote:
"Walter Roberson" wrote:
>Umesh <fr****************@gmail.comwrote:
>>I actually want to find words starting with a and ending with b
in a text file and put the output in a file. So there will be no
spaces between the words. .

All of these text searches you have asked about can be solved
by a simple technique called a "state machine".

Yes. But the OP still doesn't understand something as fundamental
as detecting EOF.

This despite the fact that he has been posting since December 2006.
Has he really gone that long without learning anything? I guess it
is time to PLONK it.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 27 '07 #20
On Sat, 26 May 2007 13:37:38 +0200, "Army1987" <pl********@for.it>
wrote in comp.lang.c:
>
"Umesh" <fr****************@gmail.comha scritto nel messaggio
news:11**********************@g37g2000prf.googlegr oups.com...
/* This is what I can do. If the length of the string starting with
abc & ending with xyz is known(11 in this case), I can program it as
follows. But if the length of the string varies between 10 to 50* what
should I do? Thanks. /
No, it doesn't. "abc45678xyz" doesn't work.
You are only comparing the string with "abc xyz".

//abc?????xyz
#include<stdio.h>
#include<stdlib.h>
int main()
{

FILE *f,*fp;
f=fopen("c:/1.txt","r");
if(f==NULL)
{
puts("Error opening file");
Write that to stderr, not to stdout...
No, no, no, NO, NO!!!

The automatic assumption that all programs should write error messages
to stderr is one of the causes of a lot of grief by inconsiderate
programmers.

Programs that generate their primary output to stdout might be
justified in outputting error messages to stderr.

Programs that generate their output to a file, as this one attempts to
do, should indeed generate their error messages to stdout. And that
specifically includes compilers.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
May 27 '07 #21
Jack Klein <ja*******@spamcop.netwrites:
On Sat, 26 May 2007 13:37:38 +0200, "Army1987" <pl********@for.it>
wrote in comp.lang.c:
>"Umesh" <fr****************@gmail.comha scritto nel messaggio
news:11**********************@g37g2000prf.googleg roups.com...
[...]
puts("Error opening file");
Write that to stderr, not to stdout...

No, no, no, NO, NO!!!
Yes, yes, yes, YES, YES!!!
The automatic assumption that all programs should write error messages
to stderr is one of the causes of a lot of grief by inconsiderate
programmers.
What grief?
Programs that generate their primary output to stdout might be
justified in outputting error messages to stderr.
*Might* be?
Programs that generate their output to a file, as this one attempts to
do, should indeed generate their error messages to stdout. And that
specifically includes compilers.
I disagree (as you've probably guessed by now).

Displaying error messages is exactly what stderr is for. I fail to
see the point of writing them to stdout just because a program's
primary output happens to go to a file.

I've just tried 4 different Unix-based C compilers, and they all write
their error messages to stderr. Why exactly is this a problem? (Note
that if they were changed to write error messages to stdout, I know of
some tools that would break.)

I'm assuming here that your environment lets you manage stdout and
stderr reasonably, for example directing them both to the same
destination. If not, that's a problem with your environment, not with
the compiler.

I suppose this discussion of compiler behavior is strictly off-topic,
since a C compiler needn't be written in C, and may not even have the
concepts of stdout and stderr. But the discussion started with an
ordinary C program writing an error message to stderr, and the same
considerations apply.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
May 27 '07 #22

"Jack Klein" <ja*******@spamcop.netwrote in message
news:nu********************************@4ax.com...
On Sat, 26 May 2007 13:37:38 +0200, "Army1987" <pl********@for.it>
wrote in comp.lang.c:
>>
"Umesh" <fr****************@gmail.comha scritto nel messaggio
news:11**********************@g37g2000prf.googleg roups.com...
/* This is what I can do. If the length of the string starting with
abc & ending with xyz is known(11 in this case), I can program it as
follows. But if the length of the string varies between 10 to 50* what
should I do? Thanks. /
No, it doesn't. "abc45678xyz" doesn't work.
You are only comparing the string with "abc xyz".
>
//abc?????xyz
#include<stdio.h>
#include<stdlib.h>
int main()
{

FILE *f,*fp;
f=fopen("c:/1.txt","r");
if(f==NULL)
{
puts("Error opening file");
Write that to stderr, not to stdout...

No, no, no, NO, NO!!!

The automatic assumption that all programs should write error messages
to stderr is one of the causes of a lot of grief by inconsiderate
programmers.

Programs that generate their primary output to stdout might be
justified in outputting error messages to stderr.

Programs that generate their output to a file, as this one attempts to
do, should indeed generate their error messages to stdout. And that
specifically includes compilers.
I am writing a lot of short utilties at the moment.
I am not sure whether they are easier to use if they send their output to
stdout or to a named file passed as a parameter. So of course all the
functions are written to take a file pointer, and I can toggle between the
two with a couple of lines in main.

One good question is "what is an error?". For instance if the user just
types the name of the program, traditionally that prints out a usage
message. I always exit with EXIT_FAILURE. The problem is that it is not
aways easy to catch that, say from Perl scripts, and you don't necessarily
want the usage message beign treated as a vaild output.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

May 28 '07 #23
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;

}

May 29 '07 #24
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;

}

May 29 '07 #25
Umesh wrote On 05/29/07 17:45,:
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;

}
What you should do is learn what { and } do, and
how they control what an `if' governs.

You might also want to brush up on what fgets()
does: What you have is all right, but suggests that
you don't know why.

Also, there will be trouble if either of the fopen()
calls should fail. Obey the Sixth Commandment!

http://www.lysator.liu.se/c/ten-commandments.html

--
Er*********@sun.com
May 29 '07 #26
Umesh <fr****************@gmail.comwrites:
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) ks***@mib.org <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"
May 29 '07 #27
So the simplified program for my understanding is as follows:
But it returns words/strings containing '.' (full stop), ',' (comma)
etc which is not intended at all. How to prevent it from doing so?

// find a string stating with abc and ending with xyz WORKING

#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;

}
Keith Thompson wrote:
Umesh <fr****************@gmail.comwrites:
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) ks***@mib.org <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"
May 30 '07 #28
To be honest I didn't understand the program logic espectially the
meaning of this line:
if (xyz != NULL)
fprintf (outfp,"%.*s\n",(int)(xyz + 3 - abc), abc);
Umesh wrote:
So the simplified program for my understanding is as follows:
But it returns words/strings containing '.' (full stop), ',' (comma)
etc which is not intended at all. How to prevent it from doing so?

// find a string stating with abc and ending with xyz WORKING

#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;

}
Keith Thompson wrote:
Umesh <fr****************@gmail.comwrites:
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) ks***@mib.org <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"
May 30 '07 #29
Umesh <fr****************@gmail.comwrites:
To be honest I didn't understand the program logic espectially the
meaning of this line:
if (xyz != NULL)
fprintf (outfp,"%.*s\n",(int)(xyz + 3 - abc), abc);
[snip]

Stop top-posting. It's rude.

Read the following:

http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
May 30 '07 #30
On Sun, May 27, 2007 at 08:05:16AM +0000, Walter Roberson wrote:
In article <11**********************@d30g2000prg.googlegroups .com>,
Umesh <fr****************@gmail.comwrote:
I actually want to find words starting with a and ending with b in a
text file and put the output in a file. So there will be no spaces
between the words. .

All of these text searches you have asked about can be solved
by a simple technique called a "state machine".

set state to 0
while (inchar = getc()) != EOF { /* beginning of a line */
if machinestate is 0 {
[snip]
All is vanity. -- Ecclesiastes
That's some mighty fine help you've got there. Unfortunately, a state
machine in this instance is a little more like a solution looking for a
problem. I'd suggest you have your man advance the ignition timing about
..3 metric smidgeons before he makes another post like this.

If the OP is using something UNIX-like, a better solution uses strstr() or
strchr() (or index()) twice. Of course there are special cases where a
custom algorigthm is more appropriate, but the apparent sophistication of
the original questioner suggests that the generic c-libary solution is
best.
Regards,

Steve

May 30 '07 #31
On Sun, May 27, 2007 at 05:14:01AM -0700, osmium wrote:
"Walter Roberson" wrote:
In article <11**********************@d30g2000prg.googlegroups .com>,
Umesh <fr****************@gmail.comwrote:
>I actually want to find words starting with a and ending with b in a
text file and put the output in a file. So there will be no spaces
between the words. .
All of these text searches you have asked about can be solved
by a simple technique called a "state machine".

Yes. But the OP still doesn't understand something as fundamental as
detecting EOF.
That's so understandable: EOF is such a subjective thing. Who among us
is so wise as to be able to say where a file begins and where it ends?

When you factor in the dynamic nature of 'files' in a complex multi-user
system, it is often difficult to be assured -- as a programmer -- that the
file one is reading and writing is actually a static object. In my
experience, the file must be considered something of a moving target and
the software written to account for this fact.
This despite the fact that he has been posting since December 2006.
Yet after all this time you still listen and then post replies.
Regards,

Steve

May 31 '07 #32
How to modify it so that it stops searcing for strings containing
'uvw' ?
>
// find a string stating with abc and ending with xyz WORKING

#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;

}
Keith Thompson wrote:
Umesh <fr****************@gmail.comwrites:
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) ks***@mib.org <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"
Jun 3 '07 #33
On Sun, 03 Jun 2007 03:49:06 -0700, Umesh
<fr****************@gmail.comwrote:
>How to modify it so that it stops searcing for strings containing
'uvw' ?
snip 150 lines

While not wishing to respond to the troll, I think it appropriate to
warn new students of the language not to use his code as an example of
working code or good programming practice or even usenet etiquette.
Remove del for email
Jun 3 '07 #34
In article <11*********************@i38g2000prf.googlegroups. com>,
Umesh <fr****************@gmail.comwrote:
>// find a string stating with abc and ending with xyz WORKING
> abc = strstr(buf, "abc");
if (abc != NULL)
{
xyz = strstr(abc + 3, "xyz");
if (xyz != NULL)
fprintf (outfp,"%.*s\n",(int)(xyz + 3 - abc), abc);
}
That doesn't find strings beginning with abc and ending with xyz:
that finds strings beginning with abc and having xyz in them anywhere,
and prints out the portion from the abc to the xyz.

If the goal is to extract from abc up to the first xyz, then the
phrase to use is "find substrings starting with abc and ending with xyz".
When you say that you want to "find strings [...] ending with xyz", then
the implication is that you are examining a complete string and the
last thing in that string is xyz.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Jun 3 '07 #35

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
5046
by: Jim Lewis | last post by:
Anyone have experience with string pattern matching? I need a fast way to match variables to strings. Example: string - variables ============ abcaaab - xyz abca - xy eeabcac - vxw x...
7
49816
by: 一首诗 | last post by:
Is there any simple way to solve this problem?
17
2873
by: Umesh | last post by:
Can anyone do it? ARMY1987- what say?
1
2525
by: brahatha | last post by:
I am trying to come up with a Java code where I can read a xml file and extract particular element/attribute. This is the structure of xml I have <RootElement> <colAttempt> <Attempt1 a="1"...
4
2604
by: Horacius ReX | last post by:
Hi, I have to read some data from a file, and on each block it always appears the followng string; xyz.vs.1-81_1 . It appears a lot of time with different numbers like; xyz.vs.1-81_1...
8
2424
by: Girish | last post by:
I have a string a = "".. I would like to convert it to a list with elements 'xyz' and 'abc'. Is there any simple solution for this?? Thanks for the help...
0
7099
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4864
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.