C++ Main: Booooyyyyyy!
C++ File Input/Output.
----------------------
By: Cyrax88 / Jcrypt [ jcrypt@gmail.com ]
--------------------------------
##########################################
#
File I/O in C++ is as simple as ( cin & cout ) if you can do basic input
and basic output then File I/O shall be a sinch with a little bit of practise.. We'll start with the header that's needed if your reading this i'd recommend you have down the basics of C++ at least... Otherwise this will seem like a bunch of gibberish.. Now the header that will be used for this tutorial will be <fstream> this holds all the predefined functions etc.. we will be using so you basic layout shall look as the following..
##########################################
#
#include <fstream>
using namespace std;
int main{
/*Stuff we'll learn about later*/
return 0;
}
##########################################
#
Now, with this said we can go onto learning about the actual I/O part. Now it's as simple as..
##########################################
#
ofstream fileName("fileToCreate.txt");
/* This will basically work as your output.. Like writing too etc... */
And finally for the input....
ifstream fileName("fileToRead.txt");
/* This will serve as a file "reader" persay so that writing to an existing file will be a breeze */
##########################################
#
Now to go on form what we've learned we are going to vreate a file named "theTut" and we will write "Hello World" to it!...
Each piece i will try to comment as good as possible so sorry for the spacing on the coding this is designed to learn from...
##########################################
#
#include <iostream>
/* Header we use for cin and cout */
#include <fstream>
/* Header we need for File I/O */
using namespace std;
/* This i will go int-depth on in another tutorial */
int main(){
/* Call the basic 'starting point' for the program' */
ofstream myFile("theTut.txt");
/* Remember we are gonna be creating this */
if(myFile.is_open()){
/* Make sure it was opened else give us a error! */
myFile << "Hello, World!\n";
/* Used myFile for the text to be printed to in place of the ( cout ) function. */
myFile.close();
/* Closes the file for us */
cin.get();
/* Basically keeps window open until the enter key is pressed. */
return 0;
/* Make sure the program ends fine. */
}
/* Close the if( )statement */
}
/* Closing bracket we are done! */
##########################################
#
/* Code when formatted correctly */
#include <fstream>
#include <iostream>
using namespace std;
int main(){
ofstream myFile("theTut.txt");
if(myFile.is_open()){
myFile << "Hello, World!\n";
myFile.close();
cin.get();
return 0;
}
}
######################################
Simple, eh!? Now to tackle another aspect of FILE I/O.... input with variables....
######################################
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char myName[] = "Jcrypt";
int age = 17;
ofstream myFile("theTut.txt");
if(myFile.is_open()){
myFile << "My Name: " << myName;
myFile << "\nMy Age: " << age;
myFile.close();
}
cin.get();
return 0;
}
######################################
In the past example, I set basic variables myName, and age. And as mentioned earlier I wrote the values to the file as I would a standard output. To show you exactlly what I meant look how similar these are.
######################################
cout << "My age: " << age << endl;
and...
myFile << "My age: " << age << "\n";
######################################
More to come... in part#2 (sorry outta time at the moment....)