filestream

I tried to open a file in input,output and binary. However it gives me trouble.Look at the test code below:


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
32
33
34
35
36
37
38
39
 int main()
 11 {
 12 fstream data;
 13 const char name[10] = "hello";
 14 int buffer[10] = {0,1,2,3,4,5,6,7,8,9};
 15 int buf[10];
 16 int ret = openFile(data,name);//call the function to open file
 17 if(!ret)
 18 {
 19         exit(-1);
 20 }
 21 data.write((char*) buffer, sizeof(buffer));//write to file
 22 cout<<"write completed"<<endl;
 23 //data.close();
 24 
 25 //data.open(name,ios::in | ios::binary);
 26 data.read((char *) buf, sizeof buf);//read from file and store it in buf
 27 
 28 for(int i=0;i<10;i++)
 29 {
 30 cout<<buf[i]<<" ";//printing values in buf
 31 }
 32 cout<<endl;
 33 
 34 data.close();
 35 return 0;
 36 }
 37 
 38 //function to just open the file
 39 int openFile(fstream &datafile,const char *ptr)
 40 {
 41 datafile.open(ptr, ios::in | ios::out | ios::binary);
 42 if(!datafile)
 43 {
 44         cout<<"open failed!"<<endl;
 45         return 0;
 46 }
 47 return 1;
 48 }


However , it does not work if the file isn't already created which defies tony gaddis c++ book page 884 chapter 14 which says the file will be created.

Further assuming i created the file, it still prints garbage values.

write completed
384877536 32767 65535 1 384877392 32767 4197686 0 384877424 32767 


However , if i uncomment line 23 and 25 and modify line 41 to just ios::out|ios::binary, the output works.

write completed
0 1 2 3 4 5 6 7 8 9 


Hence, if this is the case and we cant use in and out together why then do we need fstream object. It defeats the purpose. ifstream and ofstream are sufficient. please explain.Advance Thanks.
I have no idea why doesn't it create a file.
I do, however have an idea how to solve your problem.
Add data.seekg(0); between reading and writing. fstream object has to pointers, get and put. They indicate where in the file to read/write. And apparently they move together.. (I'd be glad if someone could explain to me how does that make any sense)
So you have to move it back to beginning if you want to read what you wrote.
Where do I begin?

1. Don't create a stream, then open/close it. Someone out there's teaching this style which is wrong. Instead, remember that objects can be initialised for immediate use (usually) and cleanup on termination. So the declaration should be:
std::fstream data(name, ios::in | ios::out | ios::binary | ios::trunc);

2. Once you've written the data, you need to move the read pointer to the start before reading. You can check where the read pointer (get position) is with data.tellg(). That's you're not seeing what you've written.

3. A note on declarations.
const char name[10] = "hello";
declares an array of chars 10 elements long, then intialises it with an array of chars 7 elements long (hello plus the null terminator). The remaining 3 chars are never used. It's better to just use the 7 characters:
const char name[] = "hello";

4. Putting it all together:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	const char name[] = "datafile.out";
	fstream data(name, ios::in | ios::out | ios::binary | ios::trunc);

	int buffer[] = {0,1,2,3,4,5,6,7,8,9};
	data.write((char*)buffer, sizeof(buffer));

	data.seekg(0, ios_base::beg);
	int buf[10];
	data.read((char*)buf, sizeof(buf)/sizeof(int));

	for (int i = 0; i < 10; ++i)
		cout << buf[i] <<" ";
	cout << endl;

	return 0;
}
Last edited on
Topic archived. No new replies allowed.