C++ read file into char array

·

3 min read

The only way to learn how to retrieve/access file on C++ is to write C++ programs. More practice leads to perfection. On below there is a short description regarding accessing files in C++. It is very easy but to be more familiar with the idea, do not forget to write and write the code.

Image description

ARRAY in C++?

An array is a serious group of memory locations. This memory locations have similar name and type. The index is specified by square brackets, [ ]. The first index is 0. Array is written as follows:

type variable_name[size];

  • type: base type of the array, determines the data type of each element in the array

  • size: how many elements the array will hold

  • variable_name: the name of the array

Below is an example how to define and array .

char try[10];// defined as array of 10 chars

int try[] = {1,2,3,4}; // defined as array of 4 int

vector coordinates[3][3] =

{{0, 0, 0},

{1, 0, 5},

{4, 7, 9}};// defined as array of 3 * 3 vector

What is character array in C++?

An array only with characters is called character array. C++ allows character arrays to take on certain characteristics, properties and functionality when arrays are declared or initialized in particular ways. We can define char array in different ways. But the below shown two ways are very common:

char array1[7] = {‘y’,’e’,’s’}; this array type contains three specified characters. After the three specified characters junk left in the memory by the last user. On the below memory arrangement, the junk area is pointer as *. The whole memory size for this array is 07.

0

1

2

3

4

5

6

y

e

s

*

*

*

*

char array2[10] = “yes”; this array definition type as also the same three characters. But at last it has a “null character”. This null character is symbolized by \∅. It shows that after this word or specific character there is some designated memory space to separate from the next.

0

1

2

3

4

5

6

y

e

s

\∅

*

*

*

What is fstream?

Before explaining fstream, definition of ofstream and ifstream must come first.

  • ofstream is one of C++ library stream class member which represents the output file stream. ofstream used to create files. Additionally, it helps to add and write information to files. The library can be accessed By writing #include <ofstream>; at the top of the code.
  • ifstream is the other stream class library member in C++, represents the input file stream. It is for reading information from files. The library can be accessed By writing #include <ifstream>; at the top of the code.
  • fstream is stream class, represents the file stream in general. It is used for both ofstream and ifstream uses. Or it covers their use. In other words, it means it can create files, add data and information to files, and read and retrieve information from files. To access files while coding C++ just by declaring #include <fstream>; enough. Below shown simple example for fstream:
#include <iostream>;

#include <fstream>;

using namespace std;

int main () {

  ofstream samplefile;

  samplefile.open ("kodlogs.txt");

  samplefile << "Hello Kodlogs user.">>;

  samplefile.close();

  return 0;

}

Summary

An array is a serious group of memory locations. This memory locations have similar name and type. An array only with characters is called character array. Character array can be defined in a very different ways in C++. To access file in C++, at the top of the code we have to call/declare fstrea, ifstream and ofstream. These three library members allow us to access files.