Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6949267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:55:54+00:00 2026-05-27T13:55:54+00:00

I would like to read coordinates from an from an input file. An example

  • 0

I would like to read coordinates from an from an input file. An example input file would look something like :

1    0.1542    0.2541    1.2451   N
12   4.5123    2.0014    2.0154   O
43   8.2145    0.2978    4.2165   H

etc… The size of this file is variable. The first column is a number assigned to an atom, the following columns are its x,y,z coordinates, and the final column is the elemental symbol of the atom.

I tried something along the lines of:

integer, allocatable :: atnum(:)
double precision, allocatable :: coord(:,:)
character(len=2), allocatable :: element(:)

open(unit=20, file='input', status='old',action='read')

read(20,*,end=200) atnum, coord(:,1:3), element
200 close(20)

This throws me the error:

Fortran runtime error: Bad integer for item 2 in list input

I assume that the program read the first entry into atnum(1), but then tried to continue reading into the second entry of the first row into atnum(2). How can I get it to read the input correctly?

I also think that there might be a problem with telling the program to read the middle three columns into coord(:,1:3). It is likely that it will read the first three entries into coord(1,1), coord(2,1), coord(3,1), then run into the character at the end of the line and become confused again. How can I tell it to fix the first subscript for the line, and read into the other dimension? Or will I have to swap the indices, like coord(1:3,:)? Will that work?

EDIT: The above has been answered by tpg2114, but I still have a problem. I can’t allocate the the array until I know how many sets of coordinates are to be read, but I only know how many atoms there are until I reach the end of the file. The program compiles fine if I don’t allocate atnum, coord and element, but returns a segmentation fault when I try to run it. How can I get it to read into the dynamic arrays without previously allocating them?

It sounds similar to this question: Variable size arrays in Fortran without Allocate()

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:55:55+00:00Added an answer on May 27, 2026 at 1:55 pm

    You need to loop over the READ statement. A single READ will only go to the end of the record, in this case the line. You will know when you run out of lines because you put the end=200 argument, so it will jump to line 200 (which you don’t seem to have here).

    So, you need to make sure:

    A) Your arrays are long enough to contain the file (you don’t show the allocation for them, I assume you allocated them?)

    B) You loop over the READ statement

    C) You specify an index for the atnum argument and the first argument of coord and the elem argument.

    For example, assuming the declarations are the same and the arrays are allocated large enough, and I is an INTEGER

    I = 1
    DO 
      read(20,*,end=200) atnum(I), coord(I,1:3), elem(I)
      I = I + 1
    END DO 
    200 continue
    

    Also, consider dropping the end=200 part and using IOSTAT instead since that is modern and line numbers are deprecated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to read a text file and input its contents into an
I would like to read each line of text from the file outputted from
I would like to read a DICOM file in C#. I don't want to
I would like to read the last 1 megabyte of a MP3 file and
In java i would like to read a file line by line and print
I would like to read a file which has several lines, and then search
I would like to read a thin YAML file using a simple C program.
I would like to read a text file into an array of strings using
I'm trying to load some XY-coordinates from a asc-file. It looks like this: -55.988544
I would like to read the text and binary attachments in a saved Outlook

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.