In: Computer Science
In c++
Write a function that:
#include <iostream>
#include <fstream>
using namespace std;
int func(string s,int n){// functions accepting a string and integer
fstream filestr;
filestr.open ("test.txt");
if (filestr.is_open())
{
filestr << n;// write the integer to file
filestr.close();
return 0;
}
else
{
return -1;// return -1 if file is not open
}
}
int main () {
cout<< func("a",2);
}
Execution output:
integer written to file: