Saving output to a file
Saving output to a file
This seems a very stupid question but I can't figure out how to save the screen output of a FreeBasic program to a file that I can print. I can print a program file by cutting and pasting to a word processor but that doesn't work for the output. I am new to FreeBasic, have both FBIde and FBEdit on Windows 10, and can't find a comprehensible answer to this simple question. I'd appreciate help. Thanks.
-
- Site Admin
- Posts: 6323
- Joined: Jul 05, 2005 17:32
- Location: Manchester, Lancs
Re: Saving output to a file
Hi fitzreyn, welcome to the forum.
FB does have the built-in ability to save the screen to a BMP file, using the bsave function:
FB does have the built-in ability to save the screen to a BMP file, using the bsave function:
Code: Select all
screenres 320, 200
print "Test Image"
bsave "test.bmp", 0
-
- Posts: 538
- Joined: Jul 15, 2005 4:13
Re: Saving output to a file
There should be a 'print screen' button on your keyboard,
PrnScr, pressing that and opening a piant program and using paste function will post the screen contents at time of pressing PrnScr to a new image in windows paint.
Its alot easier then adding specific code to your program,
But fb can deffinately Bsave any video screen to an image file.
PrnScr, pressing that and opening a piant program and using paste function will post the screen contents at time of pressing PrnScr to a new image in windows paint.
Its alot easier then adding specific code to your program,
But fb can deffinately Bsave any video screen to an image file.
Re: Saving output to a file
And Alt-PrintScreen to copy only the window having the focus.
-
- Posts: 1186
- Joined: May 08, 2006 21:58
- Location: Crewe, England
Re: Saving output to a file
If it's just plain text output, you could modify your program to print the results you need to a .txt file, which you could print either from Notepad or FBEdit, or paste the content into your word processor.
Code: Select all
open "answers.txt" for output as #94
print #94,"my answer"
close #94
Re: Saving output to a file
With the dos command > (using shell), you can create a file of about 8000 characters.
But chr(10) or !"\n" (a new line) , it doesn't like.
But chr(10) or !"\n" (a new line) , it doesn't like.
Code: Select all
#define Intrange(f,l) int(Rnd*((l+1)-(f))+(f))
dim as string answer
for z as long=1 to 8140
answer+=chr(intrange(48,57)) '0 to 9
Next
shell " echo " & answer & " > newfile.txt"
Shell "notepad newfile.txt"
kill "newfile.txt"
-
- Posts: 606
- Joined: Nov 28, 2012 1:27
- Location: CA, USA moving to WA, USA
- Contact:
Re: Saving output to a file
A little work, but you can write a function that will scan each character position on the screen and save each character to a file.
Add a filter for unwanted characters. This will give you an editable character file.
Tack it onto the end of your program, or - if you have a program control loop - make a 'hot key' to call it.
If you are new and learning, this is an excellent exercise to learn a few commands and see how to get info about the screen you are using.
David
Add a filter for unwanted characters. This will give you an editable character file.
Tack it onto the end of your program, or - if you have a program control loop - make a 'hot key' to call it.
If you are new and learning, this is an excellent exercise to learn a few commands and see how to get info about the screen you are using.
David
-
- Posts: 19
- Joined: Feb 22, 2017 18:34
Re: Saving output to a file
If your program is text-based and produces only text output, you can also "catch" it if you execute your program like this:
or like this:
This will tell your operating system to send effects of print commands not to the screen, but to the file output.txt (first command) or to clipboard (second one). After that you can print generated txt file or paste the clipboard contents into your text editor (Ctrl+V). This method has it's disadvantages, of course (for example you will lose formatting done with tabulators), but may be useful sometimes.
Code: Select all
my_program.exe >output.txt
Code: Select all
my_program_exe |clip
Re: Saving output to a file
Thanks to all that replied to my question, especially to jevans4949. Once I realized that the second line of his/her code meant to replace "print" by "print #94," every time I wanted to print something, it was clear what to do, since all I wanted was to print a text result, no graphics. This was a good clear answer for someone as ignorant as I am. I still wonder why there isn't an easier way for someone that doesn't understand command lines. fitzreyn
Re: Saving output to a file
That is not safe coding of imposing a file number (as #94) to bind to a disk file (with OPEN, PRINT #, ...).
The better is to request a file number to the system (just before the 'OPEN' code line), by using the keyword FREEFILE:
The better is to request a file number to the system (just before the 'OPEN' code line), by using the keyword FREEFILE:
Code: Select all
dim as integer f = freefile
open "answers.txt" for output as #f
print #f, "my answer"
close #f
-
- Posts: 1186
- Joined: May 08, 2006 21:58
- Location: Crewe, England
Re: Saving output to a file
I realised this; the OP was evidently inexperienced in FreeBASIC, so I decided not to over-complicate with FreeFile. I also figured he was unlikely to be using many files, so just picked a high number at random.fxm wrote:That is not safe coding of imposing a file number (as #94) to bind to a disk file (with OPEN, PRINT #, ...).
The better is to request a file number to the system (just before the 'OPEN' code line), by using the keyword FREEFILE:Code: Select all
dim as integer f = freefile open "answers.txt" for output as #f print #f, "my answer" close #f