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
What file information ? How is it to separate ? What code is supposed to do this ?
– Isac
the code checks a certain number of selected lines until the line breaks and stores the characters in the variable "c"
– FZero
file alex 6461 anddre 979794 Douglas 6469794 Gustavo 3131346 leadnro 679764 Matrix 9777946 oscar 97946 Vinicius 649413 Zion 646461
– FZero
i want to pick the characters picked up on the line and divide into 2 strings so name: alex tel:6461
– FZero
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.
– Isac
I’ve already edited, I think now it’s right
– FZero
i managed to turn the read line characters into string but the rest is difficult =(
– FZero
You can use the function
strchr
librarystring.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
orcalloc
to create a new string with the characters.– Wesley Gonçalves
Possible duplicate https://answall.com/questions/190755/como-separar-uma-string-em-peda%C3%A7os? Rq=1
– Wesley Gonçalves
I even understood but give me an example there please
– FZero
const char Ch2 = ' '; char * Ret = strchr(Nome2, Ch2); ?
– FZero
can leave finally I got kkkkkkkkk
– FZero