C plus plus - auto-mate/CheatSheetWiki GitHub Wiki

Full Reference

append to file

use ios::app

#include <iostream>
#include  <fstream>
#include <string>

using namespace std;

int main()
{

    fPathString="C:\\temp\\test.txt";

    ofstream fData(fPathString.c_str(),ios::app);
    if (fData.is_open()) {
        fData.write("Hello World\n",12);

    }

    fData.close();

    return 0;
}

constants

e.g.

const double pi=3.14159;
const char* newLine = "\n";
const string newString = "All OK SO FAR!";

include

load a library e.g.

#include <iostream>

namespace

Rather than using std::cout etc. use

using namespace std;  

now only cout is required

read from a file

#include <iostream>
#include  <fstream>
#include <string>

using namespace std;

int main()
{

    string fPathString;
    cout << "Enter File To List: ";
    cin >> fPathString;
    cout << "Listing for " << fPathString << '\n\n';


    string lineR;
    ifstream fData(fPathString.c_str());
    if (fData.is_open()) {
        while ( getline(fData,lineR) ){
            cout << lineR << '\n';
        }
    }

    fData.close();

    return 0;
}

write to file

#include <iostream>
#include  <fstream>
#include <string>

using namespace std;

int main()
{

    fPathString="C:\\temp\\test.txt";

    ofstream fData(fPathString.c_str());
    if (fData.is_open()) {
        fData.write("Hello World",11);
    }

    fData.close();
    return 0;
}

Sample Code

#include <iostream>
#include <string>

// declare namespace so std:: is not required
using namespace std;
// add some constants
const double pi=3.14159;
const char* newLine = "\n";
const string newString = "All OK SO FAR!";

int main()
{
    //Example Code;
    /*###############################################################################
       DECLARATIONS
      ###############################################################################*/
    /*
    Grouped Integer Decaration
    */
    int a,b;
    // declare and set initial value 1
    int c = 10;
    // declare and set initial value 2
    int d (20);

    // Declare String
    string localString;
    string aDeclaredString;

    /*###############################################################################
     CONSOLE OUTPUT
    ################################################################################*/
    // Output  Quoted Text with new line
    cout << "Calc..." << endl;
    // Set Value For localString
    localString = "OK";
    // Set Integer Values for a and b
    a = 7;
    b = 2;
    // Output sum directly
    cout << a+b << endl;
    // Output value directly
    cout << c << endl;
    // Output value directly plus newLine from newLine String variable
    cout << d << newLine;
    // Output string directly plus newLine from newLine String variable
    cout << localString << newLine;
    // Output constant string directly plus newLine from newLine std function
    cout << newString << endl;
    cout << "Part of a String Plus " "another part " << endl;
    /*############################################################################
       PROGRAM FLOW
      ############################################################################*/
    //if example with and && and or ||
    if ( ((1 > 0) && (1==1)) || (10<20)) {
        cout << "True Enough!" << endl;
    }
    else if (1==100)
            cout << " Pretty Unlikely you'll ever see this!";
    else
            cout << "or this!";

    // while example
    int n=10;
    while (n>0) {
        cout << n << ", ";
    --n;
    }

    // for example
    for (int n=10; n>0; n--) {
        cout << n << ", ";
    }
    
    // for example char in string
    /* NB BELOW c++11 version and above only...
    string str {"Hello!"};
    for (char c : str)
    {
        cout << "[" << c << "]";
    }

    // for example char in string

    for (auto c : str)
        cout << "[" << c << "]";

    NB ABOVE c++11 version and above only...*/

   /*###############################################################################
     CONSOLE INPUT
    ################################################################################*/
    // read a number from stdin and show it
    cout << "Enter a number to assign to [a]";
    cin >> a;
    cout << "[a] is " << a;
    // read a string from stdin and show it
    getline(cin,aDeclaredString);
    cout << aDeclaredString << newLine;
    cout << sizeof(aDeclaredString) << newLine;

    // Combine String and Numbers
    cout << "a plus b is " << a+b << ".";

    // return int Re "int main()..."
    return 0;
}
⚠️ **GitHub.com Fallback** ⚠️