473,401 Members | 2,146 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,401 software developers and data experts.

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<fstream>
#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<<"error happen\n";
}
else
{
string readToStr;
file>>readToStr;
file<<"abc";
std::cout<<"successed"<<std::endl;
}
}

what's the problem is ?
thanks very much.

Apr 6 '06 #1
3 3565

"DaVinci" <ap***********@gmail.com> skrev i meddelandet
news:11*********************@j33g2000cwa.googlegro ups.com...
//error happen even when there existe a a.txt file
#include<fstream>
#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<<"error happen\n";
}
else
{
string readToStr;
file>>readToStr;
file<<"abc";
std::cout<<"successed"<<std::endl;
}
}

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*********************@j33g2000cwa.googlegro ups.com...
//error happen even when there existe a a.txt file
#include<fstream>
#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<<"error happen\n";
}
else
{
string readToStr;
file>>readToStr;
file<<"abc";
std::cout<<"successed"<<std::endl;
}
}

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<fstream>
#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<<"error happen\n";
}
else
{
string readToStr;
file>>readToStr;
file<<"abc";
std::cout<<"successed"<<std::endl;
}
}

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
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...
8
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 |...
7
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...
1
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...
4
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...
3
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...
4
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...
5
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...
9
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...
6
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
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,...
0
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...

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.