Differences of ios::out and ios::trunc

...continued from
http://www.cplusplus.com/forum/beginner/77034/
---------
I would like to ask, what's the difference of ios::out , ios::trunc

1
2
ofstream out("/bla/bla/a1.txt", ios::out);
ofstream out("/bla/bla/a1.txt", ios::trunc);


The commands above (not together in the same program...), did the same thing in a1.txt....
It was full of text, and then the text gone away !!!

So what's the difference ??
THANKS IN ADVANCE !!!
[Sorry about my persistence - I just dying for learning .... ]
All I can think of is that adding the out flag for std::ofstream is redundant.

I could see a use for out in conjunction with in, for an std::fstream:
std::fstream ioFile("file.txt", std::ios_base::in | std::ios_base::out);

So maybe for other purposes, you should use trunc for better clarity?
My friend Catfish2, thanks for replying ...
I understand that ofstreams do not need ios::out.
What other purposes for, we use the ios::trunc ??
If I use

ofstream out("/bla/bla/a1.txt");

it is the same with (it does the same .. I just tried it ...)

ofstream out("/bla/bla/a1.txt", ios::trunc);

So for what purpose we use the ios::trunc ?? You know I searched a lot of ebooks, but no one explains it !!!!
Anyway it is honnorable for me for replying .... What is your opinion for this my friend ?

[Sorry for my English - I am Greek]
I think that just with the out flag, you're trying to use trunc for the wrong type (std::ofstream).

Consider that by default std::ofstream behaves as if it gets ios::out | ios::trunc, and use the out and trunc flags only where they are truly needed. For example, std::fstream.

Edit:
I understand you are probably confused by how it doesn't give you a warning or an error, but that's how C++ is. In this case you're not doing anything wrong, by giving it its implicit flags... but did you notice you can give std::ofstream the in flag?
Last edited on
My friend I just show your answer...
Give me a couple of hours to study in these you wrote me (and other things in this subject), and I will call you back !
My friend look what I found:

http://stdcxx.apache.org/doc/stdlibug/30-3.html
http://www.functionx.com/cpp/articles/filestreaming.htm
http://www.pcfunia.com/2011/08/01/file-operation-mode-like-out-app-ate-in-binary-trunc-e-t-c/

As I understand the ios::out, ios::trunc, is the same thing....
The strange thing I discovered is that

1
2
fstream strm;
strm.open("/bla/bla/a1.txt", ios::trunc);


didn't truncate the "a1.txt"....
Only God knows what is going on with this !!!
Thanks again for everything !!
I think you need to specify in or out for std::fstream:
1
2
fstream strm;
strm.open("/bla/bla/a1.txt", ios::out | ios::trunc);


I'll check now in Visual Studio 2010...
Edit: out | trunc truncates.

Edit 2:
As I understand the ios::out, ios::trunc, is the same thing....

No, out and trunc are not the same.
From tests, out implies trunc, but trunc does not imply out.

And out is implicit for std::ofstream.
Last edited on
Friend I didn't try it before for reading or writing the fstream, I just wanted to check what trunc does in this case....
Now I tried the following 2 and both truncate the file:

1
2
fstream strm;
strm.open("/bla/bla/a1.txt", ios::out | ios::trunc);


1
2
fstream strm;
strm.open("/bla/bla/a1.txt", ios::out);


So you have right !
As I said I understand the ios::out, ios::trunc, is the same thing....
I think it only applies to file systems streams however. ios::out and ios::trunc live within the ios namespace (rerouted, I believe) and can be accepted by all standard input/output objects. I believe I read something about there being support for a printer stream (don't remember where that was at) so those flags might behave differently than they do with file objects. It would be interesting to see how far you can dig.
Last edited on
I won't dig any more my friend ! It's just that I try to understand something with details...
That's why I decided to ask in this site, that you know things in C++ better than me !!
Thanks all of you for your support and help !
Soon I will "disturb" you with other difficulties in C++. One of them is "volatile", but I have to re-search for this first and I will open for this, another thread in this forum...
Thanks again all of you friends !!
It probably has some bugs, though. At least it shows that out and trunc is not the same thing.
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
40
41
42
43
44
45
#include <fstream>
#include <iostream>
#include <list>

template <typename StreamFlag>
std::list<int> binaryList(const StreamFlag &sf)
{
	std::list<int> r;
	int castedFlag = static_cast<int> (sf);
	unsigned int bitsCount = 8;

	while (bitsCount-- != 0)
	{
		if (castedFlag != 0)
		{
			r.push_front(castedFlag % 2);
			castedFlag /= 2;
		}
		else
			r.push_front(0);
	}

	return r;
}

std::ostream & operator << (std::ostream &os, const std::list<int> &l)
{
	for (std::list<int>::const_iterator it = l.begin();
		it != l.end();
		++it
		)
		os << *it;

	return os;
}

int main()
{
	std::cout << "trunc:\t" << binaryList(std::ios_base::trunc) << '\n';
	std::cout << "out:\t" << binaryList(std::ios_base::out) << '\n';
	std::cout << "app:\t" << binaryList(std::ios_base::app) << '\n';
	std::cout << "ate:\t" << binaryList(std::ios_base::ate) << '\n';
	std::cout << "binary:\t" << binaryList(std::ios_base::binary) << '\n';
	std::cout << std::endl;
}
(Visual Studio 2010 Express)
trunc:	00010000
out:	00000010
app:	00001000
ate:	00000100
binary:	00100000

(GNU GCC 4.7.1)
trunc:	00100000
out:	00010000
app:	00000001
ate:	00000010
binary:	00000100

(Borland 5.5.1)
trunc:	00010000
out:	00001000
app:	00000001
ate:	00100000
binary:	00000010


Good God, is Borland fast! Old technology, I suppose.
Friend Catfish2, very interesting the code you are writing...
I will study it line-by-line ...
By the way what compiler do you prefer ?
I use g++ & qtcreator because I love linux....
I started Qt GUI Programming 5 months ago, but I stopped to learn some details in nongui-C++...
In 2 months I will continue the Qt.
Maybe in 10 years I will reach your abilities in C++
By the way what C++gui do you prefer ?
What compiler? GCC (g++) mostly.
What GUI? None.
Maybe in 10 years I will reach your abilities in Qt. Or maybe wxWidgets.

As for the above code... I think it should work on other kinds of flags besides openmode but you may have to increase bitsCount = 8;.
Stroustrup suggested a lot of guis in his site..... One of them is "The wxWidgets" as you said.
I will try to write code by myself to produce the above output - for exercise....
After this I will study your code !!
Topic archived. No new replies allowed.