bmosca Posted May 10, 2009 at 10:40 AM Report #262793 Posted May 10, 2009 at 10:40 AM 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
bmosca Posted May 10, 2009 at 10:50 AM Author Report #262794 Posted May 10, 2009 at 10:50 AM 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"); }
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