×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Large file with variable length of both text and numbers!

Large file with variable length of both text and numbers!

Large file with variable length of both text and numbers!

(OP)
Hello,

I would like to read from a file and write into another file. The input file is very large and has variable length and has both text and numbers. How do I copy? If I read 80 characters at a time, the resulting file is less in size to the original file. Since the file is huge unable to find the diff.

Please help!

Thanks,
Vallabhi

RE: Large file with variable length of both text and numbers!

Quote:


How do I copy?
If you want o copy use the copy command from your operating system.

Quote:


If I read 80 characters at a time, the resulting file is less in size to the original file.
Probably you try to read the file line by line and you assume that the line length is limited to 80 characters, but in reality the lines are larger.

RE: Large file with variable length of both text and numbers!

I don't remember the details, but it could very well be that the opposite of what mikrom describes also happens...when you read a line that is less than 80 characters wide and turn around and write your character variable, it could be that the length of such written line always matches the declared length of the variable.

To visualize the difference between two text files you can use applications like tkdiff in Linux and Winmerge in Windows; there are others.

RE: Large file with variable length of both text and numbers!

(OP)
I cannot do it using OS commands, I need to do it in Fortran only. Thats because I am using sticky bit and it works only executable and not in shell script. Please help!

RE: Large file with variable length of both text and numbers!

I do not understand. I don't think the sticky bit has anything to do with you being able to read the file or not; sticky bit is there to not allow you to delete file even if you have write permission to the directory.

Who owns the file and what are the permission on it?

If all you need to do is copy the file, I am certain that you can do it directly from the shell for as long as the permissions are right for both source and destination.

In any case, if you want some help with your fortran program, post it, first; show us what you have done, what the results are and what it is you don't like about them...what it is that you would like to see happen.



RE: Large file with variable length of both text and numbers!

(OP)
I need to write to a file of different owner, So I created a fortran file with that owner and set the sticky bit, so that anybody can write into that file. Please see below the code I have written:

The excerpts code is as below:

OPEN(UNIT=ISRC, FILE=ARG1, STATUS='OLD', ACTION='READ')
OPEN(UNIT=IDST, FILE=ARG2, STATUS='REPLACE', ACTION='WRITE')
DO [
read (UNIT=ISRC, '(80A)', IOSTAT=IERR) CHAR
# write(*, '(80A)') char
IF (IERR != 0) EXIT
write(UNIT=IDST, '(80A)') char
]
close (ISRC)
close (IDST)

After execution, the input and output file are like below:

cidcsmp01:/home/mb131% ls -lrt temp4_33766.dat H29285
-rw-r--r-- 1 mb131 d0000000 1750960 Dec 5 03:41 temp4_33766.dat -- Input
-rw-r--r-- 1 dataload d0000000 878318 Dec 10 01:17 H29285 -- Output

As you can see the bytes are not the same.

Is this a buffer issue?

Please advise.

RE: Large file with variable length of both text and numbers!

How about

CODE

cp temp4_33766.dat H29285
chown newuser H29285
chmod 1644 H29285 
Apart from that, is the input file binary or text?

RE: Large file with variable length of both text and numbers!

(OP)
Dear xwb,

This is not that simple, ofcourse I know what you have told, but I need any user to execute this, and for that Fortran program is the only way with sticky bit set. I need to do the copying in fortran only. This fortran program is part of a large program and any user should be able to execute this.

Thanks Anyway!

Regards,
Vallabhi

RE: Large file with variable length of both text and numbers!

I tried this

CODE

program copyfile
  implicit none
  ! copies file record by record
  integer :: stat, rec_nr
  character(256) :: src_file, dst_file
  character :: my_rec

  if (iargc() .ne. 2) then
    write(*,*) 'Usage: copyfile <src_file> <dst_file>'
    stop
  end if 

  call getarg(1, src_file)
  call getarg(2, dst_file)

  open(unit = 10,file=src_file,access='DIRECT',status='OLD',RECL=1)
  open(unit = 20,file=dst_file,access='DIRECT',status='REPLACE',RECL=1)

  rec_nr = 1
  do
    ! read source
    read(10, rec=rec_nr, iostat=stat) my_rec
    if (stat .ne. 0) then
      go to 99
    end if
    ! write destination
    write(unit=20, rec=rec_nr) my_rec
    !
    rec_nr = rec_nr + 1
  end do

  ! close files
  99 continue
  close (10)
  close (20)
end program copyfile 

Output:

CODE

mikl1071@1_MIKL1071_01 ~/fortran
$ gfortran copyfile.f95 -o copyfile

mikl1071@1_MIKL1071_01 ~/fortran
$ copyfile ~/fortran/data/DATA-002.txt ~/fortran/data/copy_of_DATA.txt

mikl1071@1_MIKL1071_01 ~/fortran
$ ls -la  ~/fortran/data/*.txt
-rw-r--r--    1 mikl1071 Administ  1817498 May 10  2013 /home/mikl1071/fortran/data/DATA-002.txt
-rw-r--r--    1 mikl1071 Administ  1817498 Dec 10 10:18 /home/mikl1071/fortran/data/copy_of_DATA.txt 

RE: Large file with variable length of both text and numbers!

(OP)
Thank you so much mikrom.... It is working! The power of internet is infinity!

RE: Large file with variable length of both text and numbers!

Hi vallabhi,
I'm curious about the syntax of the code you posted above

CODE

DO [
...
# write(*, '(80A)') char
IF (IERR != 0) EXIT
...
] 
Is this any new dialect of fortran or a scripting language based on fortran which uses [..] for blocks and # for comments ?

RE: Large file with variable length of both text and numbers!

(OP)
This is ratfor code (.r files). Before compiling you have to do

$rafor < final.r > final.f

And then compile it.

RE: Large file with variable length of both text and numbers!

(OP)
Though my issue is resolved, I have one more question:

Please see below, I want to append 3 files together, How do I go about this? The last file is the huge one. First I have to replace then append two more files to the same file. Please advise.

OPEN(UNIT=ISRC, FILE=ARG1, STATUS='OLD', ACTION='READ')
OPEN(UNIT=IDST, FILE=ARG4, STATUS='REPLACE', ACTION='WRITE')
DO [
read (UNIT=ISRC, '(A)', IOSTAT=IERR) CHAR
# write(*, '(80A)') char
IF (IERR != 0) EXIT
write(UNIT=IDST, '(80A)') char
]
close (ISRC)
close (IDST)
ISRC = 3
IDST = 4
OPEN(UNIT=ISRC, FILE=ARG3, STATUS='OLD', ACTION='READ')
OPEN(UNIT=IDST, FILE=ARG4, ACCESS='APPEND',STATUS='OLD', ACTION='WRITE')
DO [
read (UNIT=ISRC, '(A)', IOSTAT=IERR) CHAR
# write(*, '(80A)') char
IF (IERR != 0) EXIT
write(UNIT=IDST, '(80A)') char
]
close (ISRC)
close (IDST)
ISRC = 7
IDST = 8
OPEN(UNIT=ISRC, FILE=ARG2, STATUS='OLD', ACTION='READ')
OPEN(UNIT=IDST, FILE=ARG4, ACCESS='APPEND',STATUS='OLD', ACTION='WRITE')
DO [
read (UNIT=ISRC, '(A)', IOSTAT=IERR) CHAR
# write(*, '(80A)') char
IF (IERR != 0) EXIT
write(UNIT=IDST, '(A)') char
]
close (ISRC)
close (IDST)

RE: Large file with variable length of both text and numbers!

I would do that similar as before

CODE

program appendfile
  implicit none
  ! appends files record by record
  integer :: stat, inp_rec_nr, out_rec_nr, nr_files, k
  character(30) :: file_0, app_file
  character :: my_rec

  nr_files = iargc()

  if (nr_files .le. 2) then
    write(*,*) 'Usage: appendfile <file_0> <app_file_1> .. <app_file_n>'
    stop
  end if 

  ! open the first file
  call getarg(1, file_0)
  open(unit = 10, file=file_0, access='DIRECT', status='OLD',RECL=1)

  ! count number of records in the first file
  out_rec_nr = 1 ! number of records in original (output) file
  do
    ! read source
    read(10, rec=out_rec_nr, iostat=stat) my_rec
    if (stat .ne. 0) then
      exit
    end if
    ! count records
    out_rec_nr = out_rec_nr + 1
  end do

  ! append all other files to the first file
  do k=2, nr_files
    ! get file for reading
    call getarg(k, app_file)
    open(unit = 20, file=app_file,access='DIRECT', status='OLD', RECL=1)

    inp_rec_nr = 1 ! number of records in each input file
    do
      ! read source
      read(20, rec=inp_rec_nr, iostat=stat) my_rec
      if (stat .ne. 0) then
        exit
      end if
      ! write destination
      write(unit=10, rec=out_rec_nr) my_rec    
      !
      inp_rec_nr = inp_rec_nr + 1  
      out_rec_nr = out_rec_nr + 1
    end do
    ! close input file
    close (20)
  end do

  ! close first (output) file
  close (10)
end program appendfile 

Usage:

CODE

mikl1071@1_MIKL1071_01 ~/fortran
$ gfortran appendfile.f95 -o appendfile

mikl1071@1_MIKL1071_01 ~/fortran
$ appendfile file_0.txt file_1.txt file_2.txt 
The example abowe appends file_1.txt and file_2.txt to file_0.txt

RE: Large file with variable length of both text and numbers!

Thanks for information. I never heard about RATFOR before. It's interesting.

RE: Large file with variable length of both text and numbers!

Ratfor is one of those preprocessor tools for people that came out in the early 80s. I've only heard of it because it was on many of the early PDP-11 Unix machines.

RE: Large file with variable length of both text and numbers!

Hi vallabhi,

I have typo in the program appendfile above.
Please correct

CODE

if (nr_files .le. 2) then
  write(*,*) 'Usage: appendfile <file_0> <app_file_1> .. <app_file_n>'
  stop
end if 
to

CODE

if (nr_files .lt. 2) then
  write(*,*) 'Usage: appendfile <file_0> <app_file_1> .. <app_file_n>'
  stop
end if 

RE: Large file with variable length of both text and numbers!

(OP)
Thanks a lot mikrom..... I have used the first tip and finished the development. Will get back to you in case of more questions! Thanks Again!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close