WILDCARD: output all a* by searching a text file

U

ume$h

/*program to search a* in a text file & write output in a file.*
indicated any character. IT IS WORKING BUT HOW TO GENERALISE IT FOR A
LONG STRING
LIKE umesh*** OR Suppose I want to find all words in a text file which
starts with 'a'
and ends with 'z' i.e a*z
where * denotes a string of characters. How can I do it? */

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
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,ch;
while((c=getc(f))!=EOF && (ch=getc(f))!=EOF )
{
if(c=='a'&& ch!=' ')
fprintf(fp,"%c%c\n",c,ch);

}


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

}


/* INPUT
abc
abc
abd
ap
OUTPUT
ab
ab
ab
ap
*/
 
I

Ian Collins

ume$h said:
/*program to search a* in a text file & write output in a file.*
indicated any character. IT IS WORKING BUT HOW TO GENERALISE IT FOR A
LONG STRING
LIKE umesh*** OR Suppose I want to find all words in a text file which
starts with 'a'
and ends with 'z' i.e a*z
where * denotes a string of characters. How can I do it? */
Read up on regular expressions and find a regex library. Boost.Regex is
a good place to start.
 
U

ume$h

/*thx,this one works but how to prevent it from giving duplicate
results? e.g. i don't want 'ab' more than once as output.*/

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
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[3];
while((c[0]=getc(f))!=EOF)
if(c[0]=='a' && (c[1]=getc(f))!=EOF && c[1]!=' ')
{
c[2]='\0';
fprintf(fp,"%s\n",c);
}
fclose(f);
fclose(fp);
return 0;
}
 
D

Default User

ume$h said:
/*program to search a* in a text file & write output in a file.*

I see you still ignore all the advice you got on comp.lang.c, and you
have morphed your ID. I smell troll now.

*plonk* again




Brian
 
I

Ian Collins

ume$h said:
/*thx,this one works but how to prevent it from giving duplicate
results? e.g. i don't want 'ab' more than once as output.*/
Retain context. Head advice. Confine trolling to one NG!
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top