Hello,

I am trying to generate multiple files out of a single text file by changing one parameter.

My text file is of the form --
p 3 6
n 1 1
n 3 -1
a 1 2 0.8
a 1 3 0.7
e 1 2 1 2 0.7
e 1 2 1 3 0.7
e 1 3 1 2 0.7

I am trying to find combinations of the 2nd element of the highlighted parameters. i.e. the number after n. Also my text file needs to be renamed accordingly. The remaining part except the highlighted remains exactly the same in all the files.

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

int main(int argc, char **argv)
{
FILE *f,*fp;
char *filename = argv[1];
char *new_file, buffer1[100];
char buffer[100];
int i,j,k,count;
int store;


f = fopen (filename, "r");
	count = 0;
	for(i=1; i<=4; i++)
	{
		for(j=1; (j<=4); j++)
		{
			if(i!=j)
			{
				sprintf (buffer1, "%s_%d_%d.txt", filename,i,j);
				fp = fopen (buffer1,"w");
				while ( fgets(buffer, 100, f) != NULL )
				{
					if(buffer[0]!= 'n')
					fputs(buffer,fp);
					else
					{
					if(count == 0)
					{
					fprintf(fp,"a");
					fprintf(fp,"	%d",i);
					fprintf(fp,"	1");
					fprintf(fp,"\n");
					count++;
					}
					else
					{ 	if(count == 1)
							{
							fprintf(fp,"a");
							fprintf(fp,"	%d",j);
							fprintf(fp,"	-1");
							fprintf(fp,"\n");
							}
					}
					}
				if (buffer[0] == ' ')
				break;
				}
			fclose(fp);
			}
		}
	
	}

fclose(f);
//free(fp);
printf("All done\n");
getchar();
return 0;
}

The above code gives me values for only one text file and then goes in to an infinite loop. The other text files are generated though as empty files.

Please help me find or correct where the problem is in the code.

Thanks!

Frankly, I did not get what you are trying to do, but maybe that's just me.

Hello,

The above code gives me values for only one text file and then goes in to an infinite loop. The other text files are generated though as empty files.

Once you completed an inner loop once, you have read everything from the file, that is reached the end of file, that is all subsequent reads do not produce any data. Either fopen and fclose your filename in each iteration, or rewind it (in each iteration, of course).

Once you completed an inner loop once, you have read everything from the file, that is reached the end of file, that is all subsequent reads do not produce any data. Either fopen and fclose your filename in each iteration, or rewind it (in each iteration, of course).

Oh... that was right... Thanks! There is one more problem that the fprintf statements --

if(count == 0)
		{
		fprintf(fp,"a");
		fprintf(fp,"	%d",i);
		fprintf(fp,"	1");
		fprintf(fp,"\n");
		count++;
		}
else
		{ 	if(count == 1)
			{
					fprintf(fp,"a");
					fprintf(fp,"	%d",j);
					fprintf(fp,"	-1");
					fprintf(fp,"\n");
			}
		}

In this part I am trying to write the 'n' s that I mentioned in my post in the new text files that I am creating.

So ideally my text file that has textname_1_3.txt should have
n 1 1
n 3 -1
(and the remaining contents are the same)

Similarly, my textfile with name textname_1_4.txt should have
n 1 1
n 4 -1
(and the remaining contents being the same)

and so on so forth.

Now it is printing out the wrong values---
like
n 2 1
n 2 -1
(remaining content is correct)
and so on.

Could you help me find where the problem is in this case?

Thanks again...

OK, now I get what you doing.
Let me reiterate: use a debugger. It is entertaining and enlightening. You will immediately see how do you actually print. Hint: lines 33 through 37 have only one chance to run.

OK, now I get what you doing.
Let me reiterate: use a debugger. It is entertaining and enlightening. You will immediately see how do you actually print. Hint: lines 33 through 37 have only one chance to run.

Oops... I was just about to reply.... I had used a wrong variable inside my fprintf's!!! That was pretty goofed up!

Thank you so much!

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

int main(int argc, char **argv)
{
FILE *f,*fp;
char *filename = argv[1];
char *new_file, buffer1[1000];
char buffer[1000];
int i,j,k,count;
int store;

    for(i=1; i<=50; i++)
    {
        for(j=1; (j<=50); j++)
        {
            if(i!=j)
            {   
                count = 0;
                f = fopen (filename, "r");
		if ((f = fopen (filename, "r")) == NULL) printf("Source file cannot be opened\n");
                sprintf (buffer1, "%s_%d_%d.txt", filename,i,j);
                fp = fopen (buffer1,"w");
		if ((fp = fopen (buffer1,"w")) == NULL) printf("Destination file cannot be opened\n");
                while ( fgets(buffer, 1000, f) != NULL )
                {   
		     
                    printf("i = %d,j = %d in file  = %s\n",i,j,buffer1);

                    if(buffer[0]!= 'n')
                    fputs(buffer,fp);
                    else
                    {
                    if(count == 0)
                    {
                    fprintf(fp,"n");
                    fprintf(fp,"    %d",i);
                    fprintf(fp,"    1");
                    fprintf(fp,"\n");
                    count++;
                    }
                    else
                    {     if(count == 1)
                            {
                            fprintf(fp,"n");
                            fprintf(fp,"    %d",j);
                            fprintf(fp,"    -1");
                            fprintf(fp,"\n");
                            }
                    }
                    }
                if (buffer[0] == ' ')
                break;
                }
            fclose(fp);
            fclose(f);
            }
        }
   
    }


//free(fp);
printf("All done\n");
return 0;
}

Hello,

I have attached the code which was working until I tried to create 2450 files (inside the multiple for loops). The input data remains in the same format as specified earlier in the problem. I am getting segmentation faults and the "Destination file cannot be opened" beyond a certain point. I am not able to understand why that would happen. I tried to increase the size of the "buffers" but that did not help.

Please let me know where the problem is here....

Thanks in advance....

You open each file twice:

f = fopen (filename, "r");
if ((f = fopen (filename, "r")) == NULL) 
...

As far as I understand it, this is a memory leak because your fclose doesn't actually close both of them. I assume that segmentation faults appear when the number of open files grows larger than the value of FOPEN_MAX, but I cannot find confirmation on the internet.

Also, instead of opening and closing of the source file so many times, use rewind. For such a large number of iterations, that would make a difference. Something like this:

if ((f = fopen (filename, "r")) == NULL) printf("Source file cannot be opened\n");
else
{
    for(i=1; i<=50; i++)
    {
        for(j=1; (j<=50); j++)
        {
            /*things*/
            rewind(f);
        }
    }
    fclose(f);
}

You did not use a debugger.

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.