trying to make binary stream object

Here part of the code

#include <iostream>
#include <fstream>

using namespace std;

int main() {
char filename[81];
int n;
int age;
int recsize = sizeof(name) + sizeof(int);


cout << "Enter file name: ";
cin.getline(filename, 80);

// Open file for binary read and write.

fstream fbin(filename, ios::binary | ios::in | ios::out);
if (!fbin) {
cout << "Could not open file " << filename;
return -1;
}

when I type a file name to be created for the binary write I get an exit 255 because the file could not be opened. When I debug and look at the fbin object it says incomplete type. I am using netbeans 6.5 and ios::binary is in like blue color but binary is in black and ios::in is all light blue, and ios::out is light blue but out is in black. Is this example from the book old? Should this code work?
Last edited on
I think that this code is ok.
I am able to open a file successfullyy with you code. What issue are you facing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>

using namespace std;

int main() 
{
	char filename[81];
	//int n;
	//int age;
	int recsize = sizeof(filename) + sizeof(int);


	cout << "Enter file name: ";
	cin.getline(filename, 80);

	// Open file for binary read and write. 

	fstream fbin(filename, ios::binary | ios::in | ios::out);
	if (!fbin)
	{
		cout << "Could not open file " << filename;
		return -1;
	}
	else
	{
		cout << "File opened succesfully" << endl;
		fbin.close();
	}
	return 0;
}
Topic archived. No new replies allowed.