Jump to content

[Ajuda] Ler ficheiro em modo de texto


zasuk
 Share

Recommended Posts

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;

Link to comment
Share on other sites

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;
}
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • 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.