| | |
Output in Text file-How to apply fprintf()?
![]() |
•
•
Join Date: Jul 2004
Posts: 5
Reputation:
Solved Threads: 0
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;
}
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;
}
You might try something like this. Some reference to the functions would be helpful.
#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; }
"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
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
Here's the link:
http://www.daniweb.com/techtalkforums/thread6542.html
•
•
Join Date: Jul 2004
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Dave Sinkula
You might try something like this.Some reference to the functions would be helpful.#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; }
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"); "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
•
•
Join Date: Jul 2004
Posts: 5
Reputation:
Solved Threads: 0
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??
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
•
•
•
•
Originally Posted by KSGuan
how can we check wheather the user key in is numerical or alphabetical??
can we change the alphabatical to numerical??
•
•
•
•
Originally Posted by KSGuan
and if i want the user to put the name of the file can i put like this...
"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
![]() |
Similar Threads
- Writing my program output to a text file. (Java)
- I need a simple tile based map editor, that will output to a text file. (Game Development)
- Output in the text file (C)
Other Threads in the C Forum
- Previous Thread: 2d array
- Next Thread: shortest path length problem
| Thread Tools | Search this Thread |
adobe ansi api array asterisks binarysearch calculate centimeter char character convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax directory feet fflush file floatingpointvalidation fork forloop frequency givemetehcodez global grade gtkgcurlcompiling hacking highest homework i/o inches infiniteloop interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. match meter microsoft mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv recvblocked repetition research scanf scheduling segmentationfault send sequential single socket socketprograming socketprogramming stack standard string suggestions systemcall threads turboc unix urboc user variable voidmain() wab win32api windows.h






