Rebound: fatal error while opening a file - c

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.

Related

Anyone know how to solve the error of "collect2.exe: error: ld returned 1 exit status" when a program in C is running?

First of all I'm at the first year of computer science so I'm a beginner in programming yet, then forgive me for my lack of knowledge. Well, my problem basically is an error that is shown in the screen everytime when I try to run(by the way I'm using Sublime Text) a program in C. In this case was a simple code:
#include <stdio.h>
int main() {
//print the heading of the game
printf("*****************************************\n");
printf("* Wellcome to the our guessing game *\n");
printf("*****************************************\n");
int secretnumber = 42;
int guess;
printf("What is your guess?");
scanf("%d", &guess);
printf("Your guess was %d \n", guess);
}
OUTPUT:
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot open output file Olamundo.exe: Permission denied
collect2.exe: error: ld returned 1 exit status
[Finished in 0.3s]
You know, i tried some solutions like move the folder where the executable lies and place it in the root(in my case Mateus(C:)), give all the permissions to the folder, turn off the antivirus(in my case i just using the Windows defender), finish the task using the the Task manager but nothing worked. Please, help me i really don't know what else i can do.
Overall, your problem is that you're trying to run a program that is interactive from within Sublime; it doesn't support that. Sublime captures output your program sends to stdout and stderr and displays it in the output panel, but it does't connect the panel to stdin.
So, the scenario you're encountering works like this:
You run your program, which is interactive (in your case it prompts for input via scanf(), and it launches and prompts you for input
You try to enter input, but nothing happens because stdin isn't connected.
You try to run your program again (or modify it and build it again thinking you might have an issue).
The version you previously tried to run is still running in the background waiting for input you can't provide, and windows locks executable files while the program is running. So, when the linker (collect2) tries to link the executable during the build, it can't because the file is locked, hence the permission error.
You can clear the error by killing the program running in the background, which you can do via the Tools > Cancel Build if you do it before this error occurs; if you've already seen the error this likely won't work because this only cancels the most recent build, which would be the one where the error occurred.
The other thing you can do is use something external to kill it; the task manager on windows, kill from a terminal on Linux/OSX, etc. You'll need to do it this way if you're already seeing the error.
Note however that this doesn't solve your underlying problem because you're still trying to run an interactive program. In order to do that from within Sublime you need to create a custom sublime-build that allows for this. Generally you'd either have it spawn an external terminal and run the program in there, or use a package like Terminus if you want to keep it internal.
In order to set this up, you need to be familiar with the sequence of commands that are needed to compile, link and run a program in one command, which you can get by checking what the build system you're currently using is (assuming you didn't create it yourself).
This video on buffering and interactive builds in Sublime (disclaimer, I'm the author) has information and examples of how Terminus can be used for something like this if you'd like more information.

Writing in two different places unformatted files in Fortran

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)

"Threat detected" from AVG when trying to run my C program

I've been working on a project I have to do for school,
They basically asked us to separate every question to a file of its own,
so what I was trying to do was comment the rest of the files or exclude them off the project, and when I try to 'Start without debugging', I end up getting this message. I did this before and everything was fine:
the weird part is that if i remove the solution and make a new one and only make one .c file it works...
I was also trying to remove the "Ex2" folder and made it again, it didn't help.
When I choose move to vault it doesn't find the file.
Edit: I've run a scan and it didn't find any infections on the computer and now that i'm trying to run the program i don't see the threat anymore, I only get this message:
Please, i would really appreciate some help (again... )
I it find very interesting that your build failed. As a result, it is HIGHLY likely that no output was built.Yet your virus scanner flags a compiled file.
If your code didn't compile, where did this executable come from?
Perhaps you do have a real virus?
It seems the last error, that is partially cut off, is "Link: Fatal Error LNK1104. Cannot Open File". That suggests that the file is locked, write-protected, or otherwise blocked.
Is anything else holding open your executable, which prevents recompilation?
I wouldn't blame your virus scanner from flagging an attempt to write to a running .exe as suspicious.

Matlab system function with a C executable

I have written a Matlab GUI for my C program. I thought about using MEX, but there are too many C files and C program requires a DLL to run.
So, instead I have the Matlab System function calling the executable with inputs, something like [status results] = system('executable "input 1" "input 2"'), which runs well, but I want real time output. results is just a percent output of how complete the program is, and I want to use this output for a GUI progress bar in Matlab.
The output does get stored into results, but only after the program is complete. Thus, making the progress bar pointless.
Is it possible to get the executable to send outputs one at a time to Matlab, and then have Matlab update the progress bar, and return to the executable?
Edit: I'm looking for a solution in Windows.
I only see two options, and neither fits directly with your current implementation approach.
The first, is to just use sockets to communicate between the two. Here's a pure matlab socket implementation, but under the hood it's using C sockets. It's been 10 years since I've done C/Java socket comms, but I recall that at the time there were some issues.
http://www.mathworks.com/matlabcentral/fileexchange/21131-tcpip-socket-communications-in-matlab
Another option is to have your executable be accessible via a C DLL from matlab, and call the DLL directly from matlab (i.e. have matlab control your app). This is the way I've been doing most such interactions lately, and it works very well.
http://www.mathworks.com/help/techdoc/ref/loadlibrary.html
I found a solution. Credit goes to Richard Alcock at Matlab Central
Specifically, for my solution:
cmd = {'executable.exe', 'input 1', 'input 2'};
processBuilder = java.lang.ProcessBuilder(cmd);
cmdProcess = processBuilder.start();
% Set up a reader to read the output from the command prompt
reader =
java.io.BufferedReader(...
java.io.InputStreamReader(...
cmdProcess.getInputStream() ...
) ...
);
% Loop until there is some output
nextLine = char( reader.readLine );
while isempty(nextLine)
nextLine = char( reader.readLine );
end
% Then loop until there is no more output
while ~isempty(nextLine);
fprintf('Output: %s\n', nextLine);
nextLine = char( reader.readLine );
end
% Get the exit value of the process
exitValue = cmdProcess.exitValue
Note: this code does not hold up the executable. The executable must finish before this code finishes, otherwise this code crashes when it gets ahead of the executable.

Debugging in c for log file

I have written down a program in c and I am trying to create a log file of it.
The problem I am facing is that while printing the outputs of each line in the file I want to write some distinctive feature such as the time of execution of that line or even the line number in the code.
Is there any way I can get to know any of these two.
I don't mind if you suggest some other way to get a distinctive feature. All I want is that looking at the log file the user gets to know that a certain part of the code was getting executed.
Thanks
I am working on linux and thus using the GCC compiler....
I have made a header file and in it I am for testing purposes writing __LINE__ . What I want to do is that in a program when I include this function of header file the line number gets printed where the function is. But instead i get the line number of the header file printf statement.
What do I need to do to get the line number of the file .
This is just a test format given below :-
new.h
void print()
{
printf("Line number is %d",__LINE__);
}
actual file
#include "new.h"
int main()
{
print();
}
Then I want that the line number that should be printed is that of actual file and not new.h which happens now....
Most C compilers provide some macros to identify each line, function, etc. With GCC, for example, you can use __LINE__, __FUNCTION__, and so on. Check your compiler documentation for details. To get a timestamp, you'll need to let us know what system you're working on.
If you want the actual date and time the function was executed, try asctime(). There is a good reference on how it's done here.
This will output something akin to Sat May 20 17:36:17 2000. If you want the time in seconds since the program started, have a variable such as int startTime = time() which holds the program start time in seconds from the Unix Epoch. Then, simply print startTime - time() to get the number of seconds since program start.
In GCC you can get line number as "__LINE__". Filename - "__FILE__".
If you want calculate execution time then just remember time on start, get time on end and substract them.
The line number can be obtained by the preprocessor macro __LINE__. The file is __FILE__. As for time, use the relevant OS library.
Or, use a logging library that support these.
Use __FILE__ and __LINE__ to get the current file and line number.
Edit: based on your edited question. Here's a simple way to do it to start.
new.h
#define PRINT() print(__LINE__)
void print(int line)
{
printf("Line number is %d",line);
}
actual file
#include "new.h"
int main()
{
PRINT();
}

Resources