fstream ifstream ofstream

i want to know if we can use these three in a single program
i have designed a program to create a .txt file (if it doesn't already exist)
and to enter some line in it then display the line
but compiler shows declaration error




Here is my program Check it out thanks for your guidness
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
fstream file("Hassan.txt", ios::in | ios::out| ios ::trunc);
if(!file.is_open())
{
cout<<"ErroR";
}else
{

cout<<"Done"<<endl;
}
ofstream file("Hassan.txt");
{
file<<"We are happy"<<endl;
file<<"We will be happy always :)"<<endl;
file.close();
}
ifstream file("Hassan.txt");
{
string line;
getline(file,line);
cout<<endl<<line<<endl;
string line2;
getline(file,line2);
cout<<endl<<line2<<endl;
}
return 0;
}
i want to know if we can use these three in a single program
Yes.

i have designed a program to create a .txt file (if it doesn't already exist)
and to enter some line in it then display the line
In your implementation, you attempt to open the same file for create, read and append at the same time. Does that seem reasonable to you?

but compiler shows declaration error
The normal way to deal with errors is to post the error exactly as the compiler produced it. In this case, you've declared all three file streams to have the same name. But the syntax error isn't the error you should focus on, but the error described above.
In your implementation, you attempt to open the same file for create, read and append at the same time. Does that seem reasonable to you?



So i should create 3 separate programs??
Think of it like this:
1
2
3
    int a = 3;
    int a = 6;
    int a = 5;

This code won't compile because you have three separate variables which have all been given the same name.
In that example, you might change it to
1
2
3
    int a = 3;
    int b = 6;
    int c = 5;


In your code using files a similar problem arises, the name file is used multiple times.

Maybe something like this:
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 <fstream>

using namespace std;

int main()
{
    ofstream file_out("Hassan.txt");
    file_out << "We are happy" << endl;
    file_out << "We will be happy always :)" << endl;
    file_out.close();
	
    ifstream file_in("Hassan.txt");
    string line;
    getline(file_in,line);
    cout << endl << line << endl;

    getline(file_in,line);
    cout << endl << line << endl;

    return 0;
}  


Or maybe like this, using braces { } to limit the scope of the name.

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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    {
        ofstream file("Hassan.txt");
        file << "We are happy" << endl;
        file << "We will be happy always :)" << endl;
    }
    
    {    
        ifstream file("Hassan.txt");
        string line;
        getline(file,line);
        cout << endl << line << endl;
        
        getline(file,line);
        cout << endl << line << endl;
    }
        
    return 0;
} 
Last edited on
Topic archived. No new replies allowed.