473,624 Members | 2,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

wrong about fstream file(s.c_str(), std::ios_base:: in | std::ios_base:: app);

//error happen even when there existe a a.txt file
#include<fstrea m>
#include <string>
#include <iostream>
using std::string;
using std::fstream;
int main()
{
string s = "a.txt";
fstream file(s.c_str(), std::ios_base:: in | std::ios_base:: app);
if(!file)
{
std::cerr<<"err or happen\n";
}
else
{
string readToStr;
file>>readToStr ;
file<<"abc";
std::cout<<"suc cessed"<<std::e ndl;
}
}

what's the problem is ?
thanks very much.

Apr 6 '06 #1
3 3574

"DaVinci" <ap***********@ gmail.com> skrev i meddelandet
news:11******** *************@j 33g2000cwa.goog legroups.com...
//error happen even when there existe a a.txt file
#include<fstrea m>
#include <string>
#include <iostream>
using std::string;
using std::fstream;
int main()
{
string s = "a.txt";
fstream file(s.c_str(), std::ios_base:: in | std::ios_base:: app);
if(!file)
{
std::cerr<<"err or happen\n";
}
else
{
string readToStr;
file>>readToStr ;
file<<"abc";
std::cout<<"suc cessed"<<std::e ndl;
}
}

what's the problem is ?
thanks very much.


In what directory do you expect the file a.txt to be found? How does
the program know that?
Bo Persson

Apr 6 '06 #2

Bo Persson wrote:
"DaVinci" <ap***********@ gmail.com> skrev i meddelandet
news:11******** *************@j 33g2000cwa.goog legroups.com...
//error happen even when there existe a a.txt file
#include<fstrea m>
#include <string>
#include <iostream>
using std::string;
using std::fstream;
int main()
{
string s = "a.txt";
fstream file(s.c_str(), std::ios_base:: in | std::ios_base:: app);
if(!file)
{
std::cerr<<"err or happen\n";
}
else
{
string readToStr;
file>>readToStr ;
file<<"abc";
std::cout<<"suc cessed"<<std::e ndl;
}
}

what's the problem is ?
thanks very much.

In what directory do you expect the file a.txt to be found? How does
the program know that?

the current working directory.

Bo Persson


Apr 7 '06 #3
DaVinci wrote:
//error happen even when there existe a a.txt file
#include<fstrea m>
#include <string>
#include <iostream>
using std::string;
using std::fstream;
int main()
{
string s = "a.txt";
fstream file(s.c_str(), std::ios_base:: in | std::ios_base:: app);
if(!file)
{
std::cerr<<"err or happen\n";
}
else
{
string readToStr;
file>>readToStr ;
file<<"abc";
std::cout<<"suc cessed"<<std::e ndl;
}
}

what's the problem is ?


Are you sure you have the permissions to open this file
for read/write? IOW, the existence of the file does not
automatically imply you will manage to open it successfully.

HTH,
- J.
Apr 9 '06 #4

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

Similar topics

7
25573
by: beliavsky | last post by:
Ideally, one can use someone's C++ code by just looking at the header files (which should contain comments describing the functions in addition to function definitions), without access to the full source code. Can analogs of C++ header files be created for Python code? Python "header" files could list only the 'def' statements and docstrings of Python functions and classes, but that does not tell you what the functions return. One could...
8
9843
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out | ios::binary) to declare a file object with read/write modes turned on for working with binary data. I've tried this and my file is not created. The only time it is created is when I specify ifstream or ofstream but not fstream. I've tried removing the...
7
2864
by: Computer Whizz | last post by:
Hi, I was just wondering if someone would like to comment on these two issues. I had a 15 minute wander around some sites and was curious about loading files (plain ASCII I think will do for a beginner right now). So I looked into the fstream library file. I saved 5 lines to the file: testing, testing this is
1
1629
by: Marcin P | last post by:
When you build a 24-bit BMP file (without compression), its size should be: width*height*bitperpixel+54bytes (54 bytes from file and bitmap headers). When I create BMP files in MS Paint in "non-standard" dimensions (different than 1024x768 or 512x384) sizes of files are bigger than they sholud be. When dimensions are standard everything is OK. Example from Paint: white picture->173x14->save as 24bit BMP...
4
12794
by: Erpman | last post by:
I am trying to access the data with in a wav file. I am testing with very small files in order to keep the code simple to start with. Basically, im writing the entire wav file to a byte using a fileStream. The problem is that when reading back the data word by word and printing the results on a general form, once the data chunk header has been read, i.e, "data" and "chunk size", all the rest of the bytes in the array have the value...
3
7107
by: Catweasel | last post by:
I'm new to C++ and have been chucked in at the deep-end. I have a C++ console app that works fine. All I want to do is write to file however as soon as I include the fstream library and try to compile VS .net chucks out errors, lots of them: c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\cstdio(17) : error C2143: syntax error : missing '{' before ':' c:\Program Files\Microsoft Visual Studio .NET
4
1701
by: Agos | last post by:
I'm trying to learn VC++.NET. I used till now Borland C++ Builder 6. I had a program starting like this: #include <fstream.h> #define TEMP_FILES 200 ...... ....... fstream chunk_file; With Borland and g++ I had no problems with these lines instead VC++.NET
5
7073
by: Roshawn Dawson | last post by:
Hi, Has anybody created an entire asp.net app using only ashx files? I know that they are simply handlers used by the asp.net worker process. I hear that they are in some respects better than using aspx files because they don't have to be compiled and don't need any page processing. As an experiment, I'm creating an affiliate site that will use ashx files mainly. During testing I haven't noticed anything different other than it seems...
9
5284
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!?? ...
6
3728
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="";
0
8238
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
8680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8624
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
8478
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7164
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...
0
5565
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1786
muto222
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.