473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

fstream problem

las
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

int main()
{
string token1 = "Hello\nall";
fstream newfile("C:/temp/test1.txt", ios::trunc);
if ( newfile.good() )
{
cout << token1.c_str() << endl << "Size of string is " <<
token1.length() << endl;
newfile.write( token1.c_str(), token1.length() );
newfile.flush();
}
else
{
cout << "Error with newfile" << endl;
}

file.close();
system("PAUSE");
return 0;
}

Why doesnt the above code write the token1 string to the test1.txt file ?
Jul 19 '05 #1
1 2695
"las" <la******@online.no> wrote in message
news:AY******************@news2.e.nsc.no...
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

int main()
{
string token1 = "Hello\nall";
fstream newfile("C:/temp/test1.txt", ios::trunc);
if ( newfile.good() )
{
cout << token1.c_str() << endl << "Size of string is " <<
token1.length() << endl;
newfile.write( token1.c_str(), token1.length() );
newfile.flush();
}
else
{
cout << "Error with newfile" << endl;
}

file.close();
system("PAUSE");
return 0;
}

Why doesnt the above code write the token1 string to the test1.txt file ?


Try the following instead:

fstream newfile("C:/temp/test1.txt", ios::out);

or:

ofstream newfile("C:/temp/test1.txt");

Jul 19 '05 #2

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

Similar topics

3
by: Frédéric Manzanares | last post by:
hello, my problem: I want to habe one Class with write and read in a file. i have overloaded the operator >> and <<. class c_File { public : fstream fs;
6
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();
3
by: David Blasdell | last post by:
This appears very strange to me, I'm having a problem with a fstream object inside a class which is not being called directly from main (another class calls the class that contains the fstream...
3
by: ratzeel | last post by:
The following snippet code throws an error while compiling on SUN os.. Any idea how to resolve this... #include <iostream.h> #include <fstream.h> #include <math.h> #include <algorithm>...
2
by: NewToCPP | last post by:
I am having some trouble including "iostream" "fstream" to my code. I tried the following ways and base times it did not work. What is the problem? test.cc: ======= ..... #include...
5
by: ehui928 | last post by:
The following program is used to open a file in mode both read and append, but it has a problem that the file can't be opened. #include <iostream> #include <fstream> #include <cstdlib> ...
6
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;
2
by: jjcp | last post by:
I would like to say thanks in advance for insight anyone can shed on this for me. Long story short from time to time I need to us C++ to take a list of file and make an index out of them in...
3
by: shyam | last post by:
Hi All I want to know if there is any problem with using fstream.getLine(char*, int ) function. My problem is that when I read a file using it, the program aborts when it reads the...
3
by: Ioannis Vranos | last post by:
Hi, I am experimenting with fstream type, I have written the following code: #include <iostream> #include <fstream> #include <string> int main() { using namespace std;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.