6

I'm writing a piece of code by which I plan to write a .txt file that's a 10x10001 matrix:

do i = 1, 10
    read(1,10) seed !Read a number from file 1
    write(2,20) seed !Write that number in file 2
    do k = 1, 10000 
        seed = mod((a*seed),m)
        R = seed/m
        write(2,20) R !I want all these numbers to be next to the seed, not on new lines
    end do
end do

But all the R's are written on new lines.

Is there a way to separate every number with a space instead of a new line, or should I implement this same code using C++ and pipelines?

nbro
  • 15,395
  • 32
  • 113
  • 196
Miguelgondu
  • 249
  • 1
  • 2
  • 12
  • 1
    performance wise you'd be better off to assemble the values in an array and write the whole thing at once. (or at least a whole 100001 element line at a time ). – agentp Sep 08 '14 at 12:06

1 Answers1

11

You can make write not add a newline at the end with the following code:

write(2, 20, advance="no") R
nbro
  • 15,395
  • 32
  • 113
  • 196
ArnonZ
  • 3,822
  • 4
  • 32
  • 42
  • Works like a charm. Thanks. Does something similar work for "read"?, I then need to read column by column in each row to perform some operation – Miguelgondu Sep 07 '14 at 20:33
  • nice. :) Reading space delimited values is a little trickier but still doable. check out [this](http://physicsforums.com/showthread.php?t=507870) post. – ArnonZ Sep 07 '14 at 20:41
  • 2
    I solved the problem using numpy in python: np.loadtxt imports the whole thing as a matrix, and then I can slice the parts I want. Thanks, though. – Miguelgondu Sep 08 '14 at 12:14