Greeting to all,
I have been trying to apply "fprintf()","fopen()" and "fclose()" into the following source code but still fail to get the right way. As I do not have any C program book to refer to, I really appreciate it if someone could show me how should I go about it. As I have mentioned in my previous thread, I am trying to transfer the output (refer to the source code below) to a text file such as showing the output in the notepad file.

#include <stdio.h>
#include <math.h>


main()
{
double p,min,max,temp;
double pymt[3][100],total[3][100];
int a=0;
int n;
printf ("Enter loan amt(p): ");
scanf ("%lf",&p);
printf ("\nKey in minimun interest rate(i):  ");
scanf ("%lf",&min);
printf ("\nKey in maximun interest rate(i):  ");
scanf ("%lf",&max);
printf("%s\t%s\t%s\t%s\n","Monthly payment","Total payment","Interest rate in %","Year");
for(n=20;n<=30;n+=5){
int b=0;
temp=min;
for(;temp<=max;temp+=0.25){
pymt[a]=((temp/100)*p)/(1-(pow((1+(temp/100)),-n)));
total[a] = pymt[a]*12*n;
printf("%.2f\t\t%.2f\t\t%.2f\t\t\t%d\n",pymt[a],total[a],temp,n);
b++;
}
a++;
}



return 0;
}

You might try something like this.

#include <stdio.h>
#include <math.h>
 
int main()
{
double p,min,max,temp;
double pymt[3][100],total[3][100];
int a=0;
int n;
printf ("Enter loan amt(p): ");
scanf ("%lf",&p);
printf ("\nKey in minimun interest rate(i): ");
scanf ("%lf",&min);
printf ("\nKey in maximun interest rate(i): ");
scanf ("%lf",&max);
printf("%s\t%s\t%s\t%s\n","Monthly payment","Total payment","Interest rate in %","Year");
for ( n=20;n<=30;n+=5 )
{
	 FILE *file = fopen("file.txt", "w");
	 if ( file )
	 {
		 int b=0;
		 temp=min;
		 for ( ;temp<=max;temp+=0.25 )
		 {
			pymt[a][b]=((temp/100)*p)/(1-(pow((1+(temp/100)),-n)));
			total[a][b] = pymt[a][b]*12*n;
			fprintf(file, "%.2f\t\t%.2f\t\t%.2f\t\t\t%d\n",pymt[a][b],total[a][b],temp,n);
			b++;
		 }
		 a++;
		 fclose(file);
	 }
}
 
return 0;
}

Some reference to the functions would be helpful.

You might try something like this.

#include <stdio.h>
#include <math.h>
 
int main()
{
double p,min,max,temp;
double pymt[3][100],total[3][100];
int a=0;
int n;
printf ("Enter loan amt(p): ");
scanf ("%lf",&p);
printf ("\nKey in minimun interest rate(i): ");
scanf ("%lf",&min);
printf ("\nKey in maximun interest rate(i): ");
scanf ("%lf",&max);
printf("%s\t%s\t%s\t%s\n","Monthly payment","Total payment","Interest rate in %","Year");
for ( n=20;n<=30;n+=5 )
{
	 FILE *file = fopen("file.txt", "w");
	 if ( file )
	 {
		 int b=0;
		 temp=min;
		 for ( ;temp<=max;temp+=0.25 )
		 {
			pymt[a][b]=((temp/100)*p)/(1-(pow((1+(temp/100)),-n)));
			total[a][b] = pymt[a][b]*12*n;
			fprintf(file, "%.2f\t\t%.2f\t\t%.2f\t\t\t%d\n",pymt[a][b],total[a][b],temp,n);
			b++;
		 }
		 a++;
		 fclose(file);
	 }
}
 
return 0;
}

Some reference to the functions would be helpful.

i success the fpintf file already.
but i notice that the data of the file is being replace by the new data each time the program is running.
how can it be program so that the new data is continue at the bottom not replace the old data.
thanz

Maybe open the file for append mode (open or create text file for writing at end-of-file).

FILE *file = fopen("file.txt", "a");

Hi Dave Sinkula,

how can we check wheather the user key in is numerical or alphabetical??
can we change the alphabatical to numerical??


and if i want the user to put the name of the file can i put like this...

char n;
printf("\nPut the file name\n");
scanf("%s",&n);

FILE *file = fopen("c:\\"n".txt", "a");

thanks for help

how can we check wheather the user key in is numerical or alphabetical??
can we change the alphabatical to numerical??

Read in text. Then attempt to convert the text to a number. If the conversion fails, you don't have a number.

and if i want the user to put the name of the file can i put like this...

I would recommend using fgets to read the file name into a string. Then use that string as the first parameter to fopen (after stripping any remaining trailing newline character).

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.