1

I just want to extract the particular word from the string.
My program is:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 100

int main() {

    FILE *f;
    char buffer[100];
    char buf[100];
    int count=0;
    char res[100];


  f=fopen("1JAC.pdb","rb");

while(fgets(buffer,BUFFER_SIZE,f))
{

if(strncmp(buffer,"ATOM",4)==0 && strncmp(buffer+13,"CA",2)==0 && strncmp(buffer+21,"A",1)==0)
{
strcpy(buf,buffer);

}

printf (buf);

Output of the program is ATOM 1033 CA LEU A 133 33.480 94.428 72.166 1.00 16.93 C

I just want to extract the word "LEU" using substring. I tried something like this:

Substring(17,3,buf);

But it doesn't work...
Could someone please tell about the substring in C.

3
  • 1
    Do not use string-reading function like fgets for binary files. Either the file is binary, in which case you should use the proper functions to read it, or the file is a text-file in which case you should open it as such. It's doubly problematic here since one of the things that differs between a text-file and a binary file is newline handling, and fgets depends on newlines being handled correctly. Commented Jul 9, 2015 at 10:37
  • Regarding your question, when you say "it doesn't work", can you please elaborate on that? What do you mean by "it doesn't work"? And what is the Substring function you reference? Commented Jul 9, 2015 at 10:39
  • 1
    Oh, and don't use input from anywhere as a format string to printf. Think about what would happen if there was printf formatting codes in the input you read? Commented Jul 9, 2015 at 10:40

3 Answers 3

1

Memcpy seems to be best way to do this ...

memcpy( destBuff, sourceBuff + 17, 3 );
destBuff[ 3 ] = '\0'; 

Please remember to add the null terminators if needed (as I have done in the example).

Also this has been answered before, several times on Stack-overflow

(Get a substring of a char*)

Sign up to request clarification or add additional context in comments.

8 Comments

memcpy should be used only if you're dealing with binary data.
What are we dealing with here? It's all binary data when copying a part of string to another. I disagree with your comment Shlomi. As long as the coder takes into account the size of the chars.
since he copied the text from the file to the buffer using strcpy, it has to be a text buffer
We are dealing with C here - what is the difference with a TEXT buffer and a BINARY buffer?? Nothing - they are all bytes, bit patterns, binary. I really do not get what you are attempting to put across here Shlomi.
I consider it bad practice to mix string functions with binary functions unless required. You're right that in the current situation There is no difference
|
1
//Use the following substring function,it will help you.
int main(int argc, char *argv[])
{

FILE *filepointer;
char string[1700];


filepointer=fopen("agg.txt", "r");

if (filepointer==NULL) 
{
        printf("Could not open data.txt!\n");
    return 1; 
}

while (fgets(string, sizeof(string), filepointer) != NULL) 
{   
    char* temp=substring(string,17,3);/*here 17 is the start position and 3 is the length of the string to be extracted*/ 
}

return 0;   
}




char *substring(char *string, int position, int length) 
{
char *pointer;
int c;
pointer = (char*) malloc(length+1);

if (pointer == NULL)
{
   printf("Unable to allocate memory.\n");
   exit(1);
}

 for (c = 0 ; c < length ; c++)
{
   *(pointer+c) = *(string+position-1);      
    string++;   
}

*(pointer+c) = '\0'; 
return pointer;
}   

4 Comments

Good example Rohit, however wouldn't it be better to return NULL on a malloc error and let the user of the function handle the error? I realize this is an example of what to use that you have supplied, just thought I'd mention the error handling, thanks
thanks Neil, yes you are correct if the argument passed is NULL then then there will be the malloc error.It is users responsibility to check for NULL values.
Funny how people insist on using dynamic memory "to save space", and then in the next breath allocate 1700 bytes on the stack without concern...
Lundin it was a good joke but before commenting please ask why have i used 1770 bytes?? thats bcoz the file which i am reading contains FIXED LENGTH 1700 bytes of data on a single line.
0
char out[4] = {0};
strncpy(out, buf+17, 3);

3 Comments

Note that strncpy might not terminate the destination string, and in this case it won't.
Don't use strncpy(), it is a dangerous function, because of this very null termination issue. It is safer (and faster) practice to use memcpy() and manually terminate the string.
You have a point, yet once you've started using string functions, you should stick with it. mixing memcpy with strncmp/fgets can cause a whole new set of problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.