Problem in C when reading file and writing to vectors / arrays

0

I'm having trouble reading data from a file and writing it to a vector and array. I can not give printf to test them. So I'm not sure if the code is correct. The file has student names and their enrollments respectively.

#include stdio.h
#include stdlib.h
#include string.h
#define TOT 5

int le_alunos(char nome_al[], int matricula_al[][TOT+1]){

    int i=0, c;
    FILE *alunos;



    alunos = fopen("ALUNOS.TXT","r");
    if(alunos == NULL){
        printf("Erro ao abrir o arquivo \"ALUNOS.TXT\" ");
        exit(1);
    }

    c = fscanf(alunos,"%[^\n] %d", &nome_al[i], matricula_al[i]);
    while(c==2){
        i++;

        printf("Nome:%s...................Matricula:%d", nome_al[i],matricula_al[i]);
    }   

    fclose(alunos);
    return i;
}

int main(void){

    int i;
    char nome_al[81];
    int matricula_al[2][TOT+1];
    int papagaio;

    papagaio = le_alunos(nome_al,matricula_al,i);



    return 0;
}
    
asked by anonymous 04.11.2014 / 15:21

2 answers

2

Include this and see if it helps you friend! =)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
    
27.11.2014 / 02:09
1
char url[]="pontos.txt";
FILE *arq;
arq = fopen(url, "a"); 

fprintf(arq,"Jogo %2d  deu %2d pontos\n", x , pontos);

You have to work with the pointers.

An address char url[]=" "; , which will be - char url[]="pontos.txt";
A pointer type FILE arq , which will be - FILE *arq;
And open the file with the pointer pointing to url[] through the function fopen ,
which will be - arq = fopen(url, "a"); .

Now just record:

fprintf(arq,"Jogo %2d  deu %2d pontos\n", x , pontos);
    
07.10.2015 / 01:41