Question:
Problem using c++ to open files!! 10pts!?
anonymous
2009-02-21 21:05:08 UTC
I am new to programming. I am using dev c++ as a compiler. Everything has worked so far, but I tride following a tutorial online that opens a text file and puts some text in it and it didn't do anything. It executes but no file is created or opened. I am stumped. I don't seem to find anyone else having this problem on the internet. I copied the code right into my compiler and it still didn't work. Here it is:

// basic file operations
#include
#include
using namespace std;

int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
Any help would be great! THanks;
Three answers:
anonymous
2009-02-21 21:13:44 UTC
The find is Created

And you are WRITING to the file NOT to the screen

you will see it if you go

to the directory weere is your programs and find the example.txt

with wirting this to a file inside it



Edit:

Remember C++ use / not \

ofstream out("C:/Location/SubLocation/lol.txt");
JoelKatz
2009-02-22 05:12:38 UTC
Just to make sure you are correctly compiling it, deliberately create some illegal code and make sure you get a compiler error. If you change 'myfile' to 'xmyfile' in just one of the lines, and it still does nothing, then you're not compiling it at all.



My guess is that you're not actually compiling the code.
Ali
2009-02-22 05:14:04 UTC
You need to compile the code inorder to make it work!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...