E
E
Evgeny Petryaev2020-09-18 23:37:28
C++ / C#
Evgeny Petryaev, 2020-09-18 23:37:28

How to run a substring through a string to the end?

I use this code

int N 8;
    char str_[N + 1];//подстрока
    str_[N] = '\0';
    unsigned char* buffer;//строка
    int i = 0;
    while (i<=strlen((char*)buffer))
        {
            strncpy_s(str_, (char*)(buffer+i), N);
            i += 8;
        }

Consider that the buffer array is full

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Zhilin, 2020-09-22
@Gremlin92

Problems in the code:

  • Potential going abroad (res2001)
  • strncpy_s - again someone writes in Visual C ++. Replace function, see below
  • Now the buffer length is recalculated every time in the loop - no comments
  • If it is not a string, but a buffer, then it must not end in \0, and string operations must not work on it. For a substring, you need to allocate exactly 8 bytes. The size of a large buffer must be taken from somewhere separately, not through strlen. To copy use std::copy
  • Let the substring be also unsigned char. char should be used for text, not bytes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question