Thread: Searching Into Files

  1. #1
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127

    Question Searching Into Files

    Hy all
    this is my first real app
    and i have a problems in file searching
    this is the source code for my program
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    /********************************************************************************/
    #define defal "Adrees.txt"
    #define setting "Setting.txt"
    #define Addres "Address:"
    #define first_name "First_Name:"
    #define last_name "Last_name:"
    #define middel_name "Middel Name:"
    #define nick_name "Nick Name:"
    #define phone_number "Phone Number:"
    #define New  "\n"
    #define line "=========================================================="
    #define html "Adress.html"
    /**********************************************************************************/
    main()
    {
          int ch;
          FILE *file,*htt,*fg,*cp;
          struct con
          {
                 char first_namel[40];
                 char last_namel[40];
                 char middel_namel[40];
                 char nick_namel[40];
                 char phone_numberl[20];
                 char addressl[20];
                 }data;
                 char read[50],read2[50],newname[80];
                 char ADDRESS[80],cplist[80],buffer[80];
                 printf("=====================================\n");
          printf("Welcome To koofar's maka Addres Book\n");
          printf("By Storm MaN \"Gr8ts To Linuxer AS strikerX ,sAFA7_eLNet,Ray,Hacko,Security Gurus All,Linuxac jsut the good people\n\"");
          printf("Chosse Between The Given List\n");
          printf("================================\n");
          printf("Loding setting");
          fg=fopen(setting,"ab");
          fclose(fg);
          fg=fopen(setting,"rb");
          fscanf(fg,"%s",ADDRESS);
          fclose(fg);
          printf("\n1>Add Contact\n2>See All Contacts\n3>Delete All Contacts\n4>Dumb to Html\n5>Reanme contact list\n6>copy contact list\n7>Search cnotatct\n8>How Many Conatcs\n10>exit\n:");
          fscanf(stdin,"%d",&ch);
          switch(ch)
          {
          case 1:
               getchar();
               fflush(stdin);
               fflush(stdin);
               printf("\nAdding Contact");
               printf("\nfirst_name:");
               gets(data.first_namel);
               printf("\nLast_Name:");
               gets(data.last_namel);
               printf("\nMiddel name:");
               gets(data.middel_namel);
               printf("\nNick Name:");
               gets(data.nick_namel);
               printf("\nphone number:");
               gets(data.phone_numberl);
               printf("\nAddress:");
               gets(data.addressl);
               fflush(stdin);
               file=fopen(ADDRESS,"ab");
               if(file != NULL)
               {
               if(!feof(file))
               {
               fputs(first_name,file);
               fputs(data.first_namel,file);
               fputs(New,file);
               fputs(last_name,file);
               fputs(data.last_namel,file);
               fputs(New,file);
               fputs(middel_name,file);
               fputs(data.middel_namel,file);
               fputs(New,file);
               fputs(nick_name,file);
               fputs(data.nick_namel,file);
               fputs(New,file);
               fputs(phone_number,file);
               fputs(data.phone_numberl,file);
               fputs(New,file);
               fputs(Addres,file);
               fputs(data.addressl,file);
               fputs(New,file);
               fputs(line,file);
               fputs(New,file);
               rewind(file);
               }
               }
               fclose(file);
               sleep(1500);
               printf("\nPress enter To exit.......");
               fflush(stdin);
               getchar();
               fflush(stdin);
               break;
               case 2:
              fflush(stdin);
              printf("\nSeeing All Contact:\n");
               file=fopen(ADDRESS,"rb");
               fflush(stdout);/*cannot use system cls cause we are not dealing with a defined opreting system*/
               while(!feof(file))
               {
               while((fgets(read,50,file))!=NULL)
               printf("%s",read);
               printf("\n");
               }
               fclose(file);
               fflush(stdin);
               fflush(stdout);
               printf("\nPress enter To exit.......");
               fflush(stdin);
               getchar();
               fflush(stdin);
               sleep(1500);
               break;
               case 3:
               fflush(stdin);
               printf("\nBy By contacts");
               remove(ADDRESS);
               file=fopen(ADDRESS,"wb");
               fprintf(file,"no cnotacts");
               fclose(file);
               sleep(1500);
               printf("\nPress enter To exit.......");
               fflush(stdin);
               getchar();
               fflush(stdin);
               break;
               case 4:
               fflush(stdin);
               printf("\nMakking it html OK\n");
               file=fopen(ADDRESS,"rb");
               htt=fopen(html,"ab");
               fputs("<html><center>",htt);
               while((fgets(read2,50,file))!=NULL)
               {
               fputs(read2,htt);
               fputs("<br>",htt);
               }
               fputs("</html></center>",htt);
               fclose(file);
               fclose(htt);
               sleep(1500);
               printf("\nPress enter To exit.......");
               fflush(stdin);
               getchar();
               fflush(stdin);
               break;
               case 5:
               fflush(stdin);
               printf("\nEnter The New Name:");
               gets(newname);
               if((rename(ADDRESS,newname)) == 0)
               {
               printf("\nFile hase been renamed to%s",newname);
               fg=fopen(setting,"wb");
               rewind(fg);
               fwrite(newname,sizeof(char),strlen(newname),fg);
               fclose(fg);
               }
               else
               {
                   printf("\nCannot rename file name");
                   }
                   break;
                   case 6:
                   fflush(stdin);
                   printf("\nEnter a Path For The New Contact List:");
                   gets(cplist);
                   file=fopen(ADDRESS,"rb");
                   cp=fopen(cplist,"wb");
                   while((fgets(buffer,80,file))!= NULL)
                   {
                    fputs(buffer,cp);
                    }
                    if(cp)
                    {
                   printf("File IS In %s",cplist);
                    }
                    else
                    {
                        fprintf(stderr,"Error");
                        }
                    break;
               case 10:
               printf("\nBye Bye");
               sleep(1500);
               exit(0);
               }   
          return 0;
    }
    /* there has to be a  file named Setting.txt in the Directory the program in*/
    Now The Problem is coming
    i can't search into the file
    this is a sample code for searching and it's perfectly failed
    this is the code
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    main()
    {
     FILE *fp;
     char se[80];
     char buffer[80];
     gets(se);
     fp=fopen("file.txt","rb");
     do
     {
         fgets(buffer,strlen(se)+1,fp);
         printf("\n%s\n%s",buffer,se);
     if(strcmp(buffer,se)==0)
     {
     printf("%sfound\n",buffer);
     } 
     }while(fp!=NULL);
     fclose(fp);              
    }
    i will explain it
    PHP Code:
    fgets(buffer,strlen(se)+1,fp); 
    here i get a string long as the inputed string bu user to search into
    PHP Code:
    if(strcmp(buffer,se)==0
    compare it
    the print it
    actually it works but not as i want it to work
    it print all the file content and then exit
    so the problem is that i can't search into files
    help

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Don't use fflush(stdin); read the FAQ.
    Also indent your code!

    Code:
    fgets(buffer,strlen(se)+1,fp);
    You want, sizeof(buffer) not strlen(se)+1
    Last edited by zacs7; 04-24-2007 at 06:03 AM.

  3. #3
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    tanks but it still as it

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You did write it yes...?

    Also you know that strcmp() is case-sensitive?

  5. #5
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Also you know that strcmp() is case-sensitive?
    you didn't get what i mean
    so i will make a example
    how does grep unix command works ?
    i mean i do this with grep
    Code:
    grep -r 'storm man' /*
    how does grep get this all information from the file?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > gets(data.addressl);
    > fflush(stdin);
    > if(!feof(file))
    There are FAQ entries on all of these.

    > how does grep get this all information from the file?
    For a start, it declares
    int main ( int argc, char *argv[ ] )

    It then goes and reads the FAQ to find out how to process command line arguments.

    Maybe something like
    Code:
    void searchFile ( const char *filename ) {
      char buff[BUFSIZ];
      FILE *fp = fopen( filename, "r" );
      if ( fp != NULL ) {
        while ( fgets( buff, sizeof buff, fp ) != NULL ) {
          // strcmp, strstr, any other search you care to implement
        }
        fclose( fp );
      }
    }
    
    int main ( int argc, char *argv[] ) {
      for ( i = 1 ; i < argc ; i++ ) {
        searchFile( argv[i] );
      }
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Quote Originally Posted by Salem View Post
    > gets(data.addressl);
    > fflush(stdin);
    > if(!feof(file))
    There are FAQ entries on all of these.

    > how does grep get this all information from the file?
    For a start, it declares
    int main ( int argc, char *argv[ ] )

    It then goes and reads the FAQ to find out how to process command line arguments.

    Maybe something like
    Code:
    void searchFile ( const char *filename ) {
      char buff[BUFSIZ];
      FILE *fp = fopen( filename, "r" );
      if ( fp != NULL ) {
        while ( fgets( buff, sizeof buff, fp ) != NULL ) {
          // strcmp, strstr, any other search you care to implement
        }
        fclose( fp );
      }
    }
    
    int main ( int argc, char *argv[] ) {
      for ( i = 1 ; i < argc ; i++ ) {
        searchFile( argv[i] );
      }
      return 0;
    }
    sorry for not reading the FAQS i am new here man
    will thanks so much
    i did a small search here and u found what makes my heart dance
    will be be back again for reporting the last news
    thanks again

  8. #8
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    and <Salem>
    >gets(data.addressl);
    i read the faqs is that ok now ?
    __________________________________________
    fgets(data.last_namel,sizeof(data.last_namel),stdi n);|
    __________________________________________
    thanks again

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    fgets( data.last_namel, sizeof( data.last_namel ), stdin );
    ya that looks alright
    ssharish2005

  10. #10
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Code:
    /* win grep.c 'example'*/ 
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define BUF 255
    main()
    {
          FILE *fp;
          int z=0;
          char source[40];
          char dest[40];
          char buffer[BUF];
          char *loc;
          printf("Hy Enter A String:\n");
          fgets(source,sizeof(source),stdin);
          fp=fopen("storm.txt","rb");
          if(fp==NULL)
          {
          fprintf(stderr,"Cannot open file");
          }
          else
          {
          while((fgets(buffer,BUF,fp))!=NULL)
          {
          if((loc=strstr(buffer,source)) == NULL)
          {
          z++;
          }
          else
          {
              printf("The Word &#37;s Found On %d",source,loc-buffer);
              printf("\n%d",ftell(fp));
              }
              }
              }
              if(z>0)
              {
              fprintf(stderr,"\nCannot Find String In This File\n");
             z=0;
             }
              fclose(fp);
              }
    here is my code
    and it's not working
    what i am doing wrong now ?
    thanks in advance

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Main returns an integer to the OS.
    ( What compiler are you using? None of mine will let me write main() with no int return type )

    Also, indent your code better, I find that very hard to follow.
    Double Helix STL

  12. #12
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    ok i will thanks

    i am using dev-cpp
    this is another code
    it works but not fine
    Code:
    #include<stdio.h>
    
    #define BUF 250
    
    main()
    
    {
         
     int x,z,g,f;
         
     FILE *fp;
        
      char buffer[BUF],dest[80],guffer[BUF];
        
      g=x=f=0;
        
      fp=fopen("storm.txt","rb");
         
    
     fgets(dest,sizeof(dest),stdin);
          
    while((fgets(buffer,BUF,fp))!=NULL)
          
    {
       
       printf("&#37;s\n",buffer);
         
     for(x=0;x<strlen(buffer);x++)
        
      {
        
      f++;
         
     strcpy(guffer,buffer);
         
     if(guffer[x] == dest[x])
        
      {
         
     g++;
         
     }
         
      if(g == strlen(guffer))
         
     {
              
     printf("string you entrid is found on line %d\n",f);
              
     }
         
     }
          
    }
          
          
          fclose(fp);
               }
    the file i am searching from content's is
    storm ahmed ahm
    storm ahmed ahm
    storm ahmed ahm
    storm ahmed ahm
    storm ahmed ahm
    storm ahmed ahm
    storm ahmed ahm
    storm ahmed ahm
    storm ahmed ahm
    storm ahmed ahm
    storm ahmed ahm
    storm ahmed ahm
    even it's not 53 lines
    when i put storm
    it do this
    storm ahmed ahm


    storm ahmed ahm


    storm ahmed ahm


    storm ahmed ahm


    string you entrid is found on line 53
    storm ahmed ahm


    storm ahmed ahm


    storm ahmed ahm


    storm ahmed ahm


    storm ahmed ahm


    storm ahmed ahm


    storm ahmed ahm


    storm ahmed ahm

    this is exactly it's out put
    when i put ahm
    it gives me nothing
    what is wrong
    actually i am trying so hard to make it works
    any idea?
    Last edited by St0rM-MaN; 04-26-2007 at 08:53 AM.

  13. #13
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    beacuse u have made a mistake in comparing the string. After u get a line from the file.You will have to check for the whole fine for the key work.

    But instead u check only few starting char which will never reach the end. And more over why do u want to open the file in a read mode when u are trying to read data in text mode. Open that in read mode

    Code:
    #include<stdio.h>
    
    #define BUF 250
    
    int main()
    {
          int x,z,g,f;
          FILE *fp;
          char buffer[BUF],dest[80],guffer[BUF];
          g=x=0;
          int flag=0;
    
          fp=fopen("test.txt","r");
          printf("Enter key\n");
          fgets(dest,sizeof(dest),stdin);
    
          while((fgets(buffer,BUF,fp))!=NULL)
          {
            printf("&#37;s %s\n",buffer, dest);
             
            if(strstr(buffer,dest) != NULL)
            {
                flag = 1;
                break;
            }
            else
                flag = 0;
          }
          
          if(flag)
             printf("String found\n");
          else
              printf("String not found\n"); 
          
               
          fclose(fp);
          getchar();
          return 0;
    }
    
    /* my output
    Enter key
    computer
    hello
     computer
    
    hello there
     computer
    
    hello computer
     computer
    
    String found
    
    */
    i tested on this file

    Code:
    hello 
    hello there
    hello computer
    1, 1, Test, test
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 01:24 PM
  2. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. Searching txt files (another file i/o qn)
    By Catif in forum C++ Programming
    Replies: 9
    Last Post: 05-13-2002, 04:14 PM
  5. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 05:18 PM