By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,400 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,400 IT Pros & Developers. It's quick & easy.

fstream doesn't write

P: n/a
My problem is this (from:
http://www.cplusplus.com/ref/iostream/fstream/open.html)

#include <fstream>

using namespace std;

int main()
{
fstream f;
f.open("a.txt",fstream::in | fstream::out | fstream::app);
f << "out";
f.close();
return 0;
}

The program doesn't produce any a.txt file! I'm using g++ (GCC) 4.1.0 (SUSE
Linux). I want read and write a file.
Oct 28 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
wiso wrote:
My problem is this (from:
http://www.cplusplus.com/ref/iostream/fstream/open.html)

#include <fstream>

using namespace std;

int main()
{
fstream f;
f.open("a.txt",fstream::in | fstream::out | fstream::app);
f << "out";
f.close();
return 0;
}

The program doesn't produce any a.txt file! I'm using g++ (GCC) 4.1.0 (SUSE
Linux). I want read and write a file.
The code from cplusplus.com is wrong, as far as I can tell. Your call
to fstream::open is failing, as you can see if you call f.is_open()
afterwards. This is because "in|out|app" is not an allowed combination
of openmode flags for an fstream object.
You probably want:
f.open("a.txt",fstream::in | fstream::out | fstream::trunc);
which will create the file if it doesn't exist already, and set its
size to 0.

Oct 28 '06 #2

P: n/a

wiso wrote:
My problem is this (from:
http://www.cplusplus.com/ref/iostream/fstream/open.html)

#include <fstream>

using namespace std;

int main()
{
fstream f;
f.open("a.txt",fstream::in | fstream::out | fstream::app);
f << "out";
f.close();
return 0;
}

The program doesn't produce any a.txt file! I'm using g++ (GCC) 4.1.0 (SUSE
Linux). I want read and write a file.
Why don't you introduce a little error checking? Note the std::ios
modes.
Personally, a tutorial that ignores errors should itself be ignored.

#include <iostream>
#include <fstream>

int main()
{
std::fstream fs;
fs.open("test.txt", std::ios::out | std::ios::app);
if (!fs || !fs.good())
{
std::cout << "could not open file!\n";
return -1;
}
fs << "out" << std::endl;
if (fs.fail())
{
std::cout << "failed to append to file!\n";
return -1;
}
return 0; // no need to close file
}

Oct 28 '06 #3

P: n/a
Pete C wrote:
wiso wrote:
>My problem is this (from:
http://www.cplusplus.com/ref/iostream/fstream/open.html)

#include <fstream>

using namespace std;

int main()
{
fstream f;
f.open("a.txt",fstream::in | fstream::out | fstream::app);
f << "out";
f.close();
return 0;
}

The program doesn't produce any a.txt file! I'm using g++ (GCC) 4.1.0
(SUSE Linux). I want read and write a file.

The code from cplusplus.com is wrong, as far as I can tell. Your call
to fstream::open is failing, as you can see if you call f.is_open()
afterwards. This is because "in|out|app" is not an allowed combination
of openmode flags for an fstream object.
You probably want:
f.open("a.txt",fstream::in | fstream::out | fstream::trunc);
which will create the file if it doesn't exist already, and set its
size to 0.
ok, but how I can use a fstream with io operation? I need to write to a file
(I don't know if it exists, without rewrite it) and alse to read it. Is
there a way to do all automatically? or I have to check if the file exists,
if not, create, ...
Oct 28 '06 #4

P: n/a
wiso wrote:
ok, but how I can use a fstream with io operation? I need to write to a file
(I don't know if it exists, without rewrite it) and alse to read it. Is
there a way to do all automatically? or I have to check if the file exists,
if not, create, ...
It seems you want this behaviour:

1) If file exists, open for reading & writing without truncation
2) If it doesn't exist, create it and open it for reading & writing

Is that correct? If so, then I don't think you can do it with a single
open() call. Try this:

f.open("a.txt", fstream::in | fstream::out);
if (!f.is_open())
f.open("a.txt", fstream::in | fstream::out | fstream::trunc);

It's not ideal, and suffers from a race condition (if some other
process writes a file called "a.txt" between those two calls to open(),
then the new file will be clobbered). Maybe someone else has a better
idea?

Oct 28 '06 #5

P: n/a
Pete C wrote:
wiso wrote:
>ok, but how I can use a fstream with io operation? I need to write to a
file (I don't know if it exists, without rewrite it) and alse to read it.
Is there a way to do all automatically? or I have to check if the file
exists, if not, create, ...

It seems you want this behaviour:

1) If file exists, open for reading & writing without truncation
2) If it doesn't exist, create it and open it for reading & writing

Is that correct? If so, then I don't think you can do it with a single
open() call. Try this:

f.open("a.txt", fstream::in | fstream::out);
if (!f.is_open())
f.open("a.txt", fstream::in | fstream::out | fstream::trunc);

It's not ideal, and suffers from a race condition (if some other
process writes a file called "a.txt" between those two calls to open(),
then the new file will be clobbered). Maybe someone else has a better
idea?
the behaviour is correct, but I don't like the double open(), I can close()
the file before the second open()
Oct 28 '06 #6

P: n/a

wiso wrote:
Pete C wrote:
wiso wrote:
My problem is this (from:
http://www.cplusplus.com/ref/iostream/fstream/open.html)

#include <fstream>

using namespace std;

int main()
{
fstream f;
f.open("a.txt",fstream::in | fstream::out | fstream::app);
f << "out";
f.close();
return 0;
}

The program doesn't produce any a.txt file! I'm using g++ (GCC) 4.1.0
(SUSE Linux). I want read and write a file.
The code from cplusplus.com is wrong, as far as I can tell. Your call
to fstream::open is failing, as you can see if you call f.is_open()
afterwards. This is because "in|out|app" is not an allowed combination
of openmode flags for an fstream object.
You probably want:
f.open("a.txt",fstream::in | fstream::out | fstream::trunc);
which will create the file if it doesn't exist already, and set its
size to 0.

ok, but how I can use a fstream with io operation? I need to write to a file
(I don't know if it exists, without rewrite it) and alse to read it. Is
there a way to do all automatically? or I have to check if the file exists,
if not, create, ...
Anything "automatic" is by implication more complicated, not simpler.
If you prefer hacking a fragile program, go ahead, i'm not getting
involved.
In your case since you might not be use to coding with pos types and
seekg-ing within a file, i strongly discourage going that route. Keep
the system simple. As simple as humanly possible.
Then there is the issue of requirements. I got the append part, but
read what and store it where?
Note that the std::ios::out | std::ios::app mode combination will
create the file if non-existant and does append as expected.

I'ld suggest trying this:
a) write a class to store the filename and a vector container of
std::strings for storage
b) add a public append() function that opens the file with a
std::ofstream for output
c) add a public read() function that opens the file with a
std::ifstream for input (load vector)
d) add a public method that displays the contents of the vector

Thats a simple design. The file streams don't even need to be manually
closed.

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

class Filer
{
std::string m_sfile;
std::vector< std::string vs; // a)
public:
Filer(std::string s) : m_sfile(s), vs() { }
bool append(std::string& s) const // b)
{
// open file with std::ofstream and append s to it
// any error returns true
}
bool read() // c)
{
// open file with std::ifstream and load vector with
std::getline(...)
// any error returns true
}
void show() const // d)
{
// to std::cout, iterate through vector
}
};

int main(int argc, char* argv[])
{
Filer filer(file.txt);

if ( filer.append("whatever") ) // "whatever" could be taken from
argv[1]
{
std::cout << "error while writing to file.\n";
return -1;
}
if ( filer.read() )
{
std::cout << "error while reading file.\n";
return -1;
}
filer.show();
return 0;
}

Personally, i'ld prefer exceptions rather than passing errors, but lets
not get carried away.

Oct 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.