File manipulation

0

Hello. I have a problem with handling create and manipulate a file in C. First I made the whole code using a struct and functions, it worked everything right. I put the commands to create the file and it is also ok.

My problem is when you open and insert data into it. I've tried it in many ways and the most I could do was to dump the data saved in memory. Follow the code.

  

P.S. They are all 8 functions, I will put only the first, not to get too polluted visually the post.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <locale.h>
#include <string.h>
#define MAX 1

struct turma{
int matricula;
char nome[15];
float nota[5];
float media;
char resultado;
}; typedef struct turma turma;

int main()
{
setlocale (LC_ALL, "Portuguese");
printf ("\nFeito por Maximiliano Meyer\n");
turma alg[MAX];
int menu;

FILE *arquivo;
arquivo = fopen("notas.txt","rb+");
if(arquivo==NULL)
    {   arquivo=fopen("notas.txt","wb+");
        printf("\nO arquivo notas não pôde ser aberto");
        if(arquivo==NULL)
        { printf(" O arquivo não pôde ser aberto");
        exit(1);
        }
    }


menu = 0;
  while (menu != 8)
  {
system("cls");
printf("\n\n  Escolha uma opção no menu abaixo:\n\n");
printf("  |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|\n");
printf("  | 1 - Inserir notas                      |\n");
printf("  | 2 - Consultar notas                    |\n");
printf("  | 3 - Alterar dados                      |\n");
printf("  | 4 - Excluir                            |\n");
printf("  | 5 - Listagem geral da turma            |\n");
printf("  | 6 - Média da turma                     |\n");
printf("  | 7 - Consultar alunos aprovados         |\n");
printf("  | 8 - Sair                               |\n");
printf("   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
scanf("%d", &menu);
printf("\n");

switch (menu)
{
  case 1 : system("cls");
  insere(alg, arquivo);
  printf("\n\aNotas inseridas!\n");
  system("Pause");
break;

... other functions ....

 case 8: system("cls");
      printf("Saindo da aplicação\nDesenvolvido por Maximiliano Meyer\n\n");
    break;

  default:
    system("cls");
    printf("Opção inválida.\nRevise o valor digitado.\n\n");
    system("Pause");
    }
  }
    fclose (arquivo);
}
void insere (FILE *arquivo, turma alg[MAX])
{   turma reg;
arquivo=fopen("notas", "rb+");
int x,y;
float cont=0;

for (x=0;x<MAX;x++)
{   printf("\nInforme os dados do %iº aluno: ", x+1);
    printf("\n\nMatrícula: ", x+1);
    scanf("%d", &alg[x].matricula);
    printf("Nome: ", x+1);
    fflush(stdin);
    gets(alg[x].nome);
        for (y=0;y<5;y++){
        printf("%iº nota: ", y+1);
        scanf("%f", &alg[x].nota[y]);
        cont = cont + alg[x].nota[y];
    }
    alg[x].media = cont/5;
    if (alg[x].media >=7)
        alg[x].resultado = 'A';
    else
        alg[x].resultado = 'R';
}
    fseek(arquivo,0,SEEK_SET);
    fwrite(&reg,sizeof(struct turma),1, arquivo);
    fclose(arquivo);
}
    
asked by anonymous 20.06.2015 / 22:07

2 answers

1

Analyzing the time you open the file

fopen("notas", "rb+");

If you just want adicionar text instead of creating a new one you use the a

fopen("notas", "a");
  

Adds to a file. Read operations, adds data to the end of the file. The file is created if it does not exist.

Source: link


Using append you do not need fseek , just use fprintf .

    
20.06.2015 / 23:29
0

Compile with the maximum warnings and errors that your compiler allows.

Within the function main() tens

  case 1 : system("cls");
  insere(alg, arquivo);

without an active prototype for the insere() function. At this point the compiler "invents" the signature int insere() .

Later you define the function with a different signature than the compiler invented, namely void insere(FILE *, turma *) and the compiler should complain !!

Solution: write the prototype of the function before to call it (the function definition also serves as a prototype), binds the maximum of warnings, and takes care and corrects all warnings produced .

    
20.06.2015 / 23:52