0
#include <iostream>
#include<fstream>
using namespace std;
int main() {
    ofstream myfile;
    ofstream file2;
    myfile.open("/home/me/CLionProjects/Project2/xample.txt", ios::app); //this file doesn't exist
    file2.open("testingfile.txt", ios::app); // this file also doesn't exist
    if (myfile.is_open())
    {
        myfile << "Writing this to a file.\n";
        cout<<"test"<<endl;
        myfile.close();
    }
    else cout<<"error";
    if(file2.is_open())
    {
        file2<< "Writing this to a file";
        cout<<"test2";
        myfile.close();
    }
    else cout<<"error";
return 0;

The result I get out of this code is: both myfile and file2 pass their if test and in the case of myfile new file xample.txt is saved in the directory I created. However, no new file "testingfile.txt" can be found on my computer(or I just can't find it). According to this source a new file should be created: http://www.cplusplus.com/doc/tutorial/files/ What am I doing wrong? EDIT: Operating system: Linux Ubuntu 18.04

scohe001
  • 15,110
  • 2
  • 31
  • 51
Tytanuser
  • 15
  • 5

0 Answers0