fstream doesn't write

P

Pete C

wiso said:
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::eek:ut | 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::eek:pen 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::eek:ut | fstream::trunc);
which will create the file if it doesn't exist already, and set its
size to 0.
 
S

Salt_Peter

wiso said:
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::eek:ut | 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::eek:ut | 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
}
 
W

wiso

Pete said:
The code from cplusplus.com is wrong, as far as I can tell. Your call
to fstream::eek:pen 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::eek:ut | 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, ...
 
P

Pete C

wiso said:
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::eek:ut);
if (!f.is_open())
f.open("a.txt", fstream::in | fstream::eek:ut | 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?
 
W

wiso

Pete said:
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::eek:ut);
if (!f.is_open())
f.open("a.txt", fstream::in | fstream::eek:ut | 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()
 
S

Salt_Peter

wiso said:
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::eek:ut | 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::eek:fstream 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::eek:fstream 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top