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!

changes in input files

Status
Not open for further replies.

samsami160

Technical User
Jan 8, 2014
7
I have a problem, I wrote a simple code that first imports a text file (text file includes one column of numbers) after reading text file, I want to delete numbers in 2,4,6,8,10,... rows) then export it to new file, my code is below:

PROGRAM a
REAL::b
open (2,file="INPUTS.txt",action="read",status="OLD")
open (3,file="OUTPUTS.txt",action="write",status="REPLACE")
do i= 1,40,2
READ(2,*)b
WRITE(3,*)(b)
END DO
CLOSE(2)
CLOSE(3)
END PROGRAM

I write do i= 1,40,2 to delete numbers in 2,4,6,8,10,... rows but my result in new file is not so, produced numbers in new file by applying do i= 1,40,2 are twenty first row.
 
The problem you have is that all you have done with the stride of 2 is turn a loop of 40 indexes to one of 20. The read will still read each line in turn and then write them out, so you'll just get the first 20 lines in the file. You could change the loop to this:

Code:
do i= 1,40,2
READ(2,*)b
READ(2,*)
WRITE(3,*)(b)
END DO

This loop will read two lines at a time, but only store and write the first one each time.
 
Thanks narlytep,I changed my code according what you said, but after executing code, a ERROR window appears , this window is:
FORTRAN APPLICATION7.exe has stopped working.

changed code is :

PROGRAM a
REAL::b
open (2,file="INPUTS.txt",action="read",status="OLD")
open (3,file="OUTPUTS.txt",action="write",status="REPLACE")
do i= 1,9,2
READ(2,*)b
READ(2,*)
WRITE(3,*)(b)

END DO
CLOSE(2)
CLOSE(3)

END PROGRAM
 
Hmm. It works for me. The only way I can get an error to appear is if my INPUTS.txt file only has 9 lines of data. You'll need a 10th otherwise it runs off the end of the file with the second read.
 
Thanks so much dear narlytep,my code works,if I want to delete numbers in 1,3,5,7,9,... rows) then export it to new file, how can I do to?
 
That's just a case of swapping the order of the two reads, so that the second read is the one that stores b.

Code:
do i= 1,9,2
READ(2,*)
READ(2,*)b
WRITE(3,*)(b)
END DO

There's probably a nice clever way to do this with an if statement so you can switch back and forth between the two options, but I'll leave that up to you.
 
Thanks dear narlytep.I have one questions about above code:
-number of data in above input file is 10,while number of data in above output file is 5,I want to produce 10-5=5 zeros (one-column)then add this column to output file.by doing this,size of input and output file will be same.
 
Easiest way is to have a second loop at the end that writes out 0 five times, before you close the file.
 
I appreciated you if you write this loop in code,I don't know what to write this loop to adding 5 zeros to end of output file.
thanks
 
Code:
PROGRAM a
REAL::b
open (2,file="INPUTS.txt",action="read",status="OLD")
open (3,file="OUTPUTS.txt",action="write",status="REPLACE")
DO i= 1,9,2
  READ(2,*)b
  READ(2,*)
  WRITE(3,*)(b)
END DO
b=0
DO i= 1,5
  WRITE(3,*) b
END DO
CLOSE(2)
CLOSE(3)

END PROGRAM
 
Thanks a lot.I have another question about above code:
How can I to multiply elements in input and output files,this means that element in 2 row of input file be multiplied by element in 1 row of output file,element in 4 row of input file be multiplied by element in 2 row of output file,element in 6 row of input file be multiplied by element in 3 row of output file,... ? if possible,please write related code.
Thanks dear narlytep for your help.
 
Hello amsami160 and narlytep,
What you try is a little bit naive. You don't know how many lines are in the input file. Rather read the input file until the end and use the line counter. Only if the line number is not even, then print the line.

 
Hi mikrom
my problem is different of what you answer,I have 2 files,input file have 10 numbers(one column)and output file have 10 numbers too.
How can I to multiply elements in input and output files,this means that element in 2 row of input file be multiplied by element in 1 row of output file,element in 4 row of input file be multiplied by element in 2 row of output file,element in 6 row of input file be multiplied by element in 3 row of output file,... ? if possible,please write related code.
 
samsami160 said:
...
I have 2 files,input file have 10 numbers(one column)and output file have 10 numbers too.
How can I to multiply elements in input and output files,this means that element in 2 row of input file be multiplied by element in 1 row of output file
...
Then I would say both of your files are input files. For example read input file1 in array1 and input file2 in array2 and with the arrays you can compute what you need.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top