Python Eclipse output to an external text file - file

I have a long running code which outputs stuff every minute. I will be running it all night and will check results in the morning.
Is there a way to write all the Console stuff to an external text file. I do not want to modify the code, but just looking to direct all Console output to an external file. I am working in Eclipse. I tried the Run -> Run Configurations... -> Your Application -> Common tab -> File idea but the output is horrible - no line breaks.
Is there a way to get the exact same output as the Console into a text file - all formatted nicely?
Many thanks in advance.
RS

you can add a text file. If you write your code to a file which is in a specific directory or if you know the name of it, your program can recognize this. For example, if your variable is words, you can write words = open("what your file is called") then at the end of you code end by writing to file: file.write(string)

Related

Results in Pyhton terminal printed (i.e., using the print function) to a .txt file (ideally created with the open function)

Here's my situation: after running a Python file in VS Code I get a bunch of results in the terminal (in my case, several integers). Now, I want to export what is displayed on the terminal to a txt. file.
I have tried this:
import sys
f = open("out.text", 'w')
sys.stdout = f
print ("out.text", file=f)
f.close()
Basically, I am looking for something like this:
with open('out.txt', 'w') as f:
print('The data I see in the terminal window', file=f)
Now, how do i get this object: 'The data I see in the terminal window'?
P.S. I am very new to programming.
You can use the write method to write to a file:
with open("out.text", "w") as f:
f.write("out.text\n")
f.write("Something else\n")
You need the \n to end the line and print the "Something else" to the next line.
You can use
with open("out.text", "a") as f:
to append the following write statements to the contents of the file.
To do both, i.e., print to the terminal and write to a file, you would need two commands, one for each:
with open("out.text", "w") as f:
print("out.text")
f.write("out.text\n")
print("Something else")
f.write("Something else\n")
Alternatively, you could redirect the terminal output when calling your script
python script.py > out.text
But this will not print anything on the terminal anymore either, but redirect everything to the file.
There is at least one other question on here that deals with this.
Have a look here:
How to redirect 'print' output to a file?
And additionally search a bit more.
There are several solutions to your problem, but having it both ways is gonna be tricky.

Open website with C and search through source code

I made a program in C that is supposed to try a bunch of numbers on a url, like this:
example.com/cbc001 ...com/cbc002
The program runs throught from 000 to 999 and is supposed to add those numbers after the 3 letters in the url. I managed to get the program to run through all numbers, 000-999.
Now my problem is I have no idea how to open a website in C, and I don't really want to open it in a browser, what I just need is the program to try all those urls ".com/abc990" and then I want to check the source file of the webpage to search for a certain word.
How could I approach this?

fprintf() present in a C .exe file called from MATLAB doesn't work

I'm calling a C executable compiled using Cygwin in MATLAB, using the unix() function. This works fine, and I can see the desired output on the MATLAB command window. However, there is an fprintf() inside the executable that is supposed to create and write to a text file which does not run - no such file is created. The text file is created just fine when I run the executable directly through Cygwin.
I was wondering if I need to grant permissions to the MATLAB file/executable to enable this? How could I go about this?
What path are you using to create the file? It might have been created -- just not where you expected it.
If it's a relative path, you could use getcwd(2) inside your C program to get and print the working directory (or e.g. getpid(2) to get the PID and then do ls -d /proc/<pid>/cwd, which will work on Linux at least). Once you have the working directory, check if the file is somewhere in there.
If it looks like the file really isn't being created, my next step would be to add some error checking to functions and print messages for errors to try to figure out what's going on. strerror(3) and perror(3) might come in handy.

How do I run a C program several times and record the outputs?

so basically I have a C program which does a lot of computation based on an input .txt file and outputs a value. I want to run it 100 times and then work out the average, obviously this would be tedious to do individually.
So I've tried to research a bit about scripting etc and I've found things like this:
https://answers.yahoo.com/question/?qid=20091206100348AAaJPP8
Am I supposed to just do this in my command prompt? (I'm on Windows btw)
Thanks for any help :)
You're on Windows, so you can use a DOS batch script (.bat) to run your program N times using a loop (or N separate commands if that's easier for you). Use the >> symbol at the end of the command to append the output to a file. See http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true for more info on this, and search google for dos bat file for help on getting started with writing batch scripts.
Try this:
Have the program append the data into the text or csv file concerned and then write another program where you can run the program for a defined number of times. Use the function system(). It accepts a string as argument and executes it in the CUI.
Hope that helps.

working with files on "start without debugging"

I'm programming in C, and
I have the following problem:
I use fopen and try to read from a csv file, that is currently storred in the folder of the exe file of the program.
the program works fine in debug mode and release mode, but when I try to run the program in "start without debugging" on visual studio 2008 express edition, the program stops working and windows is showing a message: "*.exe has stopped working. a program caused the program to stop working correctly. windows will close the program and notify you if a solution is available".
I've tried running the program on several computers, and it's the same.
another information I can give you is that if I enter the full path of the file (C:....file.csv) - then is works just fine, without any problem.
I know I didn't write any code, but I hope someone will have an idea why this can happend.
thanks is advance.
Your program is not finding the csv file, fopen() fails and return a null pointer, you try to use it without checking and your program crashes (just my guess).
Firstly, you must make a check to see if fopen() could indeed open your file:
FILE* f = fopen("file.csv", "r");
if(f == NULL) {
/* print some meaningful error */
} else {
/* use the file */
}
Secondly, you may solve the problem by executing your program from the same folder the file is present. I am not a Windows guy, but if you create a link to the ".exe", in its properties may have some configuration called "Working Directory" or something like that, that you may set to the path on where the file can be found.
Every process has a working directory, that is usually the directory from where it was started, though it may be inherited from the parent process and it may be changed programmatically. If you do not specify the full path when loading a file, the process will search for the file in its current working directory.

Resources