Jump to content

leitura de um ficheiro de texto


Recommended Posts

Posted

boas,

tenho um ficheiro texto assim:

------------------------------------------------

Numero de alunos: 8

Numero de provas: 5

------------------------------------------------

queria ir buscar os dois inteiros, (8 e 5) e atribui-los a duas variáveis, já tentei com fscanf e fgets mas não consigo fazer nada 😕 aqui vai o código:

void main()

{

    FILE *f;

    int totalunos,numprovas;

    char texto[50],c;

    f=fopen("notas.txt","r");

    if(f==NULL)

        puts("erro no ficheiro");

    else

    {

        while(!feof(f))

        {

            fscanf(f,"%s",&texto);           

            fscanf(f,"%d",&totalunos);

            fscanf(f,"%s",&texto);

            fscanf(f,"%d",&numprovas);

                   

        }

    }

    fclose(f);

}

alguma coisa está mal? também tentei assim:

void main()

{

    FILE *f;

    int totalunos,numprovas;

    char texto[50],c;

    f=fopen("notas.txt","r");

    if(f==NULL)

        puts("erro no ficheiro");

    else

    {

        while(!feof(f))

        {

            fgets(texto,50,f);           

            fscanf(f,"%d",&totalunos);

            fscanf(f,"%s",&texto);

            fscanf(f,"%d",&numprovas);

                   

        }

    }

    fclose(f);

}

Se conseguissem descobrir o que se passa, agradecia 🙂

cmpts

Posted

oh.. já consegui , lol  😕

while(!feof(f))

        {

            while((c=fgetc(f))!=':');

            fscanf(f,"%d\n",&totalunos);

            while((c=fgetc(f))!=':');

            fscanf(f,"%d\n",&numprovas);

            printf("num alunos: %d\n",totalunos);

            printf("nume provas: %d\n",numprovas);

            system("pause");

           

         

        }

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