Cam
Administrator
[M:5000]
Posts: 6,381
|
Post by Cam on Apr 14, 2011 8:56:30 GMT
Is it possible to take the contents of one variable and save it into a txt file? Do any of you guys know how to? I'm trying to work it out and curious to know if it's possible as it would help in a little code I'm making
|
|
Richie
Elite Level 1
[M:0]
^ That's My Fugly Mug
Posts: 34
|
Post by Richie on Apr 14, 2011 14:15:09 GMT
|
|
Jordan
Elite Poster
[M:5000]
Posts: 286
|
Post by Jordan on Apr 14, 2011 22:27:50 GMT
There's more than one way to do it and it also depends on the variable that you want to print out, but here's the basic idea.
#include <fstream> // Provides you with objects and functions needed to write to and read from files
// We need this since we are using standard functions from the // fstream which are part of the standard namespace. This makes // it to where we don't have to write std:: infront of all the objects // and functions from the fstream library. Ex: std::fstream is what // we would have to write if we didn't include the standard namespace. using namespace std;
int main(int argc, char **argv) { int myVariable = 45;
// Create an output stream and open the file for writing, or create it if it // doesn't exist. ios::out is a bitmask which lets the fstream object know that // we are going to be writing to the file fstream strm("myOutputFile.txt", ios::out);
// Write to the file. Note that the << operator is overloaded and // knows how to write integers to file streams (meaning it is //converting your integer into ASCII characters for you). strm << myVariable;
// close your file strm.close();
return 0; }
|
|
Cam
Administrator
[M:5000]
Posts: 6,381
|
Post by Cam on Apr 17, 2011 0:05:33 GMT
There's more than one way to do it and it also depends on the variable that you want to print out, but here's the basic idea. #include <fstream> // Provides you with objects and functions needed to write to and read from files
// We need this since we are using standard functions from the // fstream which are part of the standard namespace. This makes // it to where we don't have to write std:: infront of all the objects // and functions from the fstream library. Ex: std::fstream is what // we would have to write if we didn't include the standard namespace. using namespace std;
int main(int argc, char **argv) { int myVariable = 45;
// Create an output stream and open the file for writing, or create it if it // doesn't exist. ios::out is a bitmask which lets the fstream object know that // we are going to be writing to the file fstream strm("myOutputFile.txt", ios::out);
// Write to the file. Note that the << operator is overloaded and // knows how to write integers to file streams (meaning it is //converting your integer into ASCII characters for you). strm << myVariable;
// close your file strm.close();
return 0; }I'll try that out soon, thanks!
|
|
Jordan
Elite Poster
[M:5000]
Posts: 286
|
Post by Jordan on Apr 17, 2011 0:44:30 GMT
Glad to help, and let me know if you have anymore questions. I've been working on a data compression program which deals a lot with reading and writing to files so most of this stuff is fresh on my mind.
|
|