fstream "lame" problem

Hello guys,

could anyone please tell me why this program is writing a 0 byte file and not the whole set of data in the vector?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    vector<double> temp;
    temp.push_back(1);
    temp.push_back(2);
    temp.push_back(3);
    temp.push_back(4);
    temp.push_back(5);
    temp.push_back(4);
    temp.push_back(3);
    temp.push_back(2);
    temp.push_back(1);
    fstream myFile;
    myFile.open ("c:\\x.bin", ios::out | ios::binary);
    char* buffer = (char*)&temp.front();
    myFile.write(buffer,sizeof(double)*temp.size());
}


It's writing an empty file... which is weird.

Thanks :-)
Modern versions of windows require admin privilages to write directly to C:\.
Try putting the file in a different folder (desktop? or maybe just use "x.bin" and put it in the current directory).

After doing that, it works fine on my box.
Maybe it's not 0-byte, but you cannot see the symbols, try with...
1
2
3
4
5
6
7
8
9
    temp.push_back('1');
    temp.push_back('2');
    temp.push_back('3');
    temp.push_back('4');
    temp.push_back('5');
    temp.push_back('4');
    temp.push_back('3');
    temp.push_back('2');
    temp.push_back('1');
Thanks for the answers, guys.

@Disch: I thought of that. But if that's the case, why would it open the file in the first place? it would've complained about not opening the file. the function fstream::is_open() returns true!!! I changed the directory to desktop and I'm still getting the same result.

@EssGeEich: I want to store them as doubles, not as chars.

There's something I can't see in here... any ideas?
Seriously, this is driving me crazy!!! ASCII is working but binary isn't... wth is going on with this compiler?!?!
Weird. What compiler are you using?

Like I say it worked for me when I tried it on VC++ 2010 express.
If you want to read that file on notepad, notepad reads in every file as CHARs. If you want to store them as doubles and want to retrieve them back as doubles, you can do it. but if you want to open your newly created file with a text editor, save it as text.
P.S. : Use Notepad++ instead of notepad, it shows you some symbols in place of unrepresentable chars ([NULL],[EDX]... and similars.)
Last edited on
closed account (o1vk4iN6)
e: nvm

Last edited on
Thanks for your replies :)

I'm using MinGW that comes with QtSDK 1.2.

I'm creating a tempatised class for mathematics that has the option of writing and reading files. I'm not just playing around with fstream... I'm writing that file and trying to read it with my container.

This is really weird... I give up...!

If you got any ideas or similar experience, guys, please let me know.
Sorry for that.
Try using FILE *, fopen, fwrite, fread...
http://cplusplus.com/reference/clibrary/cstdio/fwrite/
http://cplusplus.com/reference/clibrary/cstdio/fread/
(to the second parameter for fopen, i suggest "wb" (write binary) and "rb" (read binary) respectively for Creating a file and Opening a file )
Thanks a lot man... with all its weirdness, it has worked!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    vector<double> temp;
    temp.push_back(1.);
    temp.push_back(2.);
    temp.push_back(3.);
    temp.push_back(4.);
    temp.push_back(5.);
    temp.push_back(4.);
    temp.push_back(3.);
    temp.push_back(2.);
    temp.push_back(1.);
    void* buffer = (char*)&temp.front();
    FILE *file;
    file = fopen("C:/data.bin","wb");
    fwrite(buffer,temp.size(),sizeof(double),file);


I'm still wondering what the hell is wrong with fstream!!!! I seized to use FILE pointers like ages ago!!!

Thanks again, buddy!
Last edited on
I know exactly what you made wrong in the previous program!
char* buffer = (char*)&temp.front();
temp.front() is an iterator, which means it's kinda a pointer, so basicly you are converting double** to char*, which compiles but gives the wrong answer. try
char* buffer = (char*)temp.front();(without the ampersand)
Last edited on
front() returns a reference to the first element in the vector.
begin() returns an iterator to the first element in the vector.
There should be no difference between using std::fstream and using the devil's struct, besides the latter being slightly more evil.
Last edited on
Nah he did that right.

temp.front() is a reference to the first element, just like doing temp[0]. &temp.front() would be the same as &temp[0], which is right.

You were probably thinking of temp.begin(), which is an iterator.

(char*)temp.front(); will likely fail to compile.
Maybe he didn't flush the stream, or close the file properly?
When a std::fstream object goes out of scope it flushes and closes.
When a pointer to a FILE struct goes out of scope, it gives you problems.
Topic archived. No new replies allowed.