seemingly simple ofstream problem please help

T

Tom Johnson

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::eek:ut, 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
 
J

John Harrison

Tom Johnson said:
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
 
J

John Harrison

John Harrison said:
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::eek:fstream file ("C:\\inventory\\example.txt");

std::cout << "not open";

3) main returns an int, not void.

john
 
R

red floyd

Tom said:
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::eek:ut, 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::eek:fstream file ("C:\\inventory\\example.txt");
if (!file.is_open()) std::cout << "not open" << std::endl;
return 0;
}
 
R

red floyd

red said:
Tom Johnson wrote:
[redacted]


#include <iostream>
#include <fstream>

int main()
{
std::eek:fstream 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 << ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top