Jump to content
Sign in to follow this  
zasuk

[Ajuda] Ler ficheiro em modo de texto

Recommended Posts

zasuk

Boas, como posso ler um ficheiro em que as palavras estão separadas por ";" e guardar cada uma das palavras numa variável??

é possível fazer a leitura palavra a palavra ou apenas char a char?

Exemplo do ficheiro:

6525;Andre;Silva;Acer;htc;Vodafone;portatil;

Miguel;facebook;portugal;a;programar;e;espetacular;

Share this post


Link to post
Share on other sites
zasuk

Já consigo implementar com esta função, obrigada...

mas já agora, não existe maneira de ler palavra a palavra sem ter que efectuar uma divisao?

com o fscanf por exemplo....

Share this post


Link to post
Share on other sites
Baderous

Com a sscanf consegues fazer isso:

#include <stdio.h>

int main(void) {
        FILE *fp = fopen("baderous.txt","r");
        char num[5], fname[10], sname[10], tlm[5], op[10], maq[15], str[1024];
        while (fgets(str,sizeof(str),fp)) {
                sscanf(str,"%[^;];%[^;];%[^;];%[^;];%[^;];%[^;];",num,fname,sname,tlm,op,maq);
                printf("%s\n%s\n%s\n%s\n%s\n%s\n",num,fname,sname,tlm,op,maq);
        }
        fclose(fp);
        return 0;
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.