I have a litle problem when i try to list Aluno = Student (option 4 in the program).
i have other problem, when i try to save some Aluno(student using option 1) te program telll me that it is already created.
Some one can help me?

#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;
struct estruturaAluno
{
  char numeroAluno[5];
  char nomeAluno[81];
};

int MenuEscolhaAluno();
void AdicionaAluno(fstream&);
void ApagaAluno(fstream&);
void ModificaAluno(fstream&);
int Menu_ModificarAluno();
void MostraAlunos(fstream&);
int ValidaNumeroAluno(long);

int main()
{
  int choice;
  int quit = 0;

  cout << setprecision(2)
		 << setiosflags(ios::fixed)
       << setiosflags(ios::showpoint);

  fstream part_file;

  estruturaAluno null_part = {" "," "};

  //Open the file for reading to see if file exists

  part_file.open("partfile.dat", ios::in | ios::binary);

  //If the file does not exist,
  //create a file of 99 dummy records.

  if (part_file.fail())
	 {
		part_file.open("partfile.dat", ios::out | ios::binary);

		for (long rec_num = 1L; rec_num <= 99L; ++rec_num)
		  part_file.write( (char *) &null_part, sizeof(estruturaAluno) );

		cout << "\nNull File Created.";
	 }


  //Close the file

  part_file.close();


  //Open the file for both input and output

  part_file.open("partfile.dat", ios::in | ios::out | ios::binary);

  //Processing loop

  do
	 {
		choice = MenuEscolhaAluno();
		switch (choice)
		  {
			 case 1:
				AdicionaAluno(part_file); break;
			 case 2:
				ApagaAluno(part_file); break;
			 case 3:
				ModificaAluno(part_file); break;
			 case 4:
				MostraAlunos(part_file); break;
			 case 5:
				quit = 1; break;
			 default:
				cout << "\n\nEscola Invalida. Tente novamente.\n\n"; break;
		  }
	 }
  while (!quit);

  part_file.close();

  cout << endl;
  return 0;
} //End of main()

int MenuEscolhaAluno()
{
  int choice;

  cout << "\n\nMenu Principal : ALUNO";
  cout << "\n\n1 - Adiciona Aluno"
		 << "\n2 - Apaga Aluno"
		 << "\n3 - Modifica Aluno"
		 << "\n4 - Mostra alunos inseridos"
		 << "\n5 - Sai do Menu";

  cout << "\n\nEscolha uma Opcao: ";
  cin >> choice;

  return choice;
}  //End MenuEscolhaAluno()

void AdicionaAluno(fstream& file)
{
  estruturaAluno part;
  estruturaAluno temp_part;

  char rec_num;

  cin.get();

  cout << "\nInsira o numero de Aluno:";
  cin.getline(part.numeroAluno, 5);

  rec_num = (part.numeroAluno,5);
  if ( !ValidaNumeroAluno(rec_num) )
	 return;

  cout << "\nInsira o Nome do Aluno: ";
  cin.getline(part.nomeAluno, 81);


  file.seekg( (rec_num - 1) * sizeof(estruturaAluno), ios::beg);

  file.read( (char *)&temp_part, sizeof(estruturaAluno) );

  if ( strcmp(temp_part.numeroAluno, " ") == 0)
	 {
		file.seekg( -(long)sizeof(estruturaAluno), ios::cur);
		file.write( (char *)&part, sizeof(estruturaAluno) );
		cout << "\nRecord " << part.numeroAluno << " added to file.";
	 }
  else
	 cout << "\nJa existe. Escolha outro numero.";
} //End of AdicionaAluno

void ApagaAluno(fstream& file)
{
  estruturaAluno part;
  estruturaAluno null_part = {" "," "};

  long rec_num;

  cout << "\nInsira o numero de Aluno que pretende Apagar: ";
  cin >> rec_num;

  if (!ValidaNumeroAluno(rec_num))
	 return;

  file.seekg( (rec_num - 1) * sizeof(estruturaAluno), ios::beg);
  file.read( (char *) &part, sizeof(estruturaAluno) );

  if ( strcmp(part.numeroAluno, "") == 0)
	 cout << "\nA opcao nao existe escola outra opcao.";
  else
	 {
		file.seekg(-(long)sizeof(estruturaAluno), ios::cur);
		file.write( (char *) &null_part, sizeof(estruturaAluno) );
		cout << "\nAluno numero " << rec_num << " apagado com exito do ficheiro.";
	 }

}  //End of ApagaAluno

void ModificaAluno(fstream& file)
{
  estruturaAluno part;
  long rec_num;
  int change;
  int quit = 0;

  cout << "\ninsira o numero de Aluno que pretende modificar: ";
  cin >> rec_num;

  if (!ValidaNumeroAluno(rec_num))
	 return;

  file.seekg( (rec_num - 1) * sizeof(estruturaAluno), ios::beg);
  file.read( (char *) &part, sizeof(estruturaAluno) );

  if ( strcmp(part.numeroAluno, "") == 0)
	 {
		cout << "\nO aluno nao existe. Tente outro numero.";
		return;
	 }

  cout << "\nEscola os dados que pretende modificar:\n\n";
  cout << setw(20) << "Numero de Aluno"
		 << setw(20) << "Nome de Aluno"<< endl << endl;
  cout << setw(20) << part.numeroAluno
		 << setw(20) << part.nomeAluno;


  do
	 {
		change = Menu_ModificarAluno();
		switch (change)
		  {
			 case 1:
				cout << "\nNome de Aluno: ";
				cin >> part.nomeAluno;
				break;
			 case 2:
				quit = 1;
				file.seekg(-(long)sizeof(estruturaAluno), ios::cur);
				file.write( (char *) &part, sizeof(estruturaAluno) );
				cout << "\nMudanca executada " << rec_num;
				break;
			 default:
				cout << "\nEscolha invalida. Insira nova escolha.";
				break;
		  }
	 }
  while (!quit);

} //End of ModificaAluno()

int Menu_ModificarAluno()
{
  int change;

  cout << "\n\nMenu Modificar Aluno\n\n";
  cout << "1 - Modifica Nome\n"
		 << "2 - Modificar Numero\n"
		 << "3 - Executa as mudancas e volta ao menu principal\n\n";
  cout << "Insira uma opcao: ";
  cin >> change;

  return change;
}  //End Menu_ModificarAluno()

void MostraAlunos(fstream& file)
{
  estruturaAluno part;
  long rec_num;
  long rec_count = 0L;

  file.seekg( 0L, ios::beg);

  cout << setw(20) << "\nNome"
		 << setw(20) << "Numero"<< endl << endl;

  for (rec_num = 1L; rec_num <= 99L; ++rec_num)
	 {
		file.read( (char*) &part, sizeof(estruturaAluno) );
		if (strcmp(part.numeroAluno, "") != 0)
		  {
          cout << setw(20) << part.numeroAluno
					<< setw(20) << part.nomeAluno<< endl;
			 ++rec_count;
		  }
	 }

  cout << "\n\nFile contains " << rec_count << " records.";

}  //End of MostraAlunos()

int ValidaNumeroAluno(long rec_num)
{
  if ( rec_num < 00000 || rec_num > 99999 )
	 {
		cout << "\n\nERRO: Numero invalido. Insira outra opcao.";
		return 0;
	 }
  else
	 return 1;

}  //End of ValidaNumeroAluno()

For some reason I was having some problems with it too. I think ifstream and ofstream are easier to use than fstream, but I finally got it working. Here is what I did -- its somewhat different than what you posted because 1) its in English, and 2) I only tested AdicionaAluno() function.

My version initializes the empty file with records that contain the student id numbers but blank names. I think that makes it somewhat easier to debug the program because you can send the file to Notepad and see something other than all spaces. That also means that AdicionaAluno() can either add or update a student record.

#include <iostream>
#include <fstream>
using namespace std;

struct student
{
    char id[5];
    char name[81];
};

void AdicionaAluno(fstream& file)
{
  student part;
  student temp_part;

  int rec_num;

  memset(&part,0,sizeof(student));
  cout << "Enter ID:";
  cin.getline(part.id, 5);

  rec_num = atol(part.id);

  cout << "\nEnter name: ";
  cin.getline(part.name, 81);
  unsigned offset = (rec_num-1) * sizeof(student);
  file.seekg( offset, ios_base::beg);
  if(offset != (unsigned)file.tellg())
  {
      cout << "Error seeking to record " << rec_num << "\n";
      return;
  }

  sprintf(part.id,"%04d", rec_num);
  file.read( (char *)&temp_part, sizeof(student) );
  if ( strcmp(temp_part.id, part.id) == 0)
	 {
		file.seekg( -(long)sizeof(student), ios::cur);
		file.write( (char *)&part, sizeof(student) );
		cout << "\nRecord " << part.id << " added or updated to file.";
	 }
  else
	 cout << "\nInvalid student id.\n";
} //End of AdicionaAluno


int main ()
{
    fstream part_file;
    student null_part;
    memset(&null_part,0, sizeof(student));
    null_part.id[0] = ' ';
    null_part.name[0] = ' ';
    //Open the file for reading to see if file exists

    part_file.open("partfile.dat", fstream::in | fstream::out |  fstream::binary | fstream::ate);
    if(!part_file.is_open())
    {
        part_file.clear();
        part_file.open("partfile.dat",fstream::out |  fstream::binary | fstream::trunc);
        part_file.clear();
        for (int rec_num = 0; rec_num < 100; ++rec_num)
        {
            sprintf(null_part.id, "%04d", rec_num+1);
		    part_file.write( (char *) &null_part, sizeof(student) );
            part_file.clear();
        }
        part_file.close();
        part_file.clear();
        part_file.open("partfile.dat", fstream::in | fstream::out |  fstream::binary | fstream::ate);
       cout << "\nNull File Created.";
    }


    AdicionaAluno(part_file);
    return (0);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.