473,480 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

fstream tests

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?
Jun 27 '08 #1
3 1620

"Ioannis Vranos" <iv*****@nospam.no.spamfreemail.gra écrit dans le message
de news: g2***********@ulysses.noc.ntua.gr...
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::out);
>
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

--------------------

Eric Pruneau
Jun 27 '08 #2
On Jun 10, 2:17 pm, Ioannis Vranos <ivra...@nospam.no.spamfreemail.gr>
wrote:
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #3
fstream file("test.txt", ios_base::in | ios_base::out
| 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::out | ios_base::trunc);

will create a new file (e.g. when test.txt does not pre-exist)?
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3035
by: Thomas Matthews | last post by:
Hi, My objective is to add enable & disable functionality to the ofstream class. I want to have a log file where I can disable and enable output to it. When my first tests pass, I want to...
6
3198
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
9
5265
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
1
5328
by: MForey | last post by:
I'm attempting to create a program that uses fstream objects to read/write to files. However, it is currently balky at best. The fstream.write call doesn't return an error, but the modified data...
6
17088
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
5
4143
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
7
9917
by: Soneji | last post by:
*sigh* Ok, I have two questions. First, my setup: Win-doze XP-SP2; & Dev-C++ 4.9.9.2; I recently started trying to use 'fstream' to work my input/output, and I noticed that using the...
6
3718
by: Gaijinco | last post by:
Should this do something? #include <fstream> #include <string> int main() { std::fstream filestr ("test.txt", std::fstream::in | std::fstream::out); std::string s="";
16
1991
by: HypeBeast McStreetwear | last post by:
Hey Guys I have to make a program where it accepts/denies applications into a club. The program should read in information about applicants (None was given so I believe I have to make up some...
0
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7044
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4779
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.