File manipulation

Asked

Viewed 192 times

0

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

My problem is to open and insert data into it. I have tried several ways and the most I could was to bugle the data saved in the same memory. Follows the code.

P.S. Are in 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);
}

2 answers

1

Analyzing at the time you open the file

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

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

fopen("notas", "a");

Add to a file. Read operations, add data at the end of the file. The file is created if it does not exist.

Source: http://www.tutorialspoint.com/c_standard_library/c_function_fopen.htm


Using the append you don’t need the fseek, just use fprintf.

0

Compile with as many warnings and errors as your compiler allows.

Within the function main() you have

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

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

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

Solution: writes the function prototype before calls (definition of function also serves as a prototype), binds the maximum warnings, and takes attention and corrects all warnings produced.

Browser other questions tagged

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