C problem reading file and writing to vectors/matrices

Asked

Viewed 2,323 times

0

I’m having trouble reading data from a file and writing it into a vector and matrix. I can’t give the 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;
}
  • 1

    I could not identify in your explanation what the problem you are having. Give more details of what is happening.

  • You should [Dit] your question to put new information and not answer it, unless you have already solved the problem and want to post the solution found to help other people.

  • Well I have a file . txt that has students' names and their enrollments. I want to read this file and store the names in an array, and the plates in an array. Only that I put that printf inside the while so that he was informing me what he’s already saving. Only when running the program it does not appear anything on the console... So I do not know if the program is entering into while or not. I did some tests and I couldn’t come to any conclusion...

2 answers

2

Include that and see if it helps you buddy! =)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

1

char url[]="pontos.txt";
FILE *arq;
arq = fopen(url, "a"); 

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

Work with the pointers.

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

Now just record it:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.