fstream tests

I

Ioannis Vranos

Hi, I am experimenting with fstream type, I have written the following code:

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

int main()
{
using namespace std;

char s[200]= {};

fstream file("test.txt");

file<< "This is a test\n"<< 3*9<< "\nThis is the end\n";

file>> s;

if(!file)
cerr<<"Malfunction!\n";

cout<< s<< endl;

file<< 123<< "\n";

}


The file test.txt already exists.


The output of the program to the standard output is:

john@john-desktop:~/Projects/foobar-cpp/src$ ./foobar-cpp
Malfunction!

john@john-desktop:~/Projects/foobar-cpp/src$


The contents of test.txt are:

john@john-desktop:~/Projects/foobar-cpp/src$ cat test.txt
This is a test
27
This is the end
john@john-desktop:~/Projects/foobar-cpp/src$



Question:

Why the file object does not output to the char array?
 
E

Eric Pruneau

Ioannis Vranos said:
Hi, I am experimenting with fstream type, I have written the following
code:

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

int main()
{
using namespace std;

char s[200]= {};

fstream file("test.txt");

I bet file.is_open() returns false after this... try
fstream file("test.txt", fstream::trunc | fstream::in | fstream::eek:ut);
file<< "This is a test\n"<< 3*9<< "\nThis is the end\n";

this actually do nothing since your file is not really open, but let assume
your file is now correctly open
file>> s;

problem here, check where your get pointer is.... Most likely that your
program will crash!
From now on if you want to do read/write operations your friends gonna be
seekp, seekg functions inherited from ostream and istream
if(!file)
cerr<<"Malfunction!\n";

cout<< s<< endl;

file<< 123<< "\n";

}


The file test.txt already exists.

.... and I bet it was not created by this program
 
J

James Kanze

Hi, I am experimenting with fstream type, I have written the
following code:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
using namespace std;
char s[200]= {};
fstream file("test.txt");

You really should check to ensure that the open worked. (It
won't if the file doesn't exist.)
file<< "This is a test\n"<< 3*9<< "\nThis is the end\n";
file>> s;

And what should this read? You're positionned at the end of the
file (unless the file previously existed, and contained more
bytes than you've written). So the read should fail. In fact,
the standard says that output shall not be directly followed by
input unless there is an intervening flush or seek request, so
this input should fail in all cases.
if(!file)
cerr<<"Malfunction!\n";
cout<< s<< endl;
file<< 123<< "\n";
}
The file test.txt already exists.
The output of the program to the standard output is:
john@john-desktop:~/Projects/foobar-cpp/src$ ./foobar-cpp
Malfunction!

Which is what should be expected.
 
I

Ioannis Vranos

fstream file("test.txt", ios_base::in | ios_base::eek:ut
| ios_base::trunc);


In the above code, is it guaranteed that with the use of
"ios_base::trunc", a new file will be created under all circumstances in
which

ofstream file("test.txt", ios_base::eek:ut | ios_base::trunc);

will create a new file (e.g. when test.txt does not pre-exist)?
 

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

Similar Threads


Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top