fstream not creating file
May 30, 2016 at 9:06pm UTC
anybody have any idea why a file is not being created called haha.txt when I run this code?? am I missing something?
I'm running codeblocks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream theFile("haha.txt" );
while (theFile.is_open()){
string one;
getline(cin,one);
theFile >> one;
cout << "file is open" ;
}
}
May 30, 2016 at 9:10pm UTC
An ifstream by default will not open a file that doesn't exist. If you want to create the file if it doesn't exist you'll need to use the correct open mode (app).
May 30, 2016 at 9:10pm UTC
Because you're trying to read from the file. Reading from a file doesn't create files.
Topic archived. No new replies allowed.