How to write without a newline in Fortran?

10,010

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

write(2, 20, advance="no") R
Share:
10,010
Miguelgondu
Author by

Miguelgondu

Updated on June 05, 2022

Comments

  • Miguelgondu
    Miguelgondu almost 2 years

    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?

  • Miguelgondu
    Miguelgondu over 9 years
    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
  • Arnon Zilca
    Arnon Zilca over 9 years
    nice. :) Reading space delimited values is a little trickier but still doable. check out this post.
  • Miguelgondu
    Miguelgondu over 9 years
    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.