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.

Eugeniu Rosca
  • 5,177
  • 16
  • 45
jana S
  • 13
  • 2
  • 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. – Some programmer dude Jul 09 '15 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? – Some programmer dude Jul 09 '15 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? – Some programmer dude Jul 09 '15 at 10:40

3 Answers3

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*)

Community
  • 1
  • 1
Neil
  • 1,036
  • 9
  • 18
  • memcpy should be used only if you're dealing with binary data. – Shlomi Agiv Jul 09 '15 at 10:46
  • 1
    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. – Neil Jul 09 '15 at 10:48
  • since he copied the text from the file to the buffer using strcpy, it has to be a text buffer – Shlomi Agiv Jul 09 '15 at 10:51
  • 1
    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. – Neil Jul 09 '15 at 10:57
  • 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 – Shlomi Agiv Jul 09 '15 at 11:15
  • That I can understand Shlomi, thanks. I will grin and mention that memcpy is included in string.h - I noted this discussion (http://stackoverflow.com/questions/9782126/why-memory-functions-such-as-memset-memchr-are-in-string-h-but-not-in-stdli), it's all food for thought! :-) – Neil Jul 09 '15 at 11:22
  • @ShlomiAgiv That's a bunch of nonsense! strncpy() should never be used at all, because it is dangerous and fails to null terminate strings, _because C programmers simply can't learn how to use it correctly_. Furthermore, [strncpy was never intended to be a safe version of strcpy](http://stackoverflow.com/questions/2114896/why-is-strlcpy-and-strlcat-considered-to-be-insecure). While this answer with memcpy() is correct and safe. – Lundin Jul 09 '15 at 13:16
  • fine, don't use strncpy. Use only memory functions, and always null terminate. – Shlomi Agiv Jul 12 '15 at 05:59
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;
}   
  • 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 – Neil Jul 09 '15 at 11:05
  • 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. – Rohit Daundkar Jul 09 '15 at 11:49
  • 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 Jul 09 '15 at 13:19
  • 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. – Rohit Daundkar Jul 10 '15 at 03:56
0
char out[4] = {0};
strncpy(out, buf+17, 3);
Shlomi Agiv
  • 1,183
  • 7
  • 17
  • 1
    Note that `strncpy` might not terminate the destination string, and in this case it won't. – Some programmer dude Jul 09 '15 at 10:41
  • 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. – Lundin Jul 09 '15 at 13:12
  • 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. – Shlomi Agiv Jul 12 '15 at 06:01