Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String copy

Status
Not open for further replies.

saracooper

Programmer
May 1, 2010
3
IT
Hello, probably this is a very basic question, but I know very little about Fortran, I m a C programmer but have to deal with some routines in Fortran.
How does one copy a string from one CHARACTER*8 var to another ? I tried by simple assignment -

CHARACTER*8 var1
CHARACTER*8 var2
var1 = 'test1'
var2 = var1

Here actually var1 gets populated from somewhere else, var2 gets var1 value, but I am not sure one of the 2, I think var1 looses some characters. It is like after the assignment, the memory gets messed up. Any help is really appreciated. Thank u
 
The problem with Fortran is that everything is space terminated

var1 = 'test1'

is the same as

var1 = 'test1 '

Assignment is exactly as you have it

var2 = var1

No strcpys etc. Try this
Code:
program xxx
character*8 var1, var2
var1 = 'test1'
var2 = var1
print *, 'var1=[', var1, ']'
print *, 'var2=[', var2, ']'
stop
end
 
The word "test1" is only 5 characters long, while it's declared as 8

If var2 has had a value before, it SOMETIMES happened to me (especially in F77) that characters behind stayed in the string. So if var2=was="12345678" it will be "test1678" afterwards.

You'd better make sure to clean up everthing that comes behind the 5th position before assigning it a new value.

To get a substring use var1(index1:index2)
So, var1(2:3) gives 'es'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top