Hello, every time i try to open a text file to display the content, it will create another blank file with the same name as the file that i am trying to open.
In the case below, I used strings and a loop to display all the content of a file(that already exists). I'm sure there's a glitch somewhere. Any help would be appreciated.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main ()
{
string string1;
fstream file;
file.open("data.txt", ios::in | ios::out | ios::app);
if (file.is_open())
{
cout << "File has successfully been opened" << "\n";
}
else
{
cout << "Error, could not open file" << "\n";
}
cout << "Here is the content of the file;
while (getline (file , string1))
{
cout << string1 << "\n";
}
file.eof();
}
I am trying to open the file data.txt, it's content consists of a list of student names and their classroom numbers. But every time i try to open this specific file, it creates another one with the same name. What am I doing wrong here?