Output in Text file-How to apply fprintf()?

Reply

Join Date: Jul 2004
Posts: 5
Reputation: KSGuan is an unknown quantity at this point 
Solved Threads: 0
KSGuan KSGuan is offline Offline
Newbie Poster

Output in Text file-How to apply fprintf()?

 
0
  #1
Jul 24th, 2004
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][b]=((temp/100)*p)/(1-(pow((1+(temp/100)),-n)));
total[a][b] = pymt[a][b]*12*n;
printf("%.2f\t\t%.2f\t\t%.2f\t\t\t%d\n",pymt[a][b],total[a][b],temp,n);
b++;
}
a++;
}


return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Output in Text file-How to apply fprintf()?

 
0
  #2
Jul 24th, 2004
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: Output in Text file-How to apply fprintf()?

 
0
  #3
Jul 25th, 2004
Hey go to the tutorials fourm and check out the tut for file i/o using fstream.(C++)

Here's the link:
http://www.daniweb.com/techtalkforums/thread6542.html
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 5
Reputation: KSGuan is an unknown quantity at this point 
Solved Threads: 0
KSGuan KSGuan is offline Offline
Newbie Poster

Re: Output in Text file-How to apply fprintf()?

 
0
  #4
Jul 27th, 2004
Originally Posted by Dave Sinkula
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Output in Text file-How to apply fprintf()?

 
0
  #5
Jul 27th, 2004
Maybe open the file for append mode (open or create text file for writing at end-of-file).
FILE *file = fopen("file.txt", "a");
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 5
Reputation: KSGuan is an unknown quantity at this point 
Solved Threads: 0
KSGuan KSGuan is offline Offline
Newbie Poster

Re: Output in Text file-How to apply fprintf()?

 
0
  #6
Jul 27th, 2004
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,306
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 227
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Output in Text file-How to apply fprintf()?

 
0
  #7
Jul 27th, 2004
Originally Posted by KSGuan
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.


Originally Posted by KSGuan
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).
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC