Problem with read and write file

I don't understand why it printed "File is empty". Can someone help me?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>
#include <conio.h>
#include <fstream>

using namespace std;
int main()
{
    fstream dataFile;
    dataFile.open("file.txt", ios::in | ios::out | ios::app);
    dataFile<<"Ngoc\n";
    if(dataFile==NULL)
     cout<<"File is empty";
    dataFile.close();
    getch();
}
2 possible cultprits:
- the file is not where you've told the program it is.
- permission issues on the file.

The code is fine but I'm not sure what line 10 is for.

maybe the file isn't in the folder with the program.
I use ios::out if file is not created before, it will create file.txt for us
didnt know that created the file sorry :)

Anyway, i've just copied and pasted your code into my ide and it works fine.

(vs 2010 pro edition).

what are ide/compiler you using?
I used Dev 4.9.9.2 :( Did you open file.txt by notepad and check its value?
yes and it's fine, i.e. "Ngoc".
Last edited on
where in your file system are you running this?
I use ios::out if file is not created before, it will create file.txt for us


You missed the point. That is not what that line of code does.
It adds NGOC to the file every time you run the program, and I don't think that is your intent. Your code will create the file without that, it will just be empty.
How must I solve it?
OP have you browsed to where the file should be created and is it there?
yes I did it. @mutexe: Can I addfriend with you on facebook?
no.

is the file there??
Note: Dev C++ V 4.9.9.2 was released in 2005. It's considered obsolete.

The latest version is Orwell DevC++ v5.5.1 - released October 2013.
http://orwelldevcpp.blogspot.co.uk/
Topic archived. No new replies allowed.