473,385 Members | 1,707 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,385 software developers and data experts.

seemingly simple ofstream problem please help

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
4 2893

"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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marina | last post by:
I get an "access violation" when I use someting like this: @@@@@@@@@@@@@@ string tempo; const char *output; vector <ofstream> outs(3); .... .... open_output=(const char *)tempo.c_str();...
2
by: slyphiad | last post by:
i'm kinda new at c++ so be patient ^_^ i was just wondering if u guys could help me to solve this problem that i had. i'm trying to create 5 sequential files using ofstream. this is what i...
15
by: keweiming | last post by:
I have a project which needs to open hundreds to thousands of files for writing. The following is a simplified test program I wrote to see if I can use a map<string, ofstream> object to keep the...
1
by: tvn007 | last post by:
could someone please help me to point out what wrong with the code below ? #define OUPUT_DIR "/home/user/OUT/test.out" if (appenndmode=="YES"){ ofstream out(OUTPUT_DIR,ios::app); //error:...
1
by: ictheion | last post by:
Hi, I am having a problem utilizing ofstream*'s stored in a map (map<char, ofstream*>). I'm having basically the same problem with a map<char, boost::mutex*>. My code compiles, but at run-time I...
3
by: magix | last post by:
Hi, I got following to savefile into Output.txt, but how can I specify it to be in specific folder/directory (not at the same directory as the executables? Currently, it will generate the...
11
by: Kush | last post by:
Hello All, I am having problem writing some data to file and I do not understand my mistake / misconception. I am trying to write some simple text to file but the text is rather output to...
15
by: aaragon | last post by:
Hello, does anyone have a clue about this error? and how to solve it? It seems to be trivial to me, but not for the compiler. I'm using g++ 4.2 on an Ubuntu Linux system: // main() .......
1
by: jerry | last post by:
i have written a simple phonebook program,i'll show you some of the codes,the program's head file is member.h . i suppose the head file works well.so i don't post it. here's the clips of main...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.