On Tue 2008-10-14 08:55:17 UTC-0700, Miguel Caro ([EMAIL PROTECTED]) wrote:
> for example, i have a function who calculate some values, i wish write these
> values in a file, inside a funtcion .
>
> I am trying to do this but shows an error.
You didn't provide the error.
> For example
>
> ....
> #include<iostream>
> #define Pi 3.141516
Use M_PI from <cmath> or <math.h>
> using namespace std;
>
> FILE *f;//where must i define a variable FILE *f? here? or inside a
> funtcion?
Inside the function. That is, unless you want to perform I/O on one file from
multiple functions.
But why are you using C's fopen() in C++? You should probably be
using C++'s fstreams.
> //function
> void suma(int i)
> {
> f=fopen("data.txt", "w+"); /// is it necessary to write f as incomig
> variable? if yes, how to do it?
I don't understand this question.
> float s=0;
> for (int i ; i<10;i++)
> {
> ++s=Pi;
> }
You have already declared a variable named i as a parameter of suma().
The initial value of i is undefined when the loop begins.
What did you expect "++s=Pi;" to do? This is a syntax error.
Where are you writing the data to the file?
> fclose(f);
> }
>
> //main program
>
> int main ()
> {
> ...
> suma(i);
> ...
> }
>
> specifically shows erros inside a function. I think is because the
> function.
What?
> A question: how did you write data into a text file inside a function?
In C you could use fprintf().
In C++, use fstreams:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outf("output.txt");
outf << 7.77 << endl;
outf.close();
return 0;
}
> is my question clear?
Not really.
You asked more than one question.