20-04-2021
Fortran Read Last Line From File
How to read data from specific line in a file
How to read data from specific line in a file
Hi,
Here is my problem. My file is in the following format.
It has 5 columns with first two being integers and 3-5 are floating values.
file format (similar format with different values)
col 1 col 2 col 3 col 4 col 5
5001 1001 12.34 11.34 43.454
5001 1002 11.54 65.43 65.234
5001 1003 12.54 54.64 65.778
. . . . .
. . . . .
5001 1099 14.65 16.53 12.568
5001 1100 13.65 13.54 12.545
5002 1001 12.34 11.34 43.454
5002 1002 11.54 65.43 65.234
5002 1003 12.54 54.64 65.778
. . . . .
. . . . .
5002 1099 13.45 51.15 61.542
5002 1100 13.65 13.54 12.545
so on...
That means, for each number in column 1 i have 100 values. Or i should say the column 1 number doesnt change for for 100 rows and then it increases. Infact my original file has (5760 x 4633) rows.
Now here is how i need to read it:
There is a variable 'crhr' which returns a value which corresponds to any of the values in column 1. For example it may be 5002 or 5050 or anything. So when this variable has a value of 5002 what i need is i should be able to read the rows corresponding to the number 5002 and store it in an array.
My problem now is how do i make Fortran start reading the file from a particular line based on the value returned by the variable 'crhr'
thanks
sougandh
Here is my problem. My file is in the following format.
It has 5 columns with first two being integers and 3-5 are floating values.
file format (similar format with different values)
col 1 col 2 col 3 col 4 col 5
5001 1001 12.34 11.34 43.454
5001 1002 11.54 65.43 65.234
5001 1003 12.54 54.64 65.778
. . . . .
. . . . .
5001 1099 14.65 16.53 12.568
5001 1100 13.65 13.54 12.545
5002 1001 12.34 11.34 43.454
5002 1002 11.54 65.43 65.234
5002 1003 12.54 54.64 65.778
. . . . .
. . . . .
5002 1099 13.45 51.15 61.542
5002 1100 13.65 13.54 12.545
so on...
That means, for each number in column 1 i have 100 values. Or i should say the column 1 number doesnt change for for 100 rows and then it increases. Infact my original file has (5760 x 4633) rows.
Now here is how i need to read it:
There is a variable 'crhr' which returns a value which corresponds to any of the values in column 1. For example it may be 5002 or 5050 or anything. So when this variable has a value of 5002 what i need is i should be able to read the rows corresponding to the number 5002 and store it in an array.
My problem now is how do i make Fortran start reading the file from a particular line based on the value returned by the variable 'crhr'
thanks
sougandh
Fortran Read Data From File
Fortran uses the unit number to access the file with later read and write. The last write statement prints whatever divisors are left over after the last full line of 10. Fortran allows you to read data from, and write data into files. In the last chapter, you have seen how to read data from, and write data to the terminal.
Fortran Read Write
- Reading a file line by line is a trivial problem in many programming languages, but not in C. The standard way of reading a line of text in C is to use the fgets function, which is fine if you know in advance how long a line of text could be. You can find all the code examples and the input file at the GitHub repo for this article.
- Python: Read a CSV file line by line with or without header; Python: Read a file in reverse order line by line; Python: Search strings in a file and get line numbers of lines containing the string; Python: Get last N lines of a text file, like tail command; Python: Three ways to check if a file is empty; 5 Different ways to read a file line.