[prog] FORTRAN read with dos carriage returns
Conor Daly
c.daly at met.ie
Wed Oct 5 03:11:56 EST 2005
Ha! I have a file which comes from an MS system and so contains +AFw-r +AFw-n
carriage return / linefeed pairs. We're reading these with a fortran
program but, we first pass the file through "tr -d '+AFw-r'" to strip the +AFw-r
characters. However, a file inexplicably failed to be stripped and, having
forgotten about the 'tr' stuff, we were at a loss to understand the
behaviour of the Fortran program. Anyhow, after much troubleshooting, we
discover that a read() statement is barfing when there is a +AFw-r+AFw-n at the end of
the line instead of a +AFw-r. The assumption is that the +AFw-r somehow sends the
read statement back to the beginning of the string and the next thing read
is an end of line so the string ends up empty. I confirmed this by adding
the string 'test' between the +AFw-r and the +AFw-n which appears at the beginning
of the line using more but 'od -c' shows up the +AFw-rtest+AFw-n sequence.
Anyway, the question is, is there a way in FORTRAN to skip over a +AFw-r in a
read statement? It's not guaranteed that the +AFw-r will always be in the same
position so I can't just read x characters and then skip one.
I can do it in C with an 'if(string[i] == '+AFw-r') <skip it>' sort of statement
but I don't know how to do it in Fortran.
Sample files and code below...
use 'od -c <file>' to see what is in the files
File <cdtemp_r_n> contains the original data including a '+AFw-r+AFw-n' pair at the
end of each line.
File <cdtemp_n> contains the original data after "tr -d '+AFw-r'" .
File <cdtemp> contains the string "test" between the '+AFw-r' and '+AFw-n' chars.
This will look odd when viewed with more.
loadrad.f is the fortran source. Compile with "g77 -o loadrad loadrad.f"
load.c is the C source. Compile with "gcc -o load load.c". When run with
just a filename, it uses '+AFw-n' as end of line (eg. ./load cdtemp). When run
with a second argument, it discards '+AFw-r' characters (eg. ./load cdtemp n).
So, is there a way to achieve this in fortran?
Cheers,
Conor
--
Conor Daly, Please avoid sending me
Met Eireann, Glasnevin Hill, Word or PowerPoint attachments.
Dublin 9, Ireland http://www.fsf.org/philosophy/no-word-attachments.html
Ph +-3531 8064276 Fax +-3531 8064247
*********************************************************************************
This e-mail and any files transmitted with it are confidential and intended solely for the addressee. If you have received this email in error please notify the sender.
This e-mail message has also been scanned for the presence of computer viruses.
T+AOE an r+AO0-omhphost seo, agus aon chomhad at+AOE nasctha leis, faoi r+APo-n agus is don t+AOk a seoladh chuige amh+AOE-in +AOk. M+AOE tharla go bhfuair t+APo an r+AO0-omhphost seo tr+AO0 dhearmad cuir in i+APo-l don t+AOk a sheol +AOk led+IBk thoil.
T+AOE an teachtaireacht r+AO0-omhphoist seo scuabtha le bogearra+AO0 frithv+AO0-reas.
********************************************************************************
-------------- next part --------------
7411 5 7 1 2 17 32 65131155146192206158 92 87 83 82 53 18 7 2 1528
test
7411 5 7 2 1 6 14 23104140 92109 70196220151 46 29 15 11 7 4 1238
test
7411 5 7 3 2 28 37 50130131117275220149222142 93 29 13 10 11 1 1660
test
-------------- next part --------------
7411 5 7 1 2 17 32 65131155146192206158 92 87 83 82 53 18 7 2 1528
7411 5 7 2 1 6 14 23104140 92109 70196220151 46 29 15 11 7 4 1238
7411 5 7 3 2 28 37 50130131117275220149222142 93 29 13 10 11 1 1660
-------------- next part --------------
7411 5 7 1 2 17 32 65131155146192206158 92 87 83 82 53 18 7 2 1528
7411 5 7 2 1 6 14 23104140 92109 70196220151 46 29 15 11 7 4 1238
7411 5 7 3 2 28 37 50130131117275220149222142 93 29 13 10 11 1 1660
-------------- next part --------------
#include<stdio.h>
int main(int argc, char *argv[]) {
FILE *iptr;
int i;
char string[200];
if((iptr = fopen(argv[1], "r")) == NULL) {
printf("cannot open file %s+AFw-n", argv[1]);
} else {
printf("opened file %s+AFw-n", argv[1]);
}
while(! feof(iptr) ) {
for(i=0; i<200; i+-+-) {
fscanf(iptr, "%c", &string[i]);
if(argc > 2) {
if(string[i] == '+AFw-r') i--;
}
if(string[i] == '+AFw-n') {
string[i]= '+AFw-0';
break;
}
}
printf("received:%s:%d chars+AFw-n", string, i);
}
return (0);
}
-------------- next part --------------
character*200 string
character*40 fil
write(*,9)
9 format(' '+AC8ALw,' enter input file name')
read(*,10)fil
10 format(a40)
write(*,*) 'file name is ',fil
open (unit=1,file=fil,access='sequential',status='unknown')
count = 0
100 continue
read(1,119,end=999)string
119 format(a)
write(*,*) 'string was : ',string
go to 100
999 continue
stop
end
More information about the Programming
mailing list