How can I read only the real data from a txt file in Fortran 90?

  • Context: Fortran 
  • Thread starter Thread starter msabet
  • Start date Start date
  • Tags Tags
    Data Reading
Click For Summary

Discussion Overview

The discussion revolves around reading real data from a text file in Fortran 90, specifically focusing on how to ignore the initial comment lines in the file. The scope includes technical explanations and proposed coding solutions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes the format of the text file and expresses the need to read only the real data while ignoring the first two lines.
  • Another participant emphasizes the importance of knowing the file's precise format and suggests reading the initial comment lines into a character variable before processing the data.
  • A different participant reiterates that reading the lines into a variable is not necessary unless for verification purposes, and mentions a method to read and ignore the lines.
  • One participant requests a sample code to illustrate how to read the data correctly.
  • A later reply indicates that the issue has been resolved, expressing gratitude for the assistance received.

Areas of Agreement / Disagreement

There is no clear consensus on the best method to read the data, as participants offer different approaches and suggestions. The discussion remains somewhat unresolved regarding the optimal solution.

Contextual Notes

Participants mention potential errors related to input-variable types and the use of specific formats, but these issues are not fully explored or resolved.

msabet
Messages
4
Reaction score
0
hi dears
I have a txt file as follows:
"
x1 Xi x2 x-EN y-EN EA EB ma gnetic
------------------------------------------------------&------------
.0010 -.9990 .7322 .4042 -.3280 -.0002 -.3279 .0010
.0010 -.9990 .7322 .4042 -.3280 -.0002 -.3279 .0010
.0010 -.9800 .7210 .3917 -.3293 -.0030 -.3262 .0010
.0010 -.9700 .7153 .3855 -.3297 -.0045 -.3252 .0010
.0010 -.9600 .7096 .3796 -.3300 -.0060 -.3241 .0010
.0010 -.9500 .7041 .3739 -.3302 -.0074 -.3228 .0010
.0010 -.9400 .6987 .3684 -.3302 -.0089 -.3214 .0010
.0010 -.9300 .6933 .3632 -.3301 -.0103 -.3199 .0010
.0010 -.9200 .6880 .3581 -.3299 -.0117 -.3183 .0010
.0010 -.9100 .6829 .3533 -.3296 -.0130 -.3166 .0010
"
i want to read this data in fortran 90 but i need only the real data. So I don't know how i can read them in such a way that the first two line is ignored. using /-format the .exe file can'nt read the real data and an error related to input-variable type is appeared.
how you can help me.
thanks
 
Last edited:
Technology news on Phys.org


Needless to say, whenever you are reading a file, you need to know ahead of time its precise format...otherwise, how would you know what to read where?

So, if you know that your file starts with 2 or 3 comment lines, all you need to do is read those line into a character variable (string) long enough to read the entire line, say, 80 characters long. After you are done reading those 2 heading lines explicitly, then you can move on to some kind of loop where you can read your data.
 


gsal said:
So, if you know that your file starts with 2 or 3 comment lines, all you need to do is read those line into a character variable (string) long enough to read the entire line, say, 80 characters long.

You don't need to read the lines into a variable, unless you want to print them out as a check you are reading the right file, or whatever.
Code:
read(1,*)
will read a line and ignore its contents.
 
msabet said:
hi dears
I have a txt file as follows:
"
x1 Xi x2 x-EN y-EN EA EB ma gnetic
------------------------------------------------------&------------
.0010 -.9990 .7322 .4042 -.3280 -.0002 -.3279 .0010
.0010 -.9990 .7322 .4042 -.3280 -.0002 -.3279 .0010
.0010 -.9800 .7210 .3917 -.3293 -.0030 -.3262 .0010
.0010 -.9700 .7153 .3855 -.3297 -.0045 -.3252 .0010
.0010 -.9600 .7096 .3796 -.3300 -.0060 -.3241 .0010
.0010 -.9500 .7041 .3739 -.3302 -.0074 -.3228 .0010
.0010 -.9400 .6987 .3684 -.3302 -.0089 -.3214 .0010
.0010 -.9300 .6933 .3632 -.3301 -.0103 -.3199 .0010
.0010 -.9200 .6880 .3581 -.3299 -.0117 -.3183 .0010
.0010 -.9100 .6829 .3533 -.3296 -.0130 -.3166 .0010
"
i want to read this data in fortran 90 but i need only the real data. So I don't know how i can read them in such a way that the first two line is ignored. using /-format the .exe file can'nt read the real data and an error related to input-variable type is appeared.
how you can help me.
thanks
ok. can you give me a sample, e.g. in the above data?please
 
msabet said:
ok. can you give me a sample, e.g. in the above data?please

It's been solved. with gratitude
thanks
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
19K