reading multiple files using fstream in object class
Dec 5, 2012 at 10:54pm UTC
I am trying to open multiple text files using fstream in a class float_seq object that i have created but i don't know how to close the file in order to open the next one.Everything else is fine. definitions in class and main code shown below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
////definition and declaration///////
class float_seq {
public :
float_seq();
~float_seq();
float_seq(const float_seq&);
float_seq operator =(const float_seq&);
float_seq operator +(const float_seq&);
void close();
friend std::istream& operator >>(std::istream&, float_seq&);
friend std::ostream& operator <<(std::ostream&, float_seq&);
float read_from(int element);
void write_to(float value,int element);
void default_zero();
void print();
unsigned int no_of_values;
float *seq;
bool check;
/////////////////////////
float_seq::float_seq():no_of_values(0){
seq= new float [no_of_values];
}
float_seq::~float_seq(){
delete [] seq;
}
float_seq::float_seq(const float_seq& New_float_seq){
no_of_values=New_float_seq.no_of_values;
int i(0);
for (i=0 ; i < no_of_values; ++i)
{
seq[i]=New_float_seq.seq[i];
}
}
std::istream& operator >>(std::istream& fin, float_seq& float__seq){
fin>>float__seq.no_of_values;
for (int i=0; i<float__seq.no_of_values; i++) {
fin>>float__seq.seq[i];
}
return fin;
}
float_seq float_seq::operator =(const float_seq &float__seq){
delete [] seq;
seq = new float [float__seq.no_of_values];
no_of_values=float__seq.no_of_values;
for (int i=0; i<no_of_values; i++) {
seq[i]=float__seq.seq[i];
}
return *this ;
}
float_seq float_seq::operator +(const float_seq &float__seq){
float_seq sum;
sum.no_of_values=no_of_values+float__seq.no_of_values;
for (int i=0; i<no_of_values; i++) {
sum.seq[i]=seq[i];
}
for (int i=0; i<float__seq.no_of_values; i++) {
sum.seq[no_of_values+i]=float__seq.seq[i];
}
return sum;
}
std::ostream& operator <<(std::ostream& float_seqout, float_seq& float__seq){
float_seqout<<float__seq.no_of_values<<std::endl;
for (int i=0; i<float__seq.no_of_values; i++) {
float_seqout<<float__seq.seq[i]<<", " ;
}
return float_seqout;
}
void float_seq::default_zero(){
for (int i=0; i<no_of_values; i++) {
seq[i]=0;
}
}
void float_seq::print(){
int i;
cout<< no_of_values <<endl<<endl;
for (i=0;i<no_of_values;i++)
{
cout<< seq[i]<<endl;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
#include "Coursework.h"
using namespace std;
int main()
{
float_seq signal,signal2, add;
signal.default_zero();
fstream fin("filter1.txt" );
fin>> signal;
/* close file here */
fstream fin("filter2.txt" );
fin>> signal2;
add=signal+signal2;
add.print();
return 0;
}
Dec 7, 2012 at 4:55pm UTC
I think this is what you want.
1 2
/* close file here */
fin.close();
Topic archived. No new replies allowed.