Arpit
Arpit

Reputation: 105

C++: Correct use of stream operations

I am trying to learn to use the file stream correctly. The following is a snippet of code that I am maintaining. It works fine, but I was wondering whether the usage is correct and whether there are better ways to perform these tasks. For example, the use of SEEK_SET and ios::beg together. Also, is ios::ate really needed given that the next seek is to the beginning of file.

Could you please help me correct(streamline) the code.

ofstream fout(fpath,ios::out| ios::binary | ios::ate);
fout.seekp(SEEK_SET,ios::beg);
fout.write((char*)&test,sizeof(test));

fout.seekp(sizeof(off_t),ios::beg);
fout.write((char*)&temp,sizeof(temp)); //temp is int

fout.seekp((sizeof(off_t)+ sizeof(int)),ios::beg);
fout.write((char*)o,sizeof(MyClass));  

fout.seekp((sizeof(off_t)+ sizeof(int) + sizeof(MyClass)),ios::beg);
fout.write((char*)&l,sizeof(int)); 

fout.close();

Upvotes: 1

Views: 597

Answers (3)

Baltasarq
Baltasarq

Reputation: 12212

If you are trying to use the possibilities of the stream, for training purposes, then the code is fine.

If you are just trying to write some data, then the seekp() method calls are not needed, since the writing pointer will be updated automatically to the end of the last write. seekp() is only needed if you need to navigate among records, as in a database, for example.

ofstream fout(fpath,ios::out| ios::binary | ios::ate);

fout.write((char*)&test,sizeof(test));
fout.write((char*)&temp,sizeof(temp));
fout.write((char*)o,sizeof(MyClass));
fout.write((char*)&l,sizeof(int)); 

fout.close();

This code will work just fine. About problems in your code, you seem to be mixin the C library stdio, with its fseek() function, with the seekp() and seekg() methods. So, it is not very valid to pass SEEK_SET as the first parameter of your call to seekp(). In C, you would write:

#include <cstdio>

//... 

fseek( f, 5, SEEK_SET );  // from the beginning
fseek( f, -2, SEEK_CUR ); // from the current position
fseek( f, 2, SEEK_CUR );  // from the current position
fseek( f, -2, SEEK_END ); // from the end of the file

SEEK_SET will have an int value (implementation-specific, say 2), and therefore, with your line:

fout.seekp(SEEK_SET,ios::beg);

You're just starting say 2 bytes off the beginning, while your file may or may not have contents when you open it.

About ios::ate, it puts the pointers for reading and writing at the end of the file (you would have to use seekp and seekg to achieve the same behaviour if you needed it, instead of using ios::ate).

Upvotes: 2

Fred Foo
Fred Foo

Reputation: 363577

Apart from what others have written, fout.seekp(sizeof(off_t),ios::beg); is almost certainly a mistake. It sets the stream output pointer to an implementation-specific number of bytes from the beginning of the stream.

Upvotes: 0

Puppy
Puppy

Reputation: 146930

You should always use operator<< wherever possible.

ofstream fout(fpath,ios::out| ios::binary | ios::ate);
fout << test;
fout << temp;    
fout << o;
fout << l;    
fout.close();

Not sure what you're doing with the seeking so I cut them for my demonstration, but if you're seeking to the new writing position then it's definitely unnecessary. And there's no way that SEEK_SET is a valid C++ Standard library constant- they don't use that naming convention.

Upvotes: 1

Related Questions