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
 
open( ... ,access='append')

Then write your record and close your file
 
That dont work!

there is what i put:

case(2) !2-Inserir novo mineral

open(2,access='append')

write(2,*) "Introduza numero, nome mineral etc"
read(2,*) basedados%numero, basedados%nome

close(2)

That isnt working! :S
what is wrong??
 
It should be
Code:
open (2, file="basedados.txt", iostat = ok, status='old', access='append')
 
open (2, file="basedados.txt", iostat = ok, status='old', access='append')

write(2,*) "Introduza numero, nome mineral etc"
read(2,*) basedados%numero, basedados%nome

close(2)

Doesn't work either! :s

appears an error like this -

At line 50 of file basedados.f95 (unit = 2, file = 'fort.2')
Fortran runtime error: End of file
 
If you open for appending, you should only write to the file. You need to close the file and reopen for reading before you can read from it. The alternative is to open a direct access file where you can both read and write.


Say you have a file with xxx

xxx

If you open it for appending and write "Introduza numero, nome mineral etc", you will get

xxx
"Introduza numero, nome mineral etc"

When you read from it the cursor is after etc" so there is nothing to read and you get and end of file error.
 
I understood what u said, but now i saved in the file what i wrote in the program!
But i want to write in the keyboard and save it in the file.

Something like this:

case(2)
write(*,*) "Enter the mineral characteristics"

read(*,*) ( NOW I WANT TO SAVE THESE CHARACTERISTICS IN THE FILE )

Is that possible??

And thanks for your patient! :)
greetings
 
Something like this?
Code:
case(2) !2-Inserir novo mineral
   ! Read from the keyboard
   write(*,*) "Enter the mineral characteristics"
   read(*,*) basedados%numero, basedados%nome

   ! Append to the file
    open (2, file="basedados.txt", iostat = ok, status='old', access='append')
    write(2,*) basedados%numero, basedados%nome

    close(2)
 

Hey there! I think is really something like that!
But isn't working 'cause give me an error like this:

At line 48 of file basedados.f95 (unit = 5, file = 'stdin')
Fortran runtime error: Bad integer for item 2 in list input

I don't understande 'cause my 2nd item is a character and is defined like it, but the program dont understand it! :S

my complete program is:

program basededados

implicit none

type mineral !Definicao de mineral com as suas caracterisitcas
integer:: numero
character(50) :: nome,cor, brilho
real :: dureza
end type mineral

type (mineral), dimension(10) :: basedados

integer:: opcao, i, ok, fim_fich, basemineral=0,n

write(*,*) "___________BASE DE DADOS DE MINERAIS_____________"

do while(opcao/=0)
print "('')"
print "('1-Ver lista de minerais')"
print "('2-Inserir novo mineral')"
print "('3-Apagar mineral')"
print "('4-Editar mineral')"
print "('5-Organizar mineral por alguma caracteristica especifica')"
print "('0-Fim')"
read(*,*) opcao

select case(opcao)

case(1) !1-Ver lista de minerais

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) !2-Inserir novo mineral

write(*,*) "Enter the mineral characteristics"
read(*,*) basedados%numero, basedados%nome

open (2, file="basedados.txt", iostat = ok, status='old', access='append')
write(2,*) basedados%numero, basedados%nome !( THE ITEM IS THIS ONE, AND IS DEFINED AS A CHARACTER)

close(2)

case(3) !3-Apagar mineral

case(4) !4-Editar mineral

case(5) !5-Organizar mineral por alguma característica especifica

case(0) !0-Fim

end select


end do


end program basededados
 
The problem is basedados is an array. I didn't realize this when I gave you the earlier solution.

1) declare a new variable
type (mineral):: newmineral

2) For case 2
Code:
   case(2) !2-Inserir novo mineral

   write(*,*) "Enter the mineral characteristics"
   read(*,*) newmineral% numero, &
    newmineral%nome, newmineral%cor, newmineral%brilho, &
    newmineral%dureza


    open (2, file="basedados.txt", iostat = ok, status='old', access='append')
    write(2,*) newmineral !( THE ITEM IS THIS ONE, AND IS DEFINED AS A CHARACTER)

    close(2)
You will need to input all the values: a number, 3 strings and another number
 
Thanks man!! That is awesome! :)

But i didnt really understood why it didn't work with the array basedados??

And onther doubt if now i want to for manipulate the data present in the txt file, i work with the sentence like this:

newmineral&nome, newmineral%cor etc isn't it??

Greetings and thanks a lot
 
baseados is an array. I'm not sure why baseados%nome doesn't give an error. It should unless it means something different. Either way, you have to input a single item not an array.

You can manipulate the data by either

1) accessing baseados(i) where i is the index
2) newmineral = baseados(i)
do your manipulations
baseados(i) = newmineral

 
I understood what u said about manipulating the data, but now i have a new problem!

That is really stupid!!

In my case(3) i have this:

case(3) ! Insere novo registo

write(*,*) "Introduza as caracteristicas do novo mineral - Numero, nome, cor, brilho e dureza!"
read(*,*) newmineral% numero, & !Criei um novo type porque o type basedados estava-me a dar erros
newmineral%nome, newmineral%cor, newmineral%brilho, &
newmineral%dureza


open (2, file="basedados.txt", iostat = ok, status='old', access='append')
write(2,*) newmineral

close(2)

But if run the program, that don't run, and don't show me my menu. But if i delete the last part "Open(2, file....) the program show the menu!
I don't understand it!

I don't know if i explained really well to u, but can u give me a hint of what it is??

Greetings
 
I already know why that happened! :)

I need a counter everytime I insert or delete a new mineral so that why the option didnt show up!

Yoopie! xD
 
A new problem! lol

Now i want to delete a mineral from my list from the keyboard, and i created a subroutine to do it, but it give me this error:

basedados.f95:68.20:

call apagar_min(newmineral,nummineral,apagamineral)
1
Error: Rank mismatch in argument 'minerais' at (1) (1 and 0)

What is happening??

My code is:

case(4) !

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

call apagar_min(newmineral,nummineral,apagamineral)


contains
! subroutine to delete a record in the matrix

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
! search the name to delete
do k=1, nminerais
if (nome == minerais(k)%nome)then
minerais(k) = minerais(nminerais) !

nminerais = nminerais-1
exit
endif
enddo
end subroutine

Is the subroutine ok??

Thanks again one more time mate :)
 
newmineral needs to be an array like baseados
numminteral needs to be an integer
apagamineral needs to be a character(50)

In the subroutine

1) subroutine missing
2) nome needs to be character*(*)

When it talks about ranks, it normally means that your dimensions are not quite right
 
I already did what u said i think, but it continues wrong!! :s
Can u help me please???


program basedados

implicit none

type mineral !Definicao de mineral com as suas caracterisitcas
integer:: numero
character(50) :: nome,cor, brilho
real :: dureza
end type mineral

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 *,"ano a apagar?"
read (*,*)apagamineral

call apagar_min(newmineral,nminerais,apagamineral)











end select








enddo

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 ! puxa um registo para trás
! garrafas(k:ngarrafas-1)=garrafas(k+1:ngarrafas)
minerais(k) = minerais(nminerais) ! ou coloca último no sítio do apagado
nminerais = nminerais-1
exit
endif
enddo
end subroutine







end program basedados

call apagar_min(newmineral,nminerais,apagamineral)
1
Error: Rank mismatch in argument 'minerais' at (1) (1 and 0)

there is my error
 
You need to rearrange your code to use modules
Code:
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 ! puxa um registo para trás
         ! garrafas(k:ngarrafas-1)=garrafas(k+1:ngarrafas)
         minerais(k) = minerais(nminerais) ! ou coloca último no sítio do apagado
         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 *,"ano a apagar?"
            read (*,*)apagamineral
                
            call apagar_min(basededados,nminerais,apagamineral)
                
       
        end select    
    enddo 
end program
 
Sorry my option 4 still not workin! :S

I looked and I can't understand why that its happening!! Is that problem in the subroutine??

Can u help me please??
 
What doesn't work about it?

o Does it not compile - if so what errors are you getting
o Does it crash when running - if it does, what kind of crash are you getting?
o Does it run but not do what is expected - if so, say what is expected.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top