- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello
I am new to fortran and I have aproblem in the i/o unit open() statement. The following code is part of a book example.For the input file in the example a ".dat" file named "filename.dat" is created and when prompted "what is the input file name" the "filename.dat" is written.My problem is first how can I create a ".dat" file like this , and secondly If I would have a file saved in an editor like "notepad" how can I refer to that input file?
thanks a lot.
[bash]program bob implicit none character(len=20) :: filename integer :: nvals=0 , status real :: valu write(*,*) 'what"s the input file name:' read(*,*) filename write(*,1000) filename 1000 format(1X,'the name of the file is:',A) open(unit=3,file=filename,status='old',action='read',iostat=status) if(status==0) then do read(3,*,iostat=status) valu if(status /= 0) exit nvals=nvals+1 write(*,1010) nvals,valu 1010 format(1X,'For line ',I6 ,'the value is =',F10.4) end do if(status>0) then write(*,1020) nvals+1 1020 format(1X,'An error occured reading line No ',I6) else write(*,1030) nvals 1030 format(1X,'End of file reached. There were ',I6,' values') end if else write(*,1040) status 1040 format(1X,'error opening file. Iostat = ', I6) end if close(unit=3) end program bob[/bash]
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This sort of looks like a homework assignment.
The program expects that the file it reads is a text file with a series of numbers, one number per line. For example:
3.14
2
-456
It reads each line and then displays the line number and the value read. When it gets to the end, it exits.
You can use NOTEPAD or any text editor you like to create this file, and you can call it anything you want, though the program limits the length to 20 characters due to the declaration of variable "filename". When the program prompts you for the file name, you give the name you used. If the file is not in the program's "default directory", you'll need to give the path to the file (again subject to the 20 character limit). When running in Visual Studio, the default directory is the project directory. Otherwise it's the same directory the program executable is in.
Extra credit: Try putting things that are not numbers on a line and see what happens. Some things to try:
- Blank line
- The letter T
- A comma or a slash
- 3*4
What happens and why?
The program expects that the file it reads is a text file with a series of numbers, one number per line. For example:
3.14
2
-456
It reads each line and then displays the line number and the value read. When it gets to the end, it exits.
You can use NOTEPAD or any text editor you like to create this file, and you can call it anything you want, though the program limits the length to 20 characters due to the declaration of variable "filename". When the program prompts you for the file name, you give the name you used. If the file is not in the program's "default directory", you'll need to give the path to the file (again subject to the 20 character limit). When running in Visual Studio, the default directory is the project directory. Otherwise it's the same directory the program executable is in.
Extra credit: Try putting things that are not numbers on a line and see what happens. Some things to try:
- Blank line
- The letter T
- A comma or a slash
- 3*4
What happens and why?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Steve
Problem sovled,I moved the file to the project folder and it worked fine,besides this is the example
in "chapman's" Fortran book page 224 chapter 5.
Thanks for your great service.
Problem sovled,I moved the file to the project folder and it worked fine,besides this is the example
in "chapman's" Fortran book page 224 chapter 5.
Thanks for your great service.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page