473,805 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ 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<<"Malfunct ion!\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 1643

"Ioannis Vranos" <iv*****@nospam .no.spamfreemai l.gra écrit dans le message
de news: g2***********@u lysses.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<<"Malfunct ion!\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.spamfreemai l.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<<"Malfunct ion!\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 objektorientier ter Datenverarbeitu ng
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::trun c", 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
3063
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 disable the annotations written to the log file, but enable the text from the newer test cases. I've searched the C++ newsgroups and the FAQ-Lite
6
3217
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
5296
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 since I will need to write and read from files, and I am using those files as binary files. I made a sample of what's going on (below). Without using vector, everything works fine, but with using vectors, something is going wrong somewhere!?? ...
1
5351
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 doesn't show in the next read, or when I open the file itself. Additionally, I get a insufficient parameters error from if I remove the ios flags from the open--from what I have read, isn't fstream supposed to default the flags to ios::in|ios::out? I...
6
17274
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
4180
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
7
10109
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 default constructor ( or 'open' method ) will not create a file. ( fstream fstr("hello.dat") or fstr.open("hello.dat") ) I read on a site, that I needed to add these flags: fstream fstr("hello.dat", ios_base::trunc | ios_base::out | ios_base::in);
6
3736
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
2028
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 applicant info in Notepad) and output a statement of acceptance or rejection for each applicant. Each input line contains the follwing information about the applicant: Applicant's ID number (0-9,999) Age (0-120) Score on the Test of Social...
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9179
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.