Spartan025's - C++ Files
Posted: Sun Mar 18, 2007 12:22 am
Ok i have not visited or posted in a long time,like anybody cares 
Part I:Ofstream
This section deals with manipulating text files in c++.(This is not how to open Halo maps though, thats binary. That is for another section.)
first you need to include a C++ preproceesor drive called
meaning file stream.
there are 2 main file stream objects(there is another one but these two
are the basics)
ofstream - OutputFilestream
ifstream - InputFilestream
ofstream as you might guess produces files
ifstream reads files.
These objects are mainly for text files and not complicated binary files, such as Halo's .map binary file. Binary files are harder than text and is easier to start with text.
Example of a file being made into text
If you read the comments it is pretty self explanitory.
Tips on ofstream:
In large programs, put these as functions.
Example,(true story) Im in 8th grade, and a few months ago is used my C++ skills to help me on my quadratic formulas. I made a program that outputted the right answer and saved into a text file with work shown. I had two functions, Save and Quadratic and main(always included).
The point is in big programs put things in functions if you know how and use infinite loops and something to terminate the infinite loop.
I tried my best to explain this, hope it helped. Im basically turning all the notes i took and turned it into a post.

Part I:Ofstream
This section deals with manipulating text files in c++.(This is not how to open Halo maps though, thats binary. That is for another section.)
first you need to include a C++ preproceesor drive called
Code: Select all
#include <fstream>
there are 2 main file stream objects(there is another one but these two
are the basics)
ofstream - OutputFilestream
ifstream - InputFilestream
ofstream as you might guess produces files
ifstream reads files.
These objects are mainly for text files and not complicated binary files, such as Halo's .map binary file. Binary files are harder than text and is easier to start with text.
Example of a file being made into text
Code: Select all
#include <iostream>
#include <fstream>
#include <string.h>
// Here I included the necessary preprocessor drives for the
// C++ library
using namespace std; // Here is the necessary namespace
int main() {
char filename[81];
cout << "Enter the filename and press ENTER: ";
cin.getline(filename, 81);
//Char filename is the name of the txt that will be produced
ofstream save_file_to_txt(filename);
// Create and object called save_file_to_txt
// you can choose any name for your object
//------------------------------------------------------------
// Use the save_file_to_txt object like cout to make messages
// in the txt file
// write text to file, use the object like cout once again.
save_file_to_txt << Halo is awesome << endl;
save_file_to_txt.close()
// Close the file
system("PAUSE");
return 0;
}
Tips on ofstream:
In large programs, put these as functions.
Example,(true story) Im in 8th grade, and a few months ago is used my C++ skills to help me on my quadratic formulas. I made a program that outputted the right answer and saved into a text file with work shown. I had two functions, Save and Quadratic and main(always included).
The point is in big programs put things in functions if you know how and use infinite loops and something to terminate the infinite loop.
I tried my best to explain this, hope it helped. Im basically turning all the notes i took and turned it into a post.