Writing in two different places unformatted files in Fortran - file

I am trying to write in two different places (the main program and a subroutine) unformatted files in my Fortran code. The problem is that when I do it, the results change and I suspect that it is because the memory assignment is overwriting the data that I am using to make the simulation in my CFD code. I ask: Is it possible that one can just use the unformatted file (to write) once in the code? I mean, I have to use the same file to save all my data and not with different files.
I copy and past the two parts of the code to show what I am want to describe:
In the main program, the loop is:
call numcar (isave,suffix)
longueur=index(nchamp,' ')-1
nfichier=nchamp(1:longueur)//suffix
longueur=index(nfichier,' ')-1
open(10,file=nfichier(1:longueur),form='unformatted')
write(10) real(uxn,4),real(uyn,4),real(wzn,4),real(ppo,4)
close(10)
! *****************************************
isave=isave+1
and in the subroutine, the loop is:
call numcar (isavediv,suffix1)
longueur1=index(ndiv,' ')-1
nfichier1=ndiv(1:longueur1)//suffix1
longueur1=index(nfichier1,' ')-1
open(20,file=nfichier1(1:longueur1),form='unformatted')
write(20) real(ppm,4)
close(20)
! *****************************************
isavediv=isavediv+1
All the variables all declared as IMPLICIT NONE in both main program and subroutine.

I solved my problem.
The problem was that I was using the channel number 20 and a colleague of mine told me that this channel is used by the computer or some devices to process data.
I changed it for channel number 10 and it worked good again.
Thank you for your comments.
Now it looks so:
open(10,file=nfichier1(1:longueur1),form='unformatted')
write(10) real(ppm,4)
close(10)

Related

using GDB with arguments

For a class assignment we needed to write a compiler. This includes an optimizer portion. In other words, we take in a file with some "code". An output file is generated. In the second step we take in the outputted code and remove any "dead" code and re-output to a second file. I have some problems with the optimizer portion and would like to use gdb. But I can't get gdb to operate properly with the input and output files arguments. The way we would normally run the optimizer is:
./optimize <tinyL.out> optimized.out
where tinyL.out is the file outputted in the first step and optimized.out is the file I want to output with the new optimized and compiled code.
I have searched Google for the solution and the tips I have found do not seem to work for my situation. Most people seem to want to only accept an input file and not output a separate file as I need to do.
Any help is appreciated (of course)
I'm not exactly sure what you're asking. But since I'm not yet able to comment everywhere, I write this answer with a guess and edit/delete if necessary.
When GDB is started and before you start the program you wish to debug, set the arguments you want to use with set args.
A reference to the documentation.
You just need to do the file redirection within gdb.
gdb ./optimize
(gdb) run < tinyL.out > optimized.out
https://stackoverflow.com/a/2388594/5657035

C - How do I make a function that finds the location of a file it has to use just by giving it the filename? (Windows)

I am having trouble with the function fopen(). I would always send the exact location of a file as an arguement to fopen(),
which would look something like this:
fopen("c:\\Users/Username/Desktop/Projects/program_name/version 1.0/data/important_data.txt", "r");
That works just fine on my computer, but what if I decide to transfer the program to another computer?
The location would completely change.
It would no longer be:
c:\\Users/Username/Desktop/Projects/program_name/version 1.0/data/important_data.txt
But it would rather be something like:
c:\\Users/OtherUsername/Desktop/program_name/version 1.0/data/important_data.txt
My question is, how do I make a portable function which can obtain the location of a file, if I only give the
name (including the type e.g. .txt) of the file to the function?
Keep in mind, I've been learning C for less than a year. There are still a lot of things which I must learn, and
things like this are of high importance.
this is operating system specific. on linux you can use the locate shell command and parse its output ( http://www.linfo.org/locate.html )
C: Run a System Command and Get Output?
How do I execute a Shell built-in command with a C function?
however this solution will only work on linux. i think yano's solution in the comments above is better ...

Can't write to file in Fotran90

I can't understand why my Fortran90 program does not write to file anymore. It used to work some hours ago. I haven't modified that part of the code, nor I have introduced some conditions by virtue of which it should not write to file . On the next line i have put a print command which writes on terminal the same values that should be written to file, and this works.
open(500, file='data.dat')
write(500,fmt='(E14.4,E14.4)') A,B !this SHOULD be printed on unit 500
write(*, fmt='(E14.4,E14.4)') A,B ! this is printed on terminal
I have tried to change unit, but nothing changes. I have tried to open a new file with a new name and writing onto it, also in other parts of the code, even at the beginning, just after variables declaration. This does not seem to work either. It is so frustrating, because everything worked properly previously. What could have happened?
Using gfortran 5.3 under Linux and the following program (that is your code copy-pasted and the minimal boilerplate).
program hop
implicit none
double precision :: A, B
A = 1
B = 2
open(500, file='data.dat')
write(500,fmt='(E14.4,E14.4)') A,B !this SHOULD be printed on unit 500
write(*, fmt='(E14.4,E14.4)') A,B ! this is printed on terminal
end program hop
outputs 0.1000E+01 0.2000E+01 in the terminal and the exact same content in the file data.dat.
Is the write instruction in a program that hangs? Not seeing output might be related to i/o buffering.
Is the behaviour changing if you add the line
flush(500)
after the write instruction?
I have to write to file in a cycle. Now I open the file in the cycle having added position='append' and I close it at the end of the cycle. Just like
program pro
implicit none
integer (kind=2) :: i, A, B
do i=1,10
A=i
b=i+1
open(500, file='file.dat', position='append')
write(500, *) A, B
close(500)
enddo
That seems to work now.

Rebound: fatal error while opening a file

I am using a software called Rebound which is a type of N-body integrator that generates simulations according to a certain code I wrote. the software uses built in functions of its own which are based on C.
part of the code I wrote is when I'm telling the program to output certain data into separate text files every set period of time.
char array [1000000];
if (reb_output_check(r, 49.581410)){
sprintf(array, "output%d.txt", i);
i++;
reb_output_orbits(r, array ); //output the orbital elements
}
the reb_output_check function checks whenever the time is a multiple of 49.581410 years in simulation r to execute the rest.
everything is working out fine until the 252nd output where the simulation stops and this appears:
Fatal error! Exiting now. Can not open file
I've repeated the simulation more than once and I'm getting the same thing at the same time every time. I don't know why it's giving me this.
any help regarding the matter is much appreciated.

How do I read from severals files in C?

I am working on a program which have to read from txt files.
I know that there's a function called fopen("myfile.txt","rt"), but what if I have 10 files? Do i need to call the function 10 times (a call for every file)?
Yes. But if you perform the same routines on each of those functions, abstract that behaviour into a function that accepts the name of a file. Now call that function 10 times, once with each file name.
void read_from_text_file(char const *filepath);
read_from_text_file("myfile.txt");
read_from_text_file("myfile2.txt");
...
This is a core concept in computer science. Buzzwords include "abstraction", "routine", "reusability", etc.
Yes. One function call per file is needed.

Resources