Topic: Coding assistance

Hi all,

Any assistance with regards my request.

I am new to programming and Fortran.

I wanted to know how to do the following:

1. Open a text file.
2. Insert "text" at a particular line number in the text file and/or before certain text in the file.
3. Save the file.
4. Close the file.

I would even pay for someone to assist me with this as it is a rush!

Where can I learn Fortran and/or do online training courses etc.

Thank you in advance.

Re: Coding assistance

Sounds like a typical programming course lab assignment.

There are many ways to do this task.  You can't really "insert" lines into a text file, but you can copy the lines above the insert to a scratch file, write out the "insert lines", and then copy the rest of the existing lines into the scratch file.  Once that is done, you could delete the original file and rename the scratch file and you are done.

However, none of this will make any sense until you learn some basic Fortran.  If you google Fortran tutorial you will find many online resources to get you started.

Re: Coding assistance

Hi all!

I received assistance from Dr Walt Brainerd of the Fortran Company.

See Fortran code for this request:

program Gary

   implicit none
   character(len=*), parameter :: text = "Gary"
   character(len=100) :: line
   integer :: gary_unit, ios
   logical :: found_it

   open (newunit=gary_unit, file="gary_file", &
         status="old", action="read", position="rewind")
   found_it = .false.
   do
      read(unit=gary_unit, fmt="(a)", iostat=ios) line
      if (is_iostat_end(ios)) exit
      if (index(line, text) > 0) then
         ! Uncomment this "if" test to insert the line just once
!         if (.not. found_it) &
            write(unit=*, fmt="(a)") "Line to be inserted"
         found_it = .true.
      end if
      write(unit=*, fmt="(a)") trim(line)
   end do

end program Gary

Here is the input file "gary_file". Create it with this name, no file extension.

line 1 aaaaaaaaa
line 2 bbbGarybbbb
line 3 ccccccc
line 4 Gary ddddd
line 5 eeee

Here is the output:

line 1 aaaaaaaaa
Line to be inserted
line 2 bbbGarybbbb
line 3 ccccccc
Line to be inserted
line 4 Gary ddddd
line 5 eeee