Sunday, July 26, 2009

What library and function do you need to open have the program open a file for you in c++?

I'm currently learning c++ and I was thinking about creating a program that opens a file for me. What is the command for opening a file in c++ and, if there needs to be a different library please include that too.





Thanks

What library and function do you need to open have the program open a file for you in c++?
Here's a simple example:





#include %26lt;iostream%26gt; // Header for input/output streams


#include %26lt;fstream%26gt; // Header for file access





using namespace std; // Use the standard namespace





void main () {


// Create a file stream


ofstream myfile;


// Open the file


myfile.open ("example.txt");


// Write to the file


myfile %26lt;%26lt; "Writing this to a file.\n";


// Close the file


myfile.close();


}
Reply:Have a look at the following headers:


%26lt;iostream%26gt;


%26lt;fstream%26gt;


%26lt;iomanip%26gt;





You should see that all sorts of methods for reading/writing files are there.





Here's afewexample portions:





ifstream inFile;


inFile.open("myFile.dat", ios::in);





if (inFile.is_open() == 0)





inFile.get( blah blah blah)

hollyhock

No comments:

Post a Comment