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

Member Avatar for iamthwee

I think you need to go back to basics.

commented: Completely worthless comment -2

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");
        }
}

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

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

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 !

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.