andrejrc Posted June 5, 2014 at 01:28 PM Report #558307 Posted June 5, 2014 at 01:28 PM Olá pessoal estou aqui com um pequeno problema neste programa. #include <stdio.h> #include <stdlib.h> #include <string.h> FILE *ler; main() { if ((ler = fopen("Utilizadores test .txt", "r")) == NULL) { printf("Impossivel Abrir o ficheiro %s\n", "Utilizadores test .txt"); exit(0); } char nome[20]; char nome_scan[20]; char pass[20]; printf("Utilizador: "); scanf("%s", &nome); printf("Procurar por %s\n", nome); while (fscanf(ler, "%s %s", pass, nome_scan)!=EOF) { if (strcmp(nome, nome_scan) == 0) { printf("Utilizador: %s Password: %s\n", nome, pass); } } return (EXIT_SUCCESS); } Quando compilo e corro o programa, com o ficheiro criado e com dados, ele não faz o: if (strcmp(nome, nome_scan) == 0) { printf("Utilizador: %s Password: %s\n", nome, pass); } corretamente.
HappyHippyHippo Posted June 5, 2014 at 01:37 PM Report #558311 Posted June 5, 2014 at 01:37 PM olha bem para o valor de retorno da função fscanf : http://pubs.opengroup.org/onlinepubs/009604599/functions/fscanf.html Upon successful completion, these functions shall return the number of successfully matched and assigned input items isto que dizer que, o mais provável, é não estares a ler correctamente os dados do ficheiro IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
andrejrc Posted June 5, 2014 at 02:46 PM Author Report #558344 Posted June 5, 2014 at 02:46 PM mas o que acho esquisito e que sem eu substituir a variável nome por: if (strcmp("Pedro", nome_scan) == 0) {[/size] printf("Utilizador: %s Password: %s\n", nome, pass); } fica a funcionar na perfeição
HappyHippyHippo Posted June 5, 2014 at 02:54 PM Report #558348 Posted June 5, 2014 at 02:54 PM quais os dados no ficheiro e que dados estás a escrever na consola ? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
andrejrc Posted June 5, 2014 at 03:06 PM Author Report #558353 Posted June 5, 2014 at 03:06 PM (edited) O programa tinha como objectivo, partir de um ficheiro criado noutro programa, fazer um pesquisa nesse ficheiro e mostrar quais os utilizadores com o mesmo nome. o ficheiro contem uma lista de 40 e tais utilizadores e passwords ou seja a25r8ds4 Joao e5r4t7d4 Pedro p8er7as4 Luis ......etc.......... : Edited June 5, 2014 at 03:06 PM by andrejrc
HappyHippyHippo Posted June 5, 2014 at 03:24 PM Report #558356 Posted June 5, 2014 at 03:24 PM e estas a escrever "Pedro" na consola quando pede o nome a pesquisar ? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
andrejrc Posted June 5, 2014 at 03:31 PM Author Report #558359 Posted June 5, 2014 at 03:31 PM basicamente substitui a variável "nome" pela string que ele deveria de representar
HappyHippyHippo Posted June 5, 2014 at 03:44 PM Report #558362 Posted June 5, 2014 at 03:44 PM (edited) e estas a escrever "Pedro" na consola quando pede o nome a pesquisar ? edit : bem .. foi dar uma vista de olhos melhor pelo o teu código: printf("Utilizador: "); /*scanf("%s", &nome);*/ scanf("%s", nome); // <---------------- sem & Edited June 5, 2014 at 03:48 PM by HappyHippyHippo IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
andrejrc Posted June 5, 2014 at 03:49 PM Author Report #558363 Posted June 5, 2014 at 03:49 PM AaaaaH lol nao tinha percebido. ja experimentei varias strings mas nada deu certo
andrejrc Posted June 5, 2014 at 04:05 PM Author Report #558367 Posted June 5, 2014 at 04:05 PM muito obrigado por toda a ajuda que me deu mas ja encontrei o problema
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now