Passing by REFERRENCE or VALUE
Passing by REFERRENCE or VALUE
// X: Water // X: Water
// Y: Kool-Aid // Y: Kool-Aid
---------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
using namespace std;
int main(){
fstream newFile;
newFile.open("CaitlynKiramman.txt", ios::out); // write text
if(newFile.is_open()){
newFile << "Caitlyn Kiramman, I love you so much, you are the most
gorgeous girl I have ever laid my eyes on.\nPlease forgive Vi and come back to
her! She needs you!";
newFile.close();
}
newFile.open("CaitlynKiramman.txt", ios::app); // append text
if(newFile.is_open()){
newFile << "This is an append text\n";
newFile << "Vi is so adorable <3 pls Cait why did you do that!\n";
newFile.close();
}
system("pause>0");
}
Read a txt file using <fstream> library
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
fstream newFile;
newFile.open("CaitlynKiramman.txt", ios::in); // read file in console
if(newFile.is_open()){
string line;
while(getline(newFile, line)){
cout << line << endl;
}
newFile.close();
}
system("pause>0");
}
ofstream → writing to files (output)
ifstream → reading from files (input)
#include <iostream>
#include <fstream>
int main(){
char arr[100];
std::cout<<"Enter something about Caitlyn and Violet: ";
std::cin.getline(arr,696);
std::ofstream CaitVi("CaitVi.txt");
CaitVi<<arr;
CaitVi.close();
std::cout<<"\nFile write successfully"<<std::endl<<std::endl;
The other two standard streams cerr and clog are used to display messages when errors occur. Error
messages are displayed on screen even if standard output has been redirected to a file.
FUNCTION POINTERS
#include <iostream>
int Add(int a,int b){
return(a+b);
}
int main(){
int (*p)(int,int); // Declare a function pointer
p = &Add; // Assign pointer p to function Add (no ‘&’ is the same so its
okay)-> p = Add also works
int c;
c = (*p)(2,3); // Dereferencing and executing the function (p)(2,3) also
works
std::cout<<c;
// Ouput is 5
return 0;
}
#include <iostream>
void printHello(){
std::cout<<"hello bitches!";
}
int main(){
void (*pPrintHello)();
pPrintHello = &printHello;
pPrintHello();
}