-3
My intention is simply to take a part of a string that I know from position 0 to position 29, for example, contains a proper name. And from position 30 to position 45, is the phone associated with the person’s name. I can invent a function like this but if you have it ready in some library I want to use it. A hypothetical code follows:
#include <iostream>
#include <string>
using namespace std;
int main() {
FILE * pFile;
pFile = fopen ("lista.txt" , "r");
string str1, str2, str_fonte;
fgets ( str_fonte , 45 , pFile );
str1.copia( str_fonte, 0, 29 ); // código hipotético, alguma função parecida?
str2.copia( str_fonte, 30, 45 ); // código hipotético, alguma função parecida?
close(pFile);
return 0;
}
I haven’t found anything yet, while doing my own job, I hope it’s useful to someone.
#include <iostream>
#include <string>
using namespace std;
string copia (string fonte, size_t l_inf, size_t l_sup)
{
size_t i = l_inf;
size_t j = 0;
size_t tam = 1 + (l_sup - l_inf);
string copiado;
copiado.reserve(tam);
for ( size_t i = 0; i < tam; i++ )
{
copiado.push_back(' ');
}
for ( i = l_inf; i <= l_sup; i++ )
{
copiado[j] = fonte[i];
j++;
}
return copiado;
}
int main() {
string test = {"Testando a nova funcao copia ( string fonte, size_t l_inf, size_t l_sup )."};
cout << test << endl;
string teste;
teste = copia(test, 11, 27);
cout << "[" << teste << "]" << endl;
return 0;
}
The output of the program will be:
Does that answer your question? https://stackoverflow.com/a/2114388/14100521
– Natan Fernandes
Behold
std::string::substr
.– suriyel
Do not use handshakes in questions or answers: Ref: https://answall.com/help/behavior
– Augusto Vasques