Copy string from a given character

Asked

Viewed 4,111 times

0

I am developing a C program that will work with 02 files .txt

The first file, is a report, that goes straight out of the system, and in it are information like item code, location and quantity in stock, this information is in the same line...

The product code, occupies at most the first 14 characters of the string, and I used the following code to remove it from the .txt:

memcpy( cod, &c[0], 14);
cod[15] = '\0';

However, following the line, I have more information that I wanted to allocate in other variables, to pass them to the second file .txt, which I will use as output for a Zebra printer.

How do I access the characters from position 15 onwards, and copy them to a variable?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

8

You’re on the right track. Didn’t you start by taking position 0? As you want to take position 15 onwards, use it. As you go all the way, you must take the sizeof of the whole text and subtract the 15 you want to discard.

#include <stdio.h>
#include <string.h>

int main(void) {
    char texto[] = "1234567890abcdefghijklm";
    int posicao = 15;
    int final = sizeof(texto) - posicao;
    char parte[final];
    memcpy(parte, &texto[posicao], final);
    printf("%s", parte);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If the string does not have the known size at compile time, would need to use a strlen() to discover its size. Or strnlen() if it cannot trust that the string is well formed.

In place of &texto[posicao] can use texto + posicao since texto is the address of string, just need to add the number of element in it. In fact the [] is only syntactic sugar for this pointer arithmetic operation.

  • It is worth noting that "sizeof" will only work for strings compiled together with the program, as in this example - never for data entered by the user, or read from a file, or even copied from a variable to another with no predefined size. Maybe you should use strnlen in such a code - even if this example works - why it allows the program to be done in a generic way.

  • @jsbueno added this. Thank you.

  • (is 2017 -- is it worth teaching "strlen" to people, as if it were 1985? You know how it is...strnlen helps prevent "stack overflow" :-)

  • 1

    Not quite, I agree that there are cases that need to be taken care of, but not always, there are exaggerations in this business that strlen() should not be used. Then I make an addendum, I have other issues to post, I don’t want to hide everything. https://blogs.msdn.microsoft.com/larryosterman/2009/06/23/good-news-strlen-isnt-a-banned-api-after-all/

2

I suggest using sscanf. Example assuming fixed length fields 14,20,20:

#include <stdio.h>

int main(){
 char texto[] = "cod 1234567890 Joaquim da Silva    933322232";
 char a[30], b[30],c[30];
 if( sscanf(texto,"%14[^\n]%20[^\n]%[^\n]" ,a,b,c) == 3)
    printf("cod='%s' nome='%s' tel='%s'\n",a,b,c);
 return 0;
}

running gives:

cod='cod 1234567890' nome=' Joaquim da Silva   ' tel=' 933322232'

Browser other questions tagged

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