-1
#include<conio.h>
#include<iomanip>
#include<cmath>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;    
int main()
{
    fstream afile;
    afile.open("example.txt");
    afile<<"Hi I am Unnat";
    afile.close();
    string line;
    ifstream myfile ("example.txt");
    if (myfile.is_open())
    {
        while ( getline (myfile,line) )
        {
             cout << line << '\n';
        }
        myfile.close();
    }
    else cout << "Unable to open file"; 
    return 0;
}

The output i get here is always Unable to open file and when i check the folders I dont find example.txt Please help.

Unnatch
  • 13
  • 1
  • 3
  • 1
    Try `afile.open("example.txt", std::ios::out);` – R Sahu Nov 16 '15 at 18:48
  • Have you read the documentation on the function giving you trouble? http://en.cppreference.com/w/cpp/io/basic_fstream/open which calls http://en.cppreference.com/w/cpp/io/basic_filebuf/open? – Mooing Duck Nov 16 '15 at 18:48
  • You check if the input file can be opened, but not the output file. Why is that? – Some programmer dude Nov 16 '15 at 18:48
  • @RSahu The default open-mode of [`std::fstream::open`](http://en.cppreference.com/w/cpp/io/basic_fstream/open) is `ios_base::in|ios_base::out`, so `out` is already set. – Some programmer dude Nov 16 '15 at 18:49
  • @RSahu: That was my idea as well, though it [should work fine to open for both input and output even if the file doesn't exist](https://stackoverflow.com/questions/2896166/fstream-in-and-out-on-nonexistent-file#comment50923971_2896651). I'd be even more explicit myself, and use an `ofstream` instead of `fstream` so output-only was explicit. – ShadowRanger Nov 16 '15 at 18:51
  • 1
    @JoachimPileborg: Yeah, but [`ios_base::in` requires an existing file](http://stackoverflow.com/a/8836041/560648). RSahu is correct — [you need `std::ios::out` and _not_ `std::ios::in`, to get automatic file creation](http://stackoverflow.com/a/31483487/560648). – Lightness Races in Orbit Nov 16 '15 at 18:54
  • @LightnessRacesinOrbit: Do you have a standard citation for that? The comment I linked above indicates it (possibly compiler dependent) doesn't require an existing file. It seems rather odd to forbid opening a new file for input and output at once without explicitly truncating, since it's perfectly legitimate to create a file than you'll be writing and reading intermittently. – ShadowRanger Nov 16 '15 at 18:57
  • @ShadowRanger: Yes, it's odd and annoying. But it _is_ so. It's inherited from UNIX. I'm not going to dig out all the standard references again - one of the aforelinked questions probably has what you're looking for. – Lightness Races in Orbit Nov 16 '15 at 18:59
  • have used fstream::out also but when I start the compiling with VS and start without debugging, it does not create a file. But running the .exe file creates the txt file but it still shows the output Unable to open file – Unnatch Nov 16 '15 at 19:08
  • @Unnatch: Are you actually listening to anyone? – Lightness Races in Orbit Nov 16 '15 at 19:14
  • @LightnessRacesinOrbit I am sorry. I am a newbie. I am not able to get what people are saying on the comments. What I got was adding fstream::out would help and I tried that but when I want to open the file created, its not happening. – Unnatch Nov 16 '15 at 19:18
  • @Unnatch: Multiple links to information have been provided. Did you consider clicking on any of them? – Lightness Races in Orbit Nov 16 '15 at 19:19
  • @LightnessRacesinOrbit I did but nothing was similar to my situation. I got my file created but now the code is not able to read the txt file. – Unnatch Nov 16 '15 at 19:22
  • That does not seem likely. – Lightness Races in Orbit Nov 16 '15 at 19:25

1 Answers1

0

Check out C++ Reference. I don't believe fstream::open creates the file if it doesn't exist. You need to use afile.open("example.txt",fstream::out); as referred to in fstream won't create a file

Community
  • 1
  • 1
depwl9992
  • 150
  • 1
  • 13
  • I have used fstream::out also but when I start the compiling with VS and start without debugging, it does not create a file. But running the .exe file creates the txt file but it still shows the output Unable to open file – Unnatch Nov 16 '15 at 19:04