Separate a string in 2 from a file

Asked

Viewed 62 times

0

I’m having trouble separating a consulted string from a file into two.

the file is so alex 6461 anddre 979794 Douglas 6469794 Gustavo ...

i want to pick the characters picked up on the line and divide into 2 strings so name: alex tel:6461.

Who can help me I love very much.

void Buscar_Registro()
{
FILE *fp;
Agenda A;
Agenda *list;
list=&A;
int linha,i=0;
char c,nome[1001],nome2[1001];
char *ch;


 fp = fopen("AgendaDeContatos.txt","r+");
 if(fp == NULL)
    {
    printf("Erro na abertura do arquivo.\n");
    return(0);
    }
  printf("\nDigite a posição do registro:\n");
  scanf("%d",&linha);

    while(i<1001 - 1 &&(c = fgetc(fp)) != EOF) {
    if(linha==1 && c!='\n'){
//      printf("%c",c);
         nome[i++]= c;
         nome[i]='\0';
         nome2[1001]="";
         strcpy(nome2,nome);


    }
    if(c=='\n'){
        linha--;

    }
    }


    fclose (fp);

}

Complete Code: https://pastebin.com/edit/Fhws9vsE

  • 1

    What file information ? How is it to separate ? What code is supposed to do this ?

  • the code checks a certain number of selected lines until the line breaks and stores the characters in the variable "c"

  • file alex 6461 anddre 979794 Douglas 6469794 Gustavo 3131346 leadnro 679764 Matrix 9777946 oscar 97946 Vinicius 649413 Zion 646461

  • i want to pick the characters picked up on the line and divide into 2 strings so name: alex tel:6461

  • All this information is relevant to the question and helps those who want to answer it. For this reason you should edit the question by placing this information on it.

  • I’ve already edited, I think now it’s right

  • i managed to turn the read line characters into string but the rest is difficult =(

  • You can use the function strchr library string.h (see here) to get the position of a ' '(space) character. And then do the same to get the second space. So, you take the characters from position 1 to position 2. malloc or calloc to create a new string with the characters.

  • Possible duplicate https://answall.com/questions/190755/como-separar-uma-string-em-peda%C3%A7os? Rq=1

  • I even understood but give me an example there please

  • const char Ch2 = ' '; char * Ret = strchr(Nome2, Ch2); ?

  • can leave finally I got kkkkkkkkk

Show 7 more comments
No answers

Browser other questions tagged

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