# File IO - Tutorial is wrong?

This is a discussion on File IO - Tutorial is wrong? within the C++ Programming forums, part of the General Programming Boards category; I was checking through the tutorials (again), and I got to lesson #10 where it says: Code: #include <fstream.h> #include ...

1. ## File IO - Tutorial is wrong?

I was checking through the tutorials (again), and I got to lesson #10 where it says:

Code:
#include <fstream.h>
#include <iostream.h>
int main()
{
char str[10];
//Used later
ofstream a_file("example.txt");
//Creates an instance of ofstream, and opens example.txt
a_file<<"This text will now be inside of example.txt";
//Outputs to example.txt through a_file
a_file.close();
//Closes up the file
ifstream b_file("example.txt");
//Opens for reading the file
b_file>>str;
//Reads one string from the file
cout<<str;
//Should output 'this'
b_file.close();
//Do not forget this!
}
The thing is that this doesn't create a file called "example.txt" anywhere...
Is it just my comp, or what?

2. It worked for me. The file is in the same folder as the
executable. If you want the file elsewhere put in the full path
in the code.

3. Aargh! It doesn't seem to work...

I am using Dev-C++ on a WinXP comp.

---------------------------------------------
Wait, so if I wanted to create a file inside another folder, I use:
Code:
ofstream some_file("C:\damn\foo\bar.txt");

4. I used this code:
Code:
#include <fstream.h>
#include <iostream.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

int main()
{
char str[10];
//Used later
ofstream a_file("example.txt");
if ( !a_file ) {
cerr << "oops " << strerror(errno) << endl;
system("pause");
return 1;
}
else
{
cout << "Foo" << endl;
system("pause");
return 0;
}
}
And it output: Foo

Ok, I forgot the escapes, but I was not really being very specific.

5. instead of:
Code:
ofstream a_file("example.txt");
try:
Code:
ofstream a_file;
a_file.open("example.txt",ios::trunc);
...
a_file.close();
it shouldn't make any difference, but maybe your compiler is playing tricks with your head... maybe closing the file will help...

6. I doubt this is the problem, but with your compiler it might be necessary for you to explicitly enable output to the file by setting the ios::out bit when opening the file. Try this code:

Code:
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char** argv) {

ofstream MyFile;

//Open file
MyFile.open("example.txt", ostream::out);

if(MyFile.fail()) {
cerr <<"Sorry, I can't open that file for output\n";
return 0;
}

//Write to file
MyFile <<"This should be inside of example.txt" <<endl;

//Close file
MyFile.close();
}
That should create the file and write to it. If it doesn't, it should say it couldn't open the file. Hope that helps

7. You will probably find example.txt in your Dev-C++ directory ... i.e. C:\program files\Dev-C++

To specify a complete path type it this way:

ofstream a_file("c:/cfiles/scrap/example.txt"); // note - forward slashes

Popular pages Recent additions