how to make a console program open a file

Mar 22, 2011 at 8:45pm
I need to know how to how to make console program open a file
like a sound file.

do you have eny ideas?
Last edited on Mar 22, 2011 at 8:47pm
Mar 22, 2011 at 8:46pm
Use the <fstream> header file from the C++ standard library.

http://cplusplus.com/doc/tutorial/files/
Mar 22, 2011 at 8:50pm
so the fstream is a fiel open header?
Last edited on Mar 26, 2011 at 1:11am
Mar 22, 2011 at 8:53pm
Yes. If you read the documentation link I gave you it will show you how to do it.
Mar 24, 2011 at 12:26am
ok
Mar 24, 2011 at 12:31am
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}
Mar 24, 2011 at 6:50am
That's exactly right. However, I suggest between opening the file and writing to it, you use std::ofstream::fail() to check that the file has opened correctly.
Topic archived. No new replies allowed.