Thread: Grep a string from pdb file

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    Grep a string from pdb file

    Hi,
    Im new to c programming.
    I have one pdb file. i want to extract a string starting with ATOM and store it in a text file.

    here is my program,
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<fcntl.h>
    #define size 50
    main()
    {
    char temp[300],*ptr;
    char sname[50],ws[50],buf[50],buf1[50],field[32];
    FILE *file,*file1;
    int n;
    printf("Enter the Filename\n");
    scanf("%s",sname);
    file=fopen(sname,"r");
    printf("Enter the Word to Search\n");
    scanf("%s",ws);
    file1=fopen("grep.txt","w");
    while(fgets(temp,200,file)!=NULL)
    {
    ptr=strstr(temp,ws);
    if(ptr!=NULL)
    fprintf(file1,"%s",ptr);
    }
    fclose(file);
    strstr function extract the intermediate ATOM and ATOMS too. but i want a string staring with ATOM.

    Thanks in advance..

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Have you tried strnstr()? Then you can restrict the length.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    Quote Originally Posted by Subsonics View Post
    Have you tried strnstr()? Then you can restrict the length.
    when i use strnstr() it gives the following error
    (.text+0xbd): undefined reference to `strnstr'
    collect2: ld returned 1 exit status

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Hm, yeah it's not a standard C function I noticed your fcntl.h and thought it belonged to POSIX but apparently it's a BSD extension. That leaves you to implement a simple parsing function yourself, either as a second step to what strstr catches or make something from scratch that scans the lines from fgets.

    BTW, you should indent the code, that way you might get more responses here.
    Last edited by Subsonics; 04-07-2011 at 01:42 AM.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    Quote Originally Posted by Subsonics View Post
    Hm, yeah it's not a standard C function I noticed your fcntl.h and thought it belonged to POSIX but apparently it's a BSD extension. That leaves you to implement a simple parsing function yourself, either as a second step to what strstr catches or make something from scratch that scans the lines from fgets.

    BTW, you should indent the code, that way you might get more responses here.
    sorry I can't understand yours plz explain it in a clear manner.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    Code:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
      const char filename[] = "3IL8.pdb";
      char temp[300],*ptr;
      FILE *file,*file1;
      file = fopen(filename, "r");
      file1=fopen("grep.txt","w");
      while(fgets(temp,200,file)!=NULL)
      {
        ptr=strstr(temp,"ATOM");
        if(ptr!=NULL)
        fprintf(file1,"%s",ptr);  
      }  
      fclose(file);
    }
    I want the strings starting with "ATOM". but this program output will contain "ATOMS" and "intermediate ATOM" too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM