0
Well, folks, in my progress in C language study, I’m working with file manipulation, nothing too complicated, but I’ve come up with a problem I can’t solve, For lack of more definition in searches it is difficult to find a similar problem is solved on the internet, then I turn to you again
Without further wrapping, in my code, in test, ask the user to enter a name, then call the function Read data to check if that name is available within the application. If you see in the code, when you arrive at the part to analyze between "token" and "name" nothing happens even if they are equal values. Certainly they are not, I used the debug of the Codeblocks, and within token, instead of being for example, only "maria", it is appearing "maria n001236", however when it is called printf, it appears correctly "maria" and not "maria n001236" only as an example. Why does this happen? what do I do to split this string again? Sorry for errors in editing, because I am without pc, using only smartphone
users.txt files BS: I used & as the final character for testing for now, because using EOF, '-1', -1, always gave an infinite loop I don’t know why. Obs2: the content of the users.txt file and what is inside the keys {
/* System user record, type csv(comma separated command), where we have, user id(for system manipulation) user name
example:
0001,aislan
*/
# 001234,aislan 001235, 001236, 001237,
& }
Interfacecliente file. c
#include <stdio.h>
#include <conio.h>
void LerDados(char name[15])
{
FILE *users; // cria variável ponteiro para referenciar na
// memoria um tipo de arquivo txt, no caso o
// user.txt
char url[] = { "/sdcard/bd.cafeblue/bd/users.txt" };
// url q se encontra o arquivo users.txt
// abrindo o arquivo com tipo de abertura w
users = fopen(url, "r");
// testando se o arquivo foi realmente criado
if (users == NULL)
{
printf("Erro na abertura do arquivo!");
return 1;
}
else
{
int i = 0;
char *string;
int tamVetor = 0;
string = malloc(sizeof(char **) * tamVetor);
while ((lido = fgetc(users)) != '&')
{
if (lido == '#')
{
while ((lido = fgetc(users)) != '&')
{
string[i] = lido;
i++;
tamVetor++;
string = realloc(string, sizeof(char **) * tamVetor);
}
break;
}
}
printf("%s", string);
char *token;
token = strtok(string, ",");
while (token != NULL)
{
printf("%s\n", token);
if (strcmp(name, token) == 0)
{
printf("Usuario ja cadastradoe!\n");
break;
}
token = strtok(NULL, ",");
}
}
fclose(users);
}
int main()
{
char user[15];
printf("Entre com o nome: ");
scanf("%s", &user);
LerDados(user);
}
Your code has far more to it than is relevant to the problem, including functions that have no instructions, so just leave what is relevant to the problem. Take advantage and hit the part of the information that is in the file, because you can not understand for sure what really has, since I see several lost symbols as
#
,&
,}
and I can’t tell if they’re part of it or not.– Isac
Isac, I edited, see if you can understand and help me, I wait!
– Aislan Silva Costa
Why don’t you put commas in the file users.txt, in all fields? For example,
#1,Aislan,<quebra de linha>
2,Maria,<quebra>
3,Silva,<quebra>
4,Uchimura,<quebra>
5,Costa,<quebra>
6,Marcelo,<quebra>
– Marcelo Shiniti Uchimura
Thanks for the contact Marcelo. The file is formatted the way Voce recommended.
– Aislan Silva Costa
But the contents of the file are just the
001234,aislan 001235,maria 001236,joao 001237,silas
? How to format only what is inside the code-formatted file in question ? UsingCtrl+k
or the button{}
of the editor.– Isac
Thank you for the contact Marcelo. I did the recommended way and it really worked, I just put the comma between the last field and the line break and it worked. While at it, you would be able to tell me why & run at the while that checks when and the end of the file and the EOF and nor does the -1 work?
– Aislan Silva Costa