-
Filesystem C++
Help me with this:
I wanted to make a program where it allows me to open and add new Data by just a console program.
Code:
#include <ofstream>
#include <iostream>
// Run
int main ()
{
// We assign the stream library to var stm
ofstream stm; // Do I need to make this a pointer???
// My vars/pointers
char FILE;
char CONTENT;
int IF_REMOV_TRUE;
// Input the file specified to open it
std::cin >> FILE;
stm.open(FILE); // Open
if (stm != NULL) // Checks to see if its not working
{
// Allows the user to add content
std::cin >> CONTENT;
stm.put(CONTENT);
}
// I was going to add remove but I need help with the current error.
std::cin >> IF_REMOV_TRUE;
cin.get();
stm.close();
return 0;
}
My Compiler comes back with this:
Cannot open include file: 'ofstream': No such file or directory
I'm using Visual Studio 08
-
Use <fstream>.
Then, remember that the ofstream class belongs to the std namespace (as does cin).
Then realize that a char is a variable that holds only a single character, so it won't hold a filename. You want std::string inside <string>. (You'll have to use FILE.c_str() to pass it as the filename to open, though.)
You should generally write only a little bit of code at a time, then compile it and make sure it does what you expect. This is too much code with too many errors to be compiled for the first time. I'd start with an empty main and add a few things at a time.
-
Incidentally, this thread is continued at Evil. You may wish to read it for further insight.