Fibonacci Printer Program won't print to file correctly

P

Protoman

Here's a program I wrote that calcs the fibonacci numbers and writes
them to a file, from 1-50.
It prints to the screen just fine, but it only prints the last number,
Fib(50) to the file.

Code:

//FIB.hpp

#pragma once
#include <fstream>
using namespace std;

template<long long N>
class FIB
{
public:
static const long long RET=FIB<N-1>::RET+FIB<N-2>::RET;
};

template<>
class FIB<1>
{
public:
static const long long RET=1;
};

template<>
class FIB<0>
{
public:
static const long long RET=0;
};

template<long long I>
class LOOP
{
public:
static void f()
{
static ofstream fout("C:\\FIB.txt");
LOOP<I-1>::f();
fout << "Fib(" << I << ")=" << FIB<I>::RET << endl;
cout << "Fib(" << I << ")=" << FIB<I>::RET << endl;
}
};

template<>
class LOOP<0>
{
public:
static void f(){}
};

//main.cpp

#include <iostream>
#include <cstdlib>
#include "FIB.hpp"
using namespace std;

int main()
{
LOOP<50>::f();
system("PAUSE");
return EXIT_SUCCESS;
}

Could you help me figure out what's wrong? Thanks!!!!
 
A

Andre Kostur

Here's a program I wrote that calcs the fibonacci numbers and writes
them to a file, from 1-50.
It prints to the screen just fine, but it only prints the last number,
Fib(50) to the file.

Code:

//FIB.hpp

#pragma once
#include <fstream>
using namespace std;

template<long long N>
class FIB
{
public:
static const long long RET=FIB<N-1>::RET+FIB<N-2>::RET;
};

template<>
class FIB<1>
{
public:
static const long long RET=1;
};

template<>
class FIB<0>
{
public:
static const long long RET=0;
};

template<long long I>
class LOOP
{
public:
static void f()
{
static ofstream fout("C:\\FIB.txt");
LOOP<I-1>::f();
fout << "Fib(" << I << ")=" << FIB<I>::RET << endl;
cout << "Fib(" << I << ")=" << FIB<I>::RET << endl;
}
};

template<>
class LOOP<0>
{
public:
static void f(){}
};

//main.cpp

#include <iostream>
#include <cstdlib>
#include "FIB.hpp"
using namespace std;

int main()
{
LOOP<50>::f();
system("PAUSE");
return EXIT_SUCCESS;
}

Could you help me figure out what's wrong? Thanks!!!!

Same problem you had last time. You've got 50 different ofstreams. All
of them opening the same file. Only the last change stays. Same
solution as the last time too.
 
D

Dave Townsend

Protoman said:
Here's a program I wrote that calcs the fibonacci numbers and writes
them to a file, from 1-50.
It prints to the screen just fine, but it only prints the last number,
Fib(50) to the file.

Code:

//FIB.hpp

#pragma once
#include <fstream>
using namespace std;

template<long long N>
class FIB
{
public:
static const long long RET=FIB<N-1>::RET+FIB<N-2>::RET;
};

template<>
class FIB<1>
{
public:
static const long long RET=1;
};

template<>
class FIB<0>
{
public:
static const long long RET=0;
};

template<long long I>
class LOOP
{
public:
static void f()
{
static ofstream fout("C:\\FIB.txt");
LOOP<I-1>::f();
fout << "Fib(" << I << ")=" << FIB<I>::RET << endl;
cout << "Fib(" << I << ")=" << FIB<I>::RET << endl;
}
};

template<>
class LOOP<0>
{
public:
static void f(){}
};

//main.cpp

#include <iostream>
#include <cstdlib>
#include "FIB.hpp"
using namespace std;

int main()
{
LOOP<50>::f();
system("PAUSE");
return EXIT_SUCCESS;
}

Could you help me figure out what's wrong? Thanks!!!!

Just a guess, you are using "static ofstream fout("C:\\FIB.txt");", I think
if you use
just plain cout you will see the entire sequence. I think what is happening
is that you
are constructing a new ofstream as you run each templates code which will
clober what
was in the file and start a new stream. If you want to write to a file,
you will need to open
the stream once and share it with each instance of the template.

dave
 
P

Protoman

Andre said:
Same problem you had last time. You've got 50 different ofstreams. All
of them opening the same file. Only the last change stays. Same
solution as the last time too.

And how do I get there to only be ONE open ofstream? Make fout global?
I really want it class scope, so no one'll mess w/it.
 
I

Ivan Vecerina

:
: Andre Kostur wrote:
: > : >
....
: > > template<long long I>
: > > class LOOP
: > > {
: > > public:
: > > static void f()
: > > {
: > > static ofstream fout("C:\\FIB.txt");
: > > LOOP<I-1>::f();
: > > fout << "Fib(" << I << ")=" << FIB<I>::RET << endl;
: > > cout << "Fib(" << I << ")=" << FIB<I>::RET << endl;
: > > }
: > > };
: > >
: > > template<>
: > > class LOOP<0>
: > > {
: > > public:
: > > static void f(){}
: > > };
: > >
: > > //main.cpp
: > >
: > > #include <iostream>
: > > #include <cstdlib>
: > > #include "FIB.hpp"
: > > using namespace std;
: > >
: > > int main()
: > > {
: > > LOOP<50>::f();
: > > system("PAUSE");
: > > return EXIT_SUCCESS;
: > > }
: > >
: > > Could you help me figure out what's wrong? Thanks!!!!
: >
: > Same problem you had last time. You've got 50 different ofstreams.
All
: > of them opening the same file. Only the last change stays. Same
: > solution as the last time too.
:
: And how do I get there to only be ONE open ofstream? Make fout global?
: I really want it class scope, so no one'll mess w/it.


A clean design would be to open the ofstream in main(),
and to pass it as a parameter to LOOP<N>::f :

static void f( std::eek:stream output )


Ivan
 
M

Martin Vejnar

Protoman said:
Andre said:
Here's a program I wrote that calcs the fibonacci numbers and writes
them to a file, from 1-50.
It prints to the screen just fine, but it only prints the last number,
Fib(50) to the file.

[snipped part of the code]

template<long long I>
class LOOP
{
public:
static void f()
{
static ofstream fout("C:\\FIB.txt");
LOOP<I-1>::f();
fout << "Fib(" << I << ")=" << FIB<I>::RET << endl;
cout << "Fib(" << I << ")=" << FIB<I>::RET << endl;
}
};

[snip]

Could you help me figure out what's wrong? Thanks!!!!
Same problem you had last time. You've got 50 different ofstreams. All
of them opening the same file. Only the last change stays. Same
solution as the last time too.

And how do I get there to only be ONE open ofstream? Make fout global?
I really want it class scope, so no one'll mess w/it.

Why don't you change 'LOOP<T>::f()' to 'LOOP<T>::f(std::eek:stream & out)'
and pass 'out' to 'LOOP<T-1>::f(std::eek:stream &)'?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top