Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I introduce a new mineral from a list

Status
Not open for further replies.

danielinhu

Technical User
Jun 22, 2010
23
Hello people,

I already have been here, and i already solved my first problem.
Now I already can read a file from outside fortran in fortran.

But now I want to add a new line in my list how can i do it??

my program is:

....

select case(opcao)

case(1) ! This first option open the file with the data

write(*,*) "Numero do Mineral | Nome | Cor | Brilho | Dureza "
open (2, file="basedados.txt", iostat = ok, status='old') ! Abre a lista de minerais
if (ok == 0) then
do n=1, 10
read (2, *, iostat=fim_fich)basedados(n)
if (fim_fich < 0) exit
basemineral = basemineral +1
end do
close(2)
else
write(*,*) "erro na aberura do ficheiro:",ok
end if
print "(i5,2x,a11,2x,a10,2x,a10,2x,f6.1)",basedados

case(2) ! In case 2 I want to add a new mineral to my database
...

how can i do it??

Can someone help me??

Greetings
 
The program is running, but my option 4 that is to delete a mineral from the database. And that isn't working 'cause I write the name to delete the mineral and that don't delete it...

I don't know if it is something with the subroutine...

there is my code again with ur help:

module mineralmod

type mineral !Definicao de mineral com as suas caracterisitcas
integer:: numero
character(50) :: nome,cor, brilho
real :: dureza
end type mineral
contains
! subrotina para APAGAR um registo de uma matriz
subroutine apagar_min(minerais,nminerais,nome)
! parametros
type (mineral), dimension:)), intent(inout) :: minerais
integer, intent(inout) :: nminerais
character, intent(in) :: nome
! variáveis internas
integer :: k
! procura ano a apagar
do k=1, nminerais
if (nome == minerais(k)%nome)then
minerais(k) = minerais(nminerais)
nminerais = nminerais-1
exit
endif
enddo
end subroutine
end module mineralmod

program basedados

use mineralmod
implicit none


type (mineral), dimension(1200) :: basededados
type (mineral):: newmineral
character(50):: apagamineral
integer:: opcao=1, i, ok, fim_fich,n, nminerais=0
integer::nummineral

write(*,*) "*.*.*.*.*.*.*.*.* BASE DE DADOS DE MINERAIS *.*.*.*.*.*.*.*.*"

do while(opcao/=0)
print "('')"
print "('1-Load Baseadados.txt')"
print "('2-Ver lista de minerais')"
print "('3-Inserir novo mineral')"
print "('4-Apagar um mineral')"
print "('5-xxxx')"
print "('6-xxxxx')"
print "('7-Save Basedados.csv')"
print "('0-Fim')"
read(*,*) opcao

select case(opcao)

case(1) ! Load ficheiro

open (2, file="basedados.txt", iostat = ok, status='old') ! Abre a lista de minerais
if (ok == 0) then
do n=1, 1200
read (2, *, iostat=fim_fich)basededados(n)
if (fim_fich < 0) exit
end do
nminerais = n-1
close(2)
else
write(*,*) "erro na aberura do ficheiro:",ok
end if

print*, '.:Base de dados carregada com sucesso:.'

case(2) ! Mostra base de dados

print "(i5,2x,a11,2x,a10,2x,a10,2x,f6.1)",basededados(1:nminerais)


case(3) ! Insere novo registo

write(*,*) "Introduza os novos minerais - numero, nome, cor, brilho e dureza"
read(*,*) newmineral%numero,newmineral%nome,newmineral%cor,newmineral%brilho,newmineral%dureza

basededados(nminerais+1)=newmineral ! Contador de minerais que estao activos
nminerais = nminerais +1


case(4) !

print *,"Mineral a apagar??"
read (*,*)apagamineral

call apagar_min(basededados,nminerais,apagamineral)












end select
enddo
end program

Greetings
 
Not quite sure what the problem is - the subroutine deletes the mineral from a memory array. Do you want it deleted from the file? If so, then you need to write the whole array back to the file after deletion.
 
My program is :

You have 5 or 6 options i don't know yet! so the 1st option is to load the file with the minerals to the program.
the 2nd is to watch the file in the program
the 3rd is to insert a new mineral in the file
the 4th(and that is the one that isn't working) - is to insert a name of a mineral and delete it from the list. And that isn't working, and I don't know how to do that.

Did u understand now??

Thanks for ur patient
 
What isn't working about it? Is it not deleting the item from the list? In your last 4 posts, you've just said that it isn't working but you haven't said what isn't working.

Do your minerals have spaces in their names?
 
danielinhu,

I'm not sure if you understand how the program works.

You have the option 1 to load data from the file (on the disk) into array (in memory).

Then you have options for operations on the array in the memory:
2 - to list the array
3 - insert new element into the array
and
4 - delete one element from the array.

Then you have option 7 - to save the array (memory) back to the text file (disk). That's all.

I modified your program, so now the options above work - see in the thread here:

I would suggest you to program now as options 5 or 6 the sort subroutine, which sorts the array elements according to the basedados(k)%numero component.
 
Yes that's it! It is not deleting the mineral from the list! But mikrom already helped me!

Thank u both!

Now I will start writing option 5!

Greetings
 
danielinhu said:
It is not deleting the mineral from the list!
What you do you mean with the "list"?
For me list means mostly the same as array, because in terms of some scripting languages there are list instead of arrays.
However, there is big difference between both datastructures: Arrays vs. Lists.

In Terms of Fortran:
Option 4 deletes one mineral from the array.
Option 7 saves the data from array into the text file.
Option 1 loads the data from text file into the array.

Therefore I don't understand what you mean with quote above.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top