0

I am trying to write and read from a file in the same cpp program, but i am getting 3 errors

conflicting decleration 'std::istream theFile'
'theFile' has a previous decleration as 'std::istream theFile'
no match for 'operator>>' 'in theFile >>n' 

while you answer this question try to be more noob specific. here is my code.

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    int n;
    string name;
    ofstream theFile;
    istream theFile;
    theFile.open("Olive.txt");

while(cin>> n>>name)
{
    theFile<< n<<' '<< name;
}

while(theFile>>n>>name)
{
    cout <<endl<<n<<","<<name;
}

    return 0;
}

2 Answers 2

1

Use std::fstream instead. It can read/write file, and does what you need. So you will not have to open file two times, as you will doing ifstream and ofstream.

Sign up to request clarification or add additional context in comments.

Comments

1

You have declared two variables with the same type. This is not possible. You should declare two variables for the in and outfile and then open the same file:

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    int n;
    string name;
    ofstream theFileOut;
    ifstream theFileIn;
    theFileOut.open("Olive.txt");

while(cin>> n>>name)
{
    theFileOut<< n<<' '<< name;

}
theFileOut.close();
theFileIn.open("Olive.txt");
while(theFileIn>>n>>name)
{
    cout <<endl<<n<<","<<name;
}

    return 0;
}

1 Comment

Thank you tune that explains everything

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.