Separate string that has no bounders

Asked

Viewed 128 times

-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:

inserir a descrição da imagem aqui

  • Does that answer your question? https://stackoverflow.com/a/2114388/14100521

  • 1
  • 1

    Do not use handshakes in questions or answers: Ref: https://answall.com/help/behavior

1 answer

1


I can invent a function like this but if you have any ready library I want to use it

  • In C++, as suggested in this topic and in the answer you quoted, you can use substr() of the string class to separate the fields, among many alternatives.
  • In C memcpy() would be the classic option, and would also work in C++.

C and C++ are very different languages. And it will also depend on how you read this and the rest of the program. In your example the fields have fixed size and it is not determined that they are strings C, with a zero at the end of the name and another at the end of the phone.

Examples

To this.txt list file

0123456789 1234567890123456789CCCAAANNNNNNNNN1
X0123456789 1234567890123456789CCCZAANNNNNNNN2
XY0123456789 1234567890123456789CCCZAANNNNNNN3
XYW0123456789 1234567890123456789CCCZAANNNNNN4
XYWZ0123456789 1234567890123456789CCCZAANNNNN5

This would be the output of the following simple examples, in C using memcpy() and in C++ using substr()

Nome : '0123456789 1234567890123456789' Fone: 'CCCAAANNNNNNNNN1'
Nome : 'X0123456789 123456789012345678' Fone: '9CCCZAANNNNNNNN2'
Nome : 'XY0123456789 12345678901234567' Fone: '89CCCZAANNNNNNN3'
Nome : 'XYW0123456789 1234567890123456' Fone: '789CCCZAANNNNNN4'
Nome : 'XYWZ0123456789 123456789012345' Fone: '6789CCCZAANNNNN5'

In C

#include <stdio.h>
#include <memory.h>

int main(void)
{
    char    registro[47] = { 0 }; // vai ler aqui
    char saida[] = "\
Nome : '1111222233aaaabbbbdd1111222233' \
Fone: '1111222233aaabbb'"; // e gravar aqui

    FILE* in = fopen("lista.txt","r"); // deste arquivo
    int n = fread( (void*) &registro[0], sizeof(registro), 1, in);
    while (n > 0)
    {
        memcpy(saida + 8, registro, 30);
        memcpy(saida + 47, registro + 30, 16);
        printf("%s\n", saida);
        n = fread((void*)&registro[0], sizeof(registro), 1, in);
    }
    fclose(in);
    return 0;
}

Or in C++

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main(void)
{
    string arquivo = "lista.txt"; // arquivo
    ifstream entrada{ arquivo };
    string linha{};
    getline(entrada, linha);
    while (linha.length() > 0)
    {   std::cout << "Nome : \"" << linha.substr(0, 30) <<
            "\"\tFone: '" << linha.substr(30, 16) << "'\n";
        getline(entrada, linha);
    }
    entrada.close();
    return 0;
}
  • Hello @arfneto, I don’t know if I understood your remark about my example but I think you meant that I wouldn’t need to enter 0 at the end of the return string because the string type already handles the necessary adjustments. Thank you, I made an update to the code and I will accept your reply, too bad I can not score it as useful.

  • What’s written on the disk is beyond these string distinctions or anything. It’s just a file. You didn’t post a source file, so I wrote according to what seemed right :) Here’s what happens: if the strings are null-terminated you can read straight with strcpy() and use to build your strings. But then you lose the convenience of being able to type the data in a text editor. If you want to do so and want I can change the examples.

  • All right is great your examples, I who mixed C with C++. Super worth your comments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.