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!

My objective is to do a database

Status
Not open for further replies.

danielinhu

Technical User
Jun 22, 2010
23
Hello guys,

The point of my work in Fotran is to do a database of minerals in fortran 95.

So i have a list of minerals in excell.csv to export to fortran, and with that list i have a few options.
Each mineral have 7 characteristics- like name, colour, etc.
And the program should work like this:

1- See the list of minerals
2- Insert a new mineral
3- Delete a mineral
4- Edit a characteristic of a mineral
5- Organize them by name, or other characteristic
0- End the program


My idea is to do this, with "Select case". But my biggest problem is to read the data from the csv excell file in fortran.

Can somebody help me please?
Or give me some instructions?
My excell file is in attachement

Greetings, and keep the good work
:)
 
Just read a Line

CHARACTER(LEN=255) :: Line

READ(1,'(A255)')Line

and look for the separating comma using

i1=INDEX(Line,',')
i2=INDEX(Line(i1+1:255),',') and so on.

You can then store Line(i1+1:i2) in your variable where I'd use a derived type.
 
Does someone have a program like the one I need to do? Because I want to see some parts!

Greetings
 
danielinhu said:
Does someone have a program like the one I need to do? Because I want to see some parts!
In the link above I showed you one part: how to read a CSV-file.
What you ask sounds like you want, that somebody here code your whole school work for you. If you want further help, you need to show some effort, what have you done so far.
We are not here to offer a free coding service.
:)
 
Allright!
I will keep working in the program and after I will show it here, and i will keep in touch to get some help!

And thank u ! :)
 
Mikrom I have a doubt.

I already have my database almost ready but i cant do a thing!

In my option 4 I want to insert a name of a mineral and delete it from the list. Can u help me with that??

There is my 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
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
 
You have a little bug in subroutine apagar_min: the 3.argument nome is declared only as 1 character and it should be 50 characters long
Change to
Code:
subroutine apagar_min(minerais,nminerais,nome)
  ...
  character(50), intent(in) :: nome
  ...
Note, that in the subroutine apagar_min you only delete the mineral from the array. To save this changes to the database you need to program the option 7 -Save Basedados.csv
Here is the modified program, with the bug above corrected and with option 7 added:
Code:
[COLOR=#a020f0]module[/color] mineralmod

[COLOR=#2e8b57][b]type[/b][/color] mineral [COLOR=#0000ff]!Definicao de mineral com as suas caracterisitcas[/color]
    [COLOR=#2e8b57][b]integer[/b][/color]:: numero
    [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]50[/color]) :: nome,cor, brilho
[COLOR=#2e8b57][b]    real[/b][/color] :: dureza
[COLOR=#2e8b57][b]end type[/b][/color] mineral
[COLOR=#a020f0]contains[/color]
[COLOR=#0000ff]! subrotina para APAGAR um registo de uma matriz[/color]
[COLOR=#a020f0]subroutine[/color] apagar_min(minerais,nminerais,nome)
  [COLOR=#0000ff]! parametros[/color]
  [COLOR=#2e8b57][b]type[/b][/color] (mineral), [COLOR=#2e8b57][b]dimension[/b][/color](:), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]inout[/b][/color]) :: minerais
  [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]inout[/b][/color]) :: nminerais
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]50[/color]), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: nome
  [COLOR=#0000ff]! variáveis internas[/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: k
  [COLOR=#0000ff]! procura ano a apagar[/color]
  [COLOR=#804040][b]do[/b][/color] k[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], nminerais
    [COLOR=#804040][b]if[/b][/color] (nome [COLOR=#804040][b]==[/b][/color] minerais(k)%nome)[COLOR=#804040][b]then[/b][/color]
       [COLOR=#0000ff]! move last element to the k-th position[/color]
       [COLOR=#804040][b]if[/b][/color] (k [COLOR=#804040][b]<[/b][/color] nminerais) [COLOR=#804040][b]then[/b][/color]      
         minerais(k) [COLOR=#804040][b]=[/b][/color] minerais(nminerais)
       [COLOR=#804040][b]end if[/b][/color]
       [COLOR=#0000ff]! decrease number of elements [/color]
       nminerais [COLOR=#804040][b]=[/b][/color] nminerais[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]
       [COLOR=#804040][b]exit[/b][/color]
    [COLOR=#804040][b]endif[/b][/color]
  [COLOR=#804040][b]enddo[/b][/color]
[COLOR=#a020f0]end subroutine[/color]
[COLOR=#a020f0]end module[/color] mineralmod

[COLOR=#a020f0]program[/color] basedados
  [COLOR=#a020f0]use[/color] mineralmod    
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  
  
  [COLOR=#2e8b57][b]type[/b][/color] (mineral), [COLOR=#2e8b57][b]dimension[/b][/color]([COLOR=#ff00ff]1200[/color]) :: basededados
  [COLOR=#2e8b57][b]type[/b][/color] (mineral):: newmineral
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]50[/color]):: apagamineral
  [COLOR=#2e8b57][b]integer[/b][/color]:: opcao[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], i, ok, fim_fich,n, nminerais[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]0[/color]
  [COLOR=#2e8b57][b]integer[/b][/color]::nummineral
  
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"*.*.*.*.*.*.*.*.* BASE DE DADOS DE MINERAIS *.*.*.*.*.*.*.*.*"[/color]
  
  [COLOR=#804040][b]do[/b][/color] [COLOR=#804040][b]while[/b][/color](opcao[COLOR=#804040][b]/=[/b][/color][COLOR=#ff00ff]0[/color])
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"('')"[/color]
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"('1-Load minerals from file')"[/color]
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"('2-List all minerals')"[/color]
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"('3-Insert new mineral')"[/color]
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"('4-Delete mineral')"[/color]
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"('5-xxxx')"[/color]
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"('6-xxxxx')"[/color]
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"('7-Save minerals to file')"[/color]
    [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"('0-Fim')"[/color]
    [COLOR=#804040][b]read[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) opcao
    
    [COLOR=#804040][b]select case[/b][/color](opcao)
        
    [COLOR=#804040][b]case[/b][/color]([COLOR=#ff00ff]1[/color]) [COLOR=#0000ff]! Load minerals from file into array [/color]
      [COLOR=#804040][b]open[/b][/color] ([COLOR=#ff00ff]2[/color], [COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]"basedados.txt"[/color], [COLOR=#804040][b]iostat[/b][/color] [COLOR=#804040][b]=[/b][/color] ok, [COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color]) 
      [COLOR=#804040][b]if[/b][/color] (ok [COLOR=#804040][b]==[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
        [COLOR=#804040][b]do[/b][/color] n[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]1200[/color]
          [COLOR=#804040][b]read[/b][/color] ([COLOR=#ff00ff]2[/color], [COLOR=#804040][b]*[/b][/color], [COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]fim_fich)basededados(n)
          [COLOR=#804040][b]if[/b][/color] (fim_fich [COLOR=#804040][b]<[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]exit[/b][/color]
        [COLOR=#804040][b]end do[/b][/color]
        nminerais [COLOR=#804040][b]=[/b][/color] n[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]
        [COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]2[/color])
      [COLOR=#804040][b]else[/b][/color]
        [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"erro na aberura do ficheiro:"[/color],ok
      [COLOR=#804040][b]end if[/b][/color]
      [COLOR=#804040][b]print[/b][/color][COLOR=#804040][b]*[/b][/color], [COLOR=#ff00ff]'.:Base de dados carregada com sucesso:.'[/color]
    
    [COLOR=#804040][b]case[/b][/color]([COLOR=#ff00ff]2[/color]) [COLOR=#0000ff]! List all minerals[/color]
      [COLOR=#804040][b]if[/b][/color] (nminerais [COLOR=#804040][b]>[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color] 
        [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"(i5,2x,a11,2x,a10,2x,a10,2x,f6.1)"[/color], basededados([COLOR=#ff00ff]1[/color]:nminerais)
      [COLOR=#804040][b]else[/b][/color]
        [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'The list of minerals is empty !'[/color]
      [COLOR=#804040][b]end if[/b][/color] 
    
    [COLOR=#804040][b]case[/b][/color]([COLOR=#ff00ff]3[/color]) [COLOR=#0000ff]! Insert new mineral[/color]
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"New mineral - number, name, cor, brilho e dureza"[/color]
      [COLOR=#804040][b]read[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) newmineral%numero, newmineral%nome, newmineral%cor, [highlight #ffff00][COLOR=#0000ff]&[/color][/highlight]
                newmineral%brilho,newmineral%dureza      
      basededados(nminerais [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color])[COLOR=#804040][b]=[/b][/color]newmineral
      nminerais [COLOR=#804040][b]=[/b][/color] nminerais [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]

    [COLOR=#804040][b]case[/b][/color]([COLOR=#ff00ff]4[/color]) [COLOR=#0000ff]! Delete mineral[/color]
      [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]"Mineral to delete??"[/color]
      [COLOR=#804040][b]read[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) apagamineral
      [COLOR=#a020f0]call[/color] apagar_min(basededados,nminerais,apagamineral)

    [COLOR=#804040][b]case[/b][/color]([COLOR=#ff00ff]7[/color]) [COLOR=#0000ff]! write array to the file[/color]
      [COLOR=#804040][b]open[/b][/color] ([COLOR=#ff00ff]2[/color], [COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]"basedados.txt"[/color], [COLOR=#804040][b]iostat[/b][/color] [COLOR=#804040][b]=[/b][/color] ok, [COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'unknown'[/color]) 
      [COLOR=#804040][b]if[/b][/color] (ok [COLOR=#804040][b]==[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
        [COLOR=#804040][b]do[/b][/color] n[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], nminerais
          [COLOR=#804040][b]write[/b][/color] ([COLOR=#ff00ff]2[/color], [COLOR=#804040][b]*[/b][/color]) basededados(n)
        [COLOR=#804040][b]end do[/b][/color]
        [COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]2[/color])
      [COLOR=#804040][b]else[/b][/color]
        [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"error opening file"[/color],ok
      [COLOR=#804040][b]end if[/b][/color]
      [COLOR=#804040][b]print[/b][/color][COLOR=#804040][b]*[/b][/color], [COLOR=#ff00ff]'Array written succesfully'[/color]
    [COLOR=#804040][b]end select[/b][/color]     
  [COLOR=#804040][b]enddo[/b][/color] 
[COLOR=#a020f0]end program[/color]
Sample session:
Code:
$ g95 basedados.f95 -o basedados

$ basedados
 *.*.*.*.*.*.*.*.* BASE DE DADOS DE MINERAIS *.*.*.*.*.*.*.*.*

1-Load minerals from file
2-List all minerals
3-Insert new mineral
4-Delete mineral
5-xxxx
6-xxxxx
7-Save minerals to file
0-Fim
1
 .:Base de dados carregada com sucesso:.

1-Load minerals from file
2-List all minerals
3-Insert new mineral
4-Delete mineral
5-xxxx
6-xxxxx
7-Save minerals to file
0-Fim
2
    1  aaa          b           c              1.0
    2  xxx          y           z              2.0
    3  ttt          u           v              3.0

1-Load minerals from file
2-List all minerals
3-Insert new mineral
4-Delete mineral
5-xxxx
6-xxxxx
7-Save minerals to file
0-Fim
4
 Mineral to delete??
aaa

1-Load minerals from file
2-List all minerals
3-Insert new mineral
4-Delete mineral
5-xxxx
6-xxxxx
7-Save minerals to file
0-Fim
2
    3  ttt          u           v              3.0
    2  xxx          y           z              2.0

1-Load minerals from file
2-List all minerals
3-Insert new mineral
4-Delete mineral
5-xxxx
6-xxxxx
7-Save minerals to file
0-Fim
0

$

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top