Use.
The fprintf
function is part of the C libraries adapted to C ++ and resides in the header <cstdio>
, the signature of the function is as follows:
int fprintf( std::FILE* archivo, const char* formato, ... );
- The
archivo
parameter must be a file handler pointer ( std::FILE*
) which is obtained through the fopen
, do not forget to close the file with fclose
.
- The
formato
parameter is a text string with different format specifiers.
- The parameters in
...
will be a comma-separated list of the data that must match the format specifiers.
Therefore, as you mentioned Monpeco , the use you should give to fprintf
should be :
archivo = std::fopen("mi_archivo.txt", "w");
...
...
std::fprintf(archivo, "%s%d%d", cliente, apartamento, npiso);
Problem.
If that's the way I've done it and it's still the same
It is very likely that the variable cliente
does not have valid data and it is these that will end up in your file as symbols .
Proposal.
Forget about fprintf
, use the C ++ stream library:
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string cliente{};
int apartamento{}, npiso{};
std::cout << "Cliente: ";
std::cin >> cliente;
std::cout << "\nApartamento: ";
std::cin >> apartamento;
std::cout << "\nPiso: ";
std::cin >> npiso;
std::ofstream archivo("mi_archivo.txt");
archivo << cliente << '\n' << apartamento << '\n' << npiso << '\n';
return 0;
}