opening multiple files in C++ using classes

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. 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;
}
Last edited on
i tried that and still got the error message;
'redeclaration of std::fstream fin'
Hi,

The problem is that you redeclared a variable. I overlooked that.
You should rename the second variable fin to fin2 or something like that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{

    float_seq signal,signal2, add;


    signal.default_zero();
    fstream fin("filter1.txt");
    fin>> signal;
    fin.close();
    fstream fin2("filter2.txt");
    fin2>> signal2;

    add=signal+signal2;
    add.print();

    return 0;
}

Stating fstream fin twice, the compiler wants to create a variable that still exists. It cannot do that. Once you have given a name to a variable you cannot "empty" the name and declare it again. fin can be used again but not as declaration.
You can do this instead of declaring a new variable:
1
2
3
4
5
    fstream fin("filter1.txt");
    fin>> signal;
    fin.close();
    fin.open("filter2.txt");//I don't know whether fin("filter2.txt") would work
    fin>> signal2;
Last edited on
Thank you for your reply.
I just tried it and I was able to open the file with 'fin.open("filter2.txt"); not fin("filter2.txt") '
Topic archived. No new replies allowed.