By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

seemingly simple ofstream problem please help

P: n/a
Hi all, I cant believe that Im stuck on such simple code, but I am
so...
Im trying to open a text file for writing but can never get the file
to initially open. Heres what Im having trouble with.

-----------------------------------------------
#include <iostream.h>
#include <fstream.h>

void main()
{
ofstream file ("C:\inventory\example.txt")
if (!file.is_open)cout<<"not open";
}
------------------------------------------------
Ive tried ios::out, ios::app, etc... but I just keep getting "not
open" in the console. Its been 3 years since I learned and used
Visual C++ and I just picked it up again yesterday, so am I forgetting
something? Any help would be greatly appreciated. Thanks!
Tom Johnson
Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Tom Johnson" <to******@yahoo.com> wrote in message
news:2d**************************@posting.google.c om...
Hi all, I cant believe that Im stuck on such simple code, but I am
so...
Im trying to open a text file for writing but can never get the file
to initially open. Heres what Im having trouble with.

-----------------------------------------------
#include <iostream.h>
#include <fstream.h>

void main()
{
ofstream file ("C:\inventory\example.txt")


ofstream file ("C:\\inventory\\example.txt")

Easy mistake to make.

john
Jul 19 '05 #2

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:bh***********@ID-196037.news.uni-berlin.de...

"Tom Johnson" <to******@yahoo.com> wrote in message
news:2d**************************@posting.google.c om...
Hi all, I cant believe that Im stuck on such simple code, but I am
so...
Im trying to open a text file for writing but can never get the file
to initially open. Heres what Im having trouble with.

-----------------------------------------------
#include <iostream.h>
#include <fstream.h>

void main()
{
ofstream file ("C:\inventory\example.txt")


ofstream file ("C:\\inventory\\example.txt")

Easy mistake to make.

john


Incidentally there are several other things wrong with your code, I'm sure
it because you've been away for three years.

1) There are no header files called <fstream.h> and <iostream.h>, the
correct header files are <iostream> and <fstream>. The .h versions are
probably implementing a non-standard legacy version of the iostream library
(I'm taking a wild guess at your compiler being VC++). You should switch to
the standard library.

2) ofstream, cout and similar standard names are in the std namespace.

std::ofstream file ("C:\\inventory\\example.txt");

std::cout << "not open";

3) main returns an int, not void.

john
Jul 19 '05 #3

P: n/a
Tom Johnson wrote:
Hi all, I cant believe that Im stuck on such simple code, but I am
so...
Im trying to open a text file for writing but can never get the file
to initially open. Heres what Im having trouble with.

-----------------------------------------------
#include <iostream.h>
#include <fstream.h>

void main()
{
ofstream file ("C:\inventory\example.txt")
if (!file.is_open)cout<<"not open";
}
------------------------------------------------
Ive tried ios::out, ios::app, etc... but I just keep getting "not
open" in the console. Its been 3 years since I learned and used
Visual C++ and I just picked it up again yesterday, so am I forgetting
something? Any help would be greatly appreciated. Thanks!
Tom Johnson

#include <iostream>
#include <fstream>

int main()
{
std::ofstream file ("C:\\inventory\\example.txt");
if (!file.is_open()) std::cout << "not open" << std::endl;
return 0;
}
Jul 19 '05 #4

P: n/a
red floyd wrote:
Tom Johnson wrote:
[redacted]

#include <iostream>
#include <fstream>

int main()
{
std::ofstream file ("C:\\inventory\\example.txt");
if (!file.is_open()) std::cout << "not open" << std::endl;
return 0;
}


Actually, that second line in main() should just read:

if (!file) std::cout << ...
Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.