Home > Software design >  visual studio code not running code cause of ofstream
visual studio code not running code cause of ofstream

Time:02-23

I have set up my Visual Studio Code and it works fine, but when I want to use fstream functions, it doesn't run. Here's an example:

#include <iostream>
#include <math.h>
#include <conio.h>
#include <fstream>
#include <string>
using namespace std;

void createfile();

int main(){
    int choice;
    cout << "Enter 1 to create file" << endl;
    cin >> choice;
    switch (choice){
        case 1:
            createfile();
            break;
        case 2:
            break;
    }
    return 0;
}

void createfile(){
    ofstream file ("file.txt");
    file.close();
}  

The terminal says:

PS D:\vs code cpp> cd "d:\vs code cpp\" ; if ($?) { g   test.cpp -o test } ; if ($?) { .\test }

And then after trying to run, it says:

PS D:\vs code cpp>

Literally nothing. I even reinstalled Visual Studio Code, but it didn't work.

Does anyone know what the problem is?

CodePudding user response:

I reinstalled mingw again and it worked.

CodePudding user response:

First of all, it does not need so many header files, they don't seem to have any function in this code of yours. As you are just creating a file, it can be done using this code:

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

void createfile();

int main(){
    int choice;
    cout << "Enter 1 to create file" << endl;
    cin >> choice;
    switch (choice){
        case 1:
            createfile();
            break;
        case 2:
            break;
    }
    return 0;
}

void createfile(){
    ofstream file;
    file.open ("file.txt");
    file.close();
}
  • Related