fstream doesn't creat file

hello
im using dev cpp 4.9 on win xp sp3
here's the problem :
let's say i want to create a file named "h1.vhdd" in "c:\vhdd"

i tried this cod

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
fstream t;
t.open("c:\vhdd\h1.vhdd",ios::out);
t.close();

system("PAUSE");
return 0;
}


but it does not creat the file.Itried this cod in Visual c++ 2008 but it didn't work either .
you haven't created the file. Infact you are trying to access the file in the line
t.open("c:\vhdd\h1.vhdd",ios::out);

The proper method to create the file is
fstream t("h1.vhdd");
Then you may access it with the t.open() command.
Last edited on
That has nothing to do with the problem.

The reason he has no file is that he didn't write anything to it. If all you want is a blank file, make sure to flush before closing:
1
2
3
4
fstream f;
f.open( "fooey.txt", ios::out );
f << flush;
f.close();


Hope this helps.
tanks
that work fine.

here are some things i found last night , and i think they are good to know:
1. one not so obvious mistake that i made is that in C++ if you want to use a "\" in a string you have to double it so insted of
t.open("c:\vhdd\h1.vhdd",ios::out);
i sould write
t.open("c:\\vhdd\\h1.vhdd",ios::out);
. you can see the double "\" . for a better understanding you can look at http://www.cplusplus.com/doc/tutorial/constants.html .

2.if you want to use the "fstream" to worck with files there are some tricks
A. if you try to creat a file (that does not priviously exits) to use it for writing and reading using code like "fstream file("new_file",ios::out | ios::in) IT WON' T WORCK!!!!!!.
you have to create the file only for writing fstream file("new_file",ios::out) then close the file file.close() and now you can open it again this time for what ever operation you want , for exaple file.open("new_file",ios::out | ios::in | ios::binary) now it will work.

B.if you want to creat a file (that does not priviously exists) at a cirtain location ( for example "c:\\my_folder\\new_file") again you have to create it first only for writing then close it and after that open it again whit what ever operations. BUT IT WON'T WORCK UNLES THE FOLDER "my_folder" ALLREADY EXISTS .
IF YOU WANT TO CREATE THE HOLE STRUCTURE "\\my_folder\\new_file" you have to create the folder "my_folder" first.
you ca do that using the
system()
function , which takes operating system comands . for example the ms-dos comand for creating a folder is "md folder_name" . so to create the forder "my_folder" in "c:\\" you have to call the sistem() function like this
system("md c:\\my_folder");
. after you created the folder you can create the file in it using the normal fstream.
Sorry I missed the backslashes in your strings... Yeah, you've got to double them.

You can actually open a file for both reading and writing, you just have to reset the read pointer before reading.

The simplest way to think of a file is as an array of bytes (or chars) on disk. So, when you first create a file, the file pointer is addressing the first byte in the file.
1
2
[ ][ ][ ][ ][ ][ ]
 ^

After writing to the file, you have updated the file pointer:
myfile << "Hi!";
becomes:
1
2
[H][i][!][ ][ ][ ]
          ^


If you now want to read from the file, remember that the file pointer is still at the end of the file. You first have to reset it to the beginning of the file.
myfile.seekg( 0 );
becomes:
1
2
[H][i][!][ ][ ][ ]
 ^

Now you can read what you wrote:
char c = myfile.get();
becomes:
1
2
[H][i][!][ ][ ][ ]
    ^

where c contains the value 'H'.

The only 'gotcha' that you have to remember with streams is that the read pointer and the write pointer aren't necessarily separate entities (even though they are supposed to be for C++), so if you change one then assume that the other is lost. In other words, after you read, use seekp() before you write, and after you write, use seekg() before you read.


As for folders, it is a convenience of the operating system's user interface that certain user commands allow you to create folders and files at the same time. In C++ code, you have to remember that they are actually separate actions, so you must verify or create each directory before you can create a file.

The typical C and C++ function to do these things, while non-standard C and C++, but are POSIX functions, are:
1
2
3
4
5
#include <sys/stat.h>
#include <sys/types.h>

int stat(const char *restrict path, struct stat *restrict buf);
int mkdir(const char *path, mode_t mode);

http://www.opengroup.org/onlinepubs/009695399/functions/stat.html
http://www.opengroup.org/onlinepubs/000095399/functions/mkdir.html


Also, there is the Boost Filesystem library, which makes handling these things a breeze.
http://www.boost.org/doc/libs/1_35_0/libs/filesystem/doc/index.htm

Hope this helps.
if you can creat a file directly for bouth reading and writing , why does this code returns " the file is not open" . important!! the file "test_file" does not priviously exists


#include<iostream>
#include<fstream>

using namespace std;

int main(void)
{
fstream file("test_file",ios::out | ios::in | ios::binary);
if(file.is_open()) cout<<"the file is open ";
else cout<<"the file is not open";

system("PAUSE");

return 0;
}
Non-obvious answer (yeah, I know... welcome to C++ streams :-S ):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<fstream>
#include<limits>

using namespace std;

int main()
{
fstream file("test_file",ios::out | ios::in | ios::binary | ios::trunc);
if(file.is_open()) cout<<"the file is open ";
else cout<<"the file is not open";

cout << "Press ENTER to continue...";
cin.ignore( numeric_limits<streamsize>::max(), '\n' );

return 0;
}


Please use code tags.
Last edited on
thanks now it works.

bat i don't underatand this
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
can you please recomand me a site where i can read more?
How about this one?
http://www.cplusplus.com/reference/iostream/istream/ignore.html

What it does is gets rid of anything up-to and including the newline (ENTER key) that the user pressed.

Using system("PAUSE") is OK so long as you keep it out of production code, since it is a resource hog and a security hole.

Hope this helps.
Topic archived. No new replies allowed.