DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Output in Text file-How to apply fprintf()? (http://www.daniweb.com/forums/thread8390.html)

KSGuan Jul 24th, 2004 3:29 pm
Output in Text file-How to apply fprintf()?
 
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;
}

Dave Sinkula Jul 24th, 2004 8:10 pm
Re: Output in Text file-How to apply fprintf()?
 
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.

FireNet Jul 25th, 2004 3:57 am
Re: Output in Text file-How to apply fprintf()?
 
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

KSGuan Jul 27th, 2004 12:55 pm
Re: Output in Text file-How to apply fprintf()?
 
Quote:

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

Dave Sinkula Jul 27th, 2004 1:23 pm
Re: Output in Text file-How to apply fprintf()?
 
Maybe open the file for append mode (open or create text file for writing at end-of-file).
FILE *file = fopen("file.txt", "a");

KSGuan Jul 27th, 2004 1:50 pm
Re: Output in Text file-How to apply fprintf()?
 
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

Dave Sinkula Jul 27th, 2004 2:36 pm
Re: Output in Text file-How to apply fprintf()?
 
Quote:

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.


Quote:

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).


All times are GMT -4. The time now is 5:10 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC