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!