0

Hi,

I want to write a program to split one file into multiple files.
At any line that contains: "# Name:" i start writing to a new file.
My problem is that i want to save each file with a name i extract from the same line.
Example: # Name: C02427.mol TAUTOMER 1 2706

I would like to save this file as C02427_TAUTOMER_1_2706.mol2

Here is part of my code, i am stuck at the part where i put comments:
The problem is that these numbers are variable, so it can be :
# Name: C02427.mol TAUTOMER 1 2706
or
# Name: C02426.mol TAUTOMER 2
or
# Name: C02425.mol TAUTOMER 1 20

while(fgets (cbuff, 80, f1)!= NULL)
{
        if (cbuff[0] == '#' && cbuff[2] == 'N' && cbuff[3] == 'a' && cbuff[4] == 'm' && cbuff[5] == 'e')
        {
                 strncpy (name, cbuff+8, 6);
                 name[7] = '\0';
                 strcpy(filename, name);
                 strcat(filename,"_TAUTOMER_");
                 
                 /*Copy characters from cbuff+28 to end of line and replace spaces with underscore */
                 
                 strcat(filename, ".mol2");
                 fclose(f2);
                 f2 =fopen(filename, "w");
        }

Any help will be greatly appreciated.

Thanks
Manali

5
Contributors
6
Replies
7
Views
9 Years
Discussion Span
Last Post by Narue
Featured Replies
  • 1
    WaltP 2,905   9 Years Ago

    [QUOTE=mjoshi;493267]I tried the following way and it doesnt work:[/quote] [code] if (cbuff[0] == '#' && cbuff[2] == 'N' && cbuff[3] == 'a' && cbuff[4] == 'm' && cbuff[5] == 'e')[/code] Instead, how about [code]if (strncmp(cbuff, "# Name", 6)[/code] Next: [code] strncpy (name, cbuff+8, 6); name[6] = '\0'; strcpy(filename, name); strcat(filename,"_TAUTOMER_"); … Read More

0

I tried the following way and it doesnt work:
Any help ?

while(fgets (cbuff, 80, f1)!= NULL)
{
        if (cbuff[0] == '#' && cbuff[2] == 'N' && cbuff[3] == 'a' && cbuff[4] == 'm' && cbuff[5] == 'e')
        {
                 strncpy (name, cbuff+8, 6);
                 name[6] = '\0';
                 strcpy(filename, name);
                 strcat(filename,"_TAUTOMER_");
                 j =17;
                        for(i=28; cbuff[i] !='\0'; i++)
                         {
                                if(isspace(cbuff[i]))
                                {
                                strcat(filename, "_");
                                }
                                else
                                {
                                filename[j]= cbuff[i];
                                printf("%s", filename);
                                j++;
                                }
                         }
                 strcat(filename, ".mol2");
        }
}
1

I tried the following way and it doesnt work:

if (cbuff[0] == '#' && cbuff[2] == 'N' && cbuff[3] == 'a' && cbuff[4] == 'm' && cbuff[5] == 'e')

Instead, how about

if (strncmp(cbuff, "# Name", 6)

Next:

strncpy (name, cbuff+8, 6);
name[6] = '\0';
strcpy(filename, name);
strcat(filename,"_TAUTOMER_");

Simplify:

strncpy (filename, cbuff+8, 6);        // Copy the first part of the name
strcpy(&filename[6], "_TAUTOMER_");    // load constant into place

And for the rest, you're close:

fidx = strlen(filename)    // get the number of characters in filename
while(cbuff[i] !='\0')     // loop until you get to the end
{
    if (isspace(chbuff[i]))
    {
        filename[fidx++] = '_';    // load an underscore for a space
    }
    else    // the character is not a space
    {
        filename[fidx++] = chbuff[i];  // load the character
    }
    i++;           // next character
}
filename[fidx++] = '\0';     // make the filename a string
strcat(filename, ".mol2")    // finish off the filename

This might be closer

0

Here is a sample code which u could use or perhaps u can get an idea on how to use sscanf function to parse the string.

#include <stdio.h>

int main()
{
    char str[] = "# Name: C02427.mol TAUTOMER 1 2706";
    char str1[20], str2[20], str3[20];
    int D, D1;
    char FileName[35];
    
    
   if( strncmp( str, "# Name", 6) == 0)
   {
       sscanf(str, "%*[^:]: %[^.].mol %s %d %d", str1, str2, &D, &D1);
       sprintf(FileName, "%s_%s_%d_%d.mol", str1, str2, D, D1);
       printf("Filename -> %s", FileName);
   }
   
   getchar();
   return 0;
}

/* my output
Filename -> C02427_TAUTOMER_1_2706.mol
*/

Hope that helps

ssharish

0

Thanks for the reply WaltP & ssharish2005.

WaltP, there is something funny going on with using

while(cbuff[i] !='\0')

I noticed that if there are 28 characters in the cbuff, the loop will still go one extra time !!
The 29th is supposed to be '\0'. I dont understand that behaviour.

ssharish

the sscanf worked well, i didnt think of it !

0

>I dont understand that behaviour.
You're using fgets, yes? Don't forget that fgets also appends '\n' unless the buffer is filled completely.

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.