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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing whole strings including whitespace?

Status
Not open for further replies.

Samohtvii

Programmer
Aug 2, 2012
21
AU
Can someone tell me how to write whole lines to text files. Here is what I have tried but got just the first word 'do'.
is has opened unit 7 as append for an existing file
CHARACTER*50 newQuestion
read *, newQuestion
write(7, '(A)') newQuestion

say i type "do you want to?" it only adds the 'do' to the file but i want the whole line

Thanks
 
Your read statement is only reading one word. Try
Code:
read '(A)', newQuestion
 
hmm that seems to work kind of. IT reads the line into the txt file but the last word is always added to the next line.

So if the newQuestion is: "How are you?"
it will be added

How are
you?

thoughts?

Thanks for the help
 
Try to make the fieldsize that you read / write equal to the number of characters iun your string.

read (*,'(a50)') newQuestion
write (*,'(a50)') newQuestion

This would make sure that the system reads and writes all characters available in the string newQuestion.

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
hmm still the same problem :S
Changed the read and writes like you said but exactly the same.

Maybe the full code will help a bit more

CHARACTER*20 new Animal
CHARACTER*50 newQuestion
read *, newAnimal
read '(A100)', newQuestion

OPEN(7, fileName, ACCESS='APPEND')
WRITE(7, '(A100)') newQuestion
WRITE(7, '(A20)') newQuestion

write(7, '(A100)') newQuestion
write(7, '(A20)') newAnswer

strange because it is always the last word no matter how long.
how are
you

hello my name is thomas and i like
facebook

tek
tips

all the same formatting
 
How about

Code:
program main
  implicit none
  character*50 what
  read (*,'(a50)'), what
  write (*,'(a50)'), what
end program

Gordon Shumway
 
Sorry for the late response, but I am on vacation (currently on board a ship just a little north of Norway) so my time runs different to ther users :)

The thing you are having is really a little weird. We would have to check if your system adds a carriage control character while reading your variable or if it is added in output.

Do you have a debugger available? If yes, run your prog in debugging mode and check the contents of the variable NewQuestion or NewAnimal after reading. You may have to switch to hexadecimal or better octal display if available.

If you do not have a debugger available, check for an intrinsic fortran routine that allows you to change the format of a variable, I cannot look up the name of it right now. You may use it to print out your variable in octal format.

Once you have the octal code of the memory contents you may check letter for letter what it is that your prog reads to memory.

Well, this is a little complicated, maybe someone has a more simple idea.

I'll be back on line on Sunday at latest.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top