Using ".get (char a)" in C ++

0
void estadisticas ( const string & nombre , bool & ok , Estad & sts )
{
    ifstream fich ;
    fich . open ( nombre . c_str ());
    if ( fich . fail ()) {
        ok = false ;
    } else {
        inic ( sts );
        char c;
        fich . get (c);
        while (! fich . fail ()) {
            procesar (c, sts );
            fich . get (c);
        }
        ok = fich . eof ();
        fich . close ();
    }
}

What does fich.get(c) do in this code

    
asked by Skydez 10.05.2017 в 19:03
source

2 answers

1

The function receives a variable type char by reference to store in this variable the next character to read from the file.

A silly example that reads a file character to character and prints them in the console:

int main()
{
  std::ifstream fichero;
  fichero.open("test.txt");

  while( fichero.good() )
  {
    char c;
    fichero.read(c);
    std::cout << c;
  }
}
    
answered by 10.05.2017 в 19:12
0

istream& istream::get(char& ref_c)

It is a function belonging to the streams of C ++ which is used to obtain the current character within the stream and advance to the next position.

Consider the following text file (hello.txt) :

Hola Mundo

When you open hola.txt in a istream , the position starts at the zero index of the file, so when% istream::get(c) in the variable containing the file, you will get the letter H stored in the variable c and the file will now point to the next letter ( o ) .

See the following code;

#include <iostream>
#include <fstream>

int main() 
{
    std::ifstream is("hola.txt");
    while (is.good()) 
    {
        char c = 0;
        is.get(c);
        std::cout << "El caracter actual es: " << c << '\n';
    }
    is.close();

    return 0;
}

You get by result:

El caracter actual es: H
El caracter actual es: o
El caracter actual es: l
El caracter actual es: a
El caracter actual es: 
El caracter actual es: M
El caracter actual es: u
El caracter actual es: n
El caracter actual es: d
El caracter actual es: o
El caracter actual es: 

Reference: C ++ Reference .

    
answered by 10.05.2017 в 19:20