473,320 Members | 2,012 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,320 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 3561

"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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.