Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > seemingly simple ofstream problem please help

Reply
Thread Tools

seemingly simple ofstream problem please help

 
 
Tom Johnson
Guest
Posts: n/a
 
      08-15-2003
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: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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-15-2003

"Tom Johnson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-15-2003

"John Harrison" <(E-Mail Removed)> wrote in message
news:bhis2h$21n0$(E-Mail Removed)-berlin.de...
>
> "Tom Johnson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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:fstream file ("C:\\inventory\\example.txt");

std::cout << "not open";

3) main returns an int, not void.

john


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      08-15-2003
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: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:fstream file ("C:\\inventory\\example.txt");
if (!file.is_open()) std::cout << "not open" << std::endl;
return 0;
}


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      08-15-2003
red floyd wrote:

> Tom Johnson wrote:
>

[redacted]
>
>
> #include <iostream>
> #include <fstream>
>
> int main()
> {
> std: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 << ...


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need help with this seemingly simple homework problem! ThePacific Java 0 09-26-2012 12:12 AM
Re: seemingly simple list indexing problem Guilherme Polo Python 3 07-30-2008 09:20 PM
Re: seemingly simple list indexing problem John Krukoff Python 6 07-30-2008 08:50 PM
Seemingly Simple issue, and yet a mystery! Please help... W.Sh Javascript 5 02-11-2006 01:22 PM
ofstream * vs. ofstream Squid Seven C++ 5 07-14-2005 07:34 AM



Advertisments