How to deal with a .dat file containing characters

  • Thread starter lilihoso
  • Start date
  • Tags
    File
In summary: Also, it's possible that the program is reading beyond the end of the input file, then getting an error on the subsequent read statement. You might try appending a line of text to the end of the input file (after the last of the data lines), and see if your program reads that line without error.In summary, the conversation is about using Fortran 90 to read a .dat file with columns of characters and perform mathematical calculations using the first 3 columns in date format. The expert suggests using a specific format in the 'read' statement and using the 'x' specifier to skip non-numeric characters. The user then asks for help with reading a matrix with 1500 lines and the expert suggests checking the input file
  • #1
lilihoso
5
0
Hi everyone,

I'm programming witn Fortran 90.

Could anyone help me to read a .dat file who contains some columns of characters, it looks like the following:

2011/03/09 02:45:20.33 38.4350 142.8420 32.00 7.50 Mw 550 0.98 NEI 201103092006
2011/03/09 02:57:16.67 38.3610 142.9100 23.20 5.70 Mb 339 0.98 NEI 201103092008
2011/03/09 03:05:52.24 38.2770 142.8420 41.90 4.60 Mb 39 0.74 NEI 201103092011
2011/03/09 04:05:54.98 38.6550 142.7530 35.00 5.20 Mb 135 1.36 NEI 201103092017
2011/03/09 04:15:40.53 38.6890 142.9680 39.20 4.80 Mb 73 1.07 NEI 201103092019

I'd like to make it possible for mathematical calculations with the first 3 columns who are in date format.

Thank you very much in advance!
 
Technology news on Phys.org
  • #2
If each line has the same format, you can specify a format in your 'read' statement and use the 'x' specifier to skip over the non-numeric characters and blank spaces. For example, if you read from unit 10 and your variables are integers:

Code:
read (10, '(i4,1x,i2,1x,i2,…)') year, month, day, …

You can probably fill in the rest of the statement. Or if the variables are real:

Code:
read (10, '(f4.0,1x,f2.0,1x,f2.0,…)') year, month, day, …
 
  • #3
Thank you so much jtbell! It's done!
 
  • #4
Hi jtbell,

I get another problem with my 'matrix', I tried to read my whole matrix which contains 1500 lines with the same form using the statement following:
open(10, file='eq_data.dat', form='formatted', status='old', action='read', iostat=ios)
if (ios .ne. 0) stop "Error!"
ios2=0
i=1
do while (ios2 .eq. 0)
read(10, '(i4,1x,i2,1x,i2,1x,i2,1x,i2,1x,f5.2,2x,f7.4,2x,f8.4,2x,f5.2,2x,f4.2)', iostat=ios2) year(i),month(i),&
day(i),hour(i), min(i), sec(i), lat(i), long(i), dep(i), mag(i)
i=i+1
enddo
close(10)

do i=1,15
write(*,*)year(i), month(i), day(i), hour(i), min(i), sec(i), lat(i), long(i), dep(i), mag(i)
enddo

But it can't never write the correct things from i=10.

Could you tell me where the error is?

Thank you so much again!
 
  • #5
I would first suspect that the first loop is reading only the first ten (or so) lines of the data file. Try inserting (for testing purposes) the following statement immediately after the 'read' statement:

Code:
write(*,*) i, year(i), month(i), day(i), hour(i), min(i), sec(i), lat(i), long(i), dep(i), mag(i)

Note this also displays the value if 'i'. Also "comment out" the second do-loop so you see only the output from the first one (put a 'c' at the beginning of each line).

If indeed the program reads only to i = 10 (or so), I would examine the input file very carefully near that line.
 

Related to How to deal with a .dat file containing characters

1. How do I open a .dat file containing characters?

To open a .dat file containing characters, you will need to use a text editor or a specialized software program that can read and interpret the data. Some popular options include Notepad, Sublime Text, or Microsoft Excel.

2. Can I edit a .dat file containing characters?

Yes, you can edit a .dat file containing characters using a text editor or specialized software program. However, it is important to be cautious when making changes to the file, as any incorrect modifications could corrupt the data.

3. How do I convert a .dat file to a different format?

To convert a .dat file to a different format, you will need to use a file conversion tool or a specialized software program. Some options include Total CSV Converter, FileZigZag, or Microsoft Excel. Be sure to follow the instructions carefully to ensure the data is converted accurately.

4. How can I view the contents of a .dat file containing characters?

You can view the contents of a .dat file containing characters by opening it in a text editor or specialized software program. You can also use a command line tool, such as Notepad or grep, to print the contents of the file to your terminal or console window.

5. Is it possible to recover data from a corrupted .dat file?

In some cases, it may be possible to recover data from a corrupted .dat file. You can try using a data recovery tool or reaching out to a professional data recovery service. However, there is no guarantee that all of the data can be recovered, so it is important to regularly backup important files.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top