Helloouuu! my gorgeous friends!(*inspired by Dev Ed*). Today’s Snippet is the first program in which I handle the creation of a file and reading its content in C++. This could easily be used to create a log system in your application or to create PDFs containing data coming from your database such as reports, invoices, etc.

Here it is the code that handles the file creation, it also contains some comments in the main function to make it more clear to understand; the two functions created such as the arrayBank & passToFile are meant to be self-explanatory. If you want to know more about arrays, I recommend you to check my previous snippet in which I talk about the differences between arrays and vectors to create a Income Calculator. The code, it’s finally here:

// Kevin. Array vs Vector
// Income Calculator using C++ v.11 10/29/2019
#include <iostream>
#include <algorithm> // sort && reverse
#include <fstream>

using namespace std;

// Prototypes
void arrayBank(int array[], int sizeOfArray, string name);
void passToFile(int array[], int sizeOfArray, string name);


// Definitions
void arrayBank(int arrayGrades[], int sizeOfArray, string name) {
  
  sort(arrayGrades, arrayGrades+sizeOfArray); // Sort it 
  reverse(arrayGrades, arrayGrades+sizeOfArray); // Reverse it
  cout << "\nArray Output" << endl;
  for(int i = 0; i < sizeOfArray; i++){
    cout << arrayGrades[i] << endl;
    passToFile(arrayGrades, sizeOfArray, name);
  }

};

void passToFile(int arrayGrades[], int sizeOfArray, string name){
  
  // Instantiate ofstream
  ofstream myfile;

  // Open output.txt file by using ofstream.open();
  myfile.open ("output.txt");

  // Output name and pass it to output.txt file
  myfile << name;

  // Iterate throught the integers received and put them into the output.txt file
  for(int count = 0; count < sizeOfArray; count ++){
        myfile << arrayGrades[count] << " " ;
  }

  // Close functionality of fstream
  myfile.close();
}

int main() {

  // Declare and Initialize user input to hold the value between 0-100
  int option = 0;
  
  // Define array size since array don't know its size. I highly recommend using vectors.
  // I will re-write this program later on to make it work with vectors and will post it in my blog.
  int userArray[99];

  // Declare and Initialize user input name to hold your name value
  string name = "";

  // Display direction and receive input
  cout << "Please type an integer between 0-100 [space] name" << endl; cin >> option;
  
  // Display direction and receive input
  cout << "Please enter your name: "; cin >> name;
  name = name + " ";

  // Display direction containing specified quantity of inegers desired and receive input (next for-loop)
  cout << "Please continue by entering the " << option <<  " numbers desired:" << endl;
  
  // Add user input to userArray
  for(int i = 0; i < option; i++ ){ cin >> userArray[i];
  }

  // Pass the just created userArray, its defined size and the string name into the function arrayBank
  arrayBank( userArray ,option, name);

  // Display message
  cout << "Printed from the output.txt file: " << endl;

  // Instantiate fstream object
  fstream infile;
  int i = 0;
  
  // Open fstream object
  infile.open("output.txt");

  // Print content from fstream object
  string qword;
  while(!infile.eof()){
    if(i <= option){ infile >> qword;
      cout << qword << endl;
    }
    i++;
  }
  
  // close
  infile.close();

  return 0;
}

Remember to play with it at my repl.it! If you want to know more about C++, check the snippets that I posted before; they’re very straightforward and easy to follow. You can learn more about handling files here.

Leave a Reply

Back to Top