smth wrong

I am using DEV-C++ on my windows PC;
When i try to run this simple program, it says
([Error] iostream: No such file or directory
compilation terminated)

i dont know what does it mean? anyone please tell me?


// writing on a text file
#include <iostream>
#include <fstream>
using namespace std;

int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Limitless98

Try it this way..
1
2
ofstream myfile;
myfile.open ("example.txt");

@whitenite1

I'm sorry, I'm really new to this, still trying to read up on data files and input/output on files;

do you mean like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// writing on a text file
#include <iostream>
#include <fstream>
using namespace std;

int main ()
{
  ofstream myfile;
  myfile.open ("example.txt");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}


it still shows the same error message
Last edited on
You must a problem elsewhere, and not with the program, as I copied it, compiled it and ran the code in MS Visual Studio, with no problems. Maybe you have to put a '.h' after fstream, [ example <fstream.h> ] especially if it's an older version of Dev-C++. Anyway, try that and see if it helps. Get back to me if it doesn't, and I'll try putting it in my Dev-C++
i tried putting '.h' after fstream like you said, it's still showing error.
maybe its because of my software. which one do you use? maybe I'll try that.
1
2
3
4
I am using DEV-C++ on my windows PC;
When i try to run this simple program, it says
([Error] iostream: No such file or directory
compilation terminated)

This looks like something is wrong with the configuration or installation of DevC++.

I'd suggest re-installing the Orwell version of Dev C++.
http://orwelldevcpp.blogspot.co.uk/

A direct link to the download of IDE including the compiler is
http://sourceforge.net/projects/orwelldevcpp/files/Setup%20Releases/Dev-Cpp%205.11%20TDM-GCC%204.9.2%20Setup.exe/download

(this is getting a little dated now, but is still adequate for learning the basics).




Yeah!
i figured out the problem, nothing wrong with the program. it was me, i saved the codes as a 'C source file', tried saving it as 'C++ source file' that was no problem.
Another thing is, i just started working with data files. Currently trying to do something that involves data files. As in, creating a text file; writing in the text file; reading from it; editing it;I would really appreciate if anyone could give me something that i can read up on that can help me with this.
Thanks a lot.
Currently trying to do something that involves data files. As in, creating a text file; writing in the text file; reading from it; editing it;

The basics are covered in the tutorial:
http://www.cplusplus.com/doc/tutorial/basic_io/

http://www.cplusplus.com/doc/tutorial/files/

As for editing a file, my advice is to read the entire file into the program variables. Depending on how large/complex it is, you may use an array or better still a vector. Make the changes to the in-memory version. Then write it back to the same file when you're finished.
Topic archived. No new replies allowed.