WILDCARD: output all a* by searching a text file

Discussion in 'C++' started by ume$h, May 18, 2007.

  1. ume$h

    ume$h Guest

    /*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
    */
    ume$h, May 18, 2007
    #1
    1. Advertising

  2. ume$h

    Ian Collins Guest

    ume$h wrote:
    > /*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.

    --
    Ian Collins.
    Ian Collins, May 18, 2007
    #2
    1. Advertising

  3. ume$h

    ume$h Guest

    /*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;
    }
    ume$h, May 18, 2007
    #3
  4. ume$h

    Default User Guest

    ume$h wrote:

    > /*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
    Default User, May 18, 2007
    #4
  5. ume$h

    Ian Collins Guest

    ume$h wrote:
    > /*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!

    --
    Ian Collins.
    Ian Collins, May 18, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Umesh

    output all a* by searching a text file

    Umesh, May 15, 2007, in forum: C Programming
    Replies:
    19
    Views:
    565
    Keith Thompson
    May 20, 2007
  2. Replies:
    7
    Views:
    831
  3. JSkinn3
    Replies:
    3
    Views:
    744
    Rikishi42
    Mar 19, 2011
  4. drew
    Replies:
    6
    Views:
    174
  5. stumblng.tumblr
    Replies:
    1
    Views:
    198
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page