Open "filename" for output as #1

New to FreeBASIC? Post your questions here.
Post Reply
WAGNERLIP
Posts: 8
Joined: May 27, 2013 6:30

Open "filename" for output as #1

Post by WAGNERLIP »

Hi friends, this is my first post in this forum.

I've being programming in compiled basic from decades, and was using QuickBasic + BasicCompiler (BC.EXE) since then.
Then, by complete accident I found FreeBasic, and wow, no words. I am still smiling for more than 48 hours already.
I converted some of my old programs to freebasic, they were not able to run in fullscreen under Windows7, and gave me several headaches. FreeBasic solve it, and more, gave me 256 colors and larger screen modes. Can't thank you enough.

Well, due the situation, files already wrote in QuickBasic, etc, I needed to setup [#lang "qb"], so it will run without much changes and updated. The -lang "qb" option doesn't work in command line, please read about it at the end of this post.

I am fighting a nasty little problem that doesn't allow me to open and write to a hiden file in Win7.
The application has a configuration file, that sits on C:\somefile.ini with attribute as +H, so the user will not mess up with the configuration.

The new "running like crazy speed and color" .exe file is working fine (up to this point), after several little details, that I needed to learn this weekend, as for example, [FIELD #1, 1 as A$] doesn't work in FreeBasic, that's okay, I can survive with, BUT, it can't open and write to the hidden file.

If I change the file attribute in anyway (explorer, cmd screen, etc), it works, but while the file still with attribute of +H, it can't be touched by the FreeBasic.

Strange enough, FreeBasic is totally able to read the file, as [Open "filename" for input as #1], but open as output, no way.

Other strange thing, I can't run [FBC -lang qb ...] option, it says [error 73: Invalid command-line option, "-lang" ], that's okay, I found out I can include it in the first line of the basic proram as [#lang "qb"] and it works, but still, something is wrong with the option in the command line.

The FreeBasic version I donwloaded and installed is the FreeBASIC-0.24.0-win32

Any hint about the hidden file?
Of course I went around the problem, with

SHELL 'ATTRIB -H C:\FILENAME.INI"
... OPEN AND WRITE TO FILE
SHELL 'ATTRIB +H C:\FILENAME.INI"

But it seems this is not the way...

Thanks
Wagner.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Open "filename" for output as #1

Post by fxm »

Wagner, welcome to the forum.
WAGNERLIP wrote:Other strange thing, I can't run [FBC -lang qb ...] option, it says [error 73: Invalid command-line option, "-lang" ], that's okay, I found out I can include it in the first line of the basic proram as [#lang "qb"] and it works, but still, something is wrong with the option in the command line.
The option to add to the compile line is only "-lang qb" with a space before (and after if necessary).
sir_mud
Posts: 1401
Joined: Jul 29, 2006 3:00
Location: US
Contact:

Re: Open "filename" for output as #1

Post by sir_mud »

WAGNERLIP wrote:The application has a configuration file, that sits on C:\somefile.ini with attribute as +H, so the user will not mess up with the configuration.
The root directory is a protected directory and you would need admin permissions to write there. I would suggest storing your configuration files in %APPDATA%/yourapp, the directory is hidden by default.
WAGNERLIP
Posts: 8
Joined: May 27, 2013 6:30

Re: Open "filename" for output as #1

Post by WAGNERLIP »

sir_mud wrote: The root directory is a protected directory and you would need admin permissions to write there. I would suggest storing your configuration files in %APPDATA%/yourapp, the directory is hidden by default.
Thank you for the hint.

Even if in a subdirectory, it doesn't work.

Code: Select all

#lang "fb"
#INCLUDE "DIR.BI"

DIM SHARED A AS STRING
DIM SHARED FNAME AS STRING
FNAME = "C:\TDIR\TESTFILE.INI" 

SUB READFILE
OPEN FNAME FOR INPUT AS #1
   DO UNTIL EOF(1) 
      LINE INPUT #1, A
      PRINT A 
   LOOP 
   CLOSE #1
END SUB


if len(dir("C:\TDIR", fbDirectory)) = 0 then MKDIR "C:\TDIR"

OPEN FNAME FOR OUTPUT AS #1
   IF ERR>0 THEN 
     PRINT " >>> ERROR CREATING FILE" 
   ELSE
      PRINT #1, "FILE FREATED" 
      CLOSE #1
   END IF
READFILE

OPEN FNAME FOR OUTPUT AS #1
   IF ERR>0 THEN 
     PRINT " >>> ERROR OPENING -H FILE FOR OUTPUT" 
   ELSE
     PRINT #1, "PRINTING LINE 2" 
     CLOSE #1
   END IF
READFILE

SHELL "ATTRIB +H C:\TDIR\TESTFILE.INI"

OPEN FNAME FOR OUTPUT AS #1
   IF ERR>0 THEN 
     PRINT " >>> ERROR OPENING +H FILE FOR OUTPUT" 
   ELSE
     PRINT #1, "PRINTING LINE 3" 
     CLOSE #1
   END IF
READFILE

SHELL "ATTRIB -H C:\TDIR\TESTFILE.INI"

OPEN FNAME FOR OUTPUT AS #1
   IF ERR>0 THEN 
     PRINT " >>> ERROR OPENING -H FILE FOR OUTPUT" 
   ELSE
     PRINT #1, "PRINTING LINE 4" 
     CLOSE #1
   END IF
READFILE 

END
text results:

FILE CREATED
PRINTING LINE 2
>>> ERROR OPENING +H FILE FOR OUTPUT
PRINTING LINE 2
PRINTING LINE 4
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: Open "filename" for output as #1

Post by petan »

Some tips ;)
- first line in code- unneeded - "fb" is default choice
- use 'freefile' for file handle, not constants
- wrong using of 'close' - in block - bad way

Code: Select all

'opening file
'immediate read/write actions
'closing file
- missing 'declare' if coding in non-spaghetti style
- use '()' with sub's name - if empty - speedy difference for what is subroutine and what variable only
- don't use VERSALES for writing code, very bad for eye's reading. Write it as a book.

In first what I see, try only simple code for changing fileattribs of some real text file via shell.
After success add to your snippet lines for read/write data to existing file.That's all.

Pete
WAGNERLIP
Posts: 8
Joined: May 27, 2013 6:30

Re: Open "filename" for output as #1

Post by WAGNERLIP »

petan wrote:Some tips ;)
- first line in code- unneeded - "fb" is default choice
- use 'freefile' for file handle, not constants
- wrong using of 'close' - in block - bad way

Code: Select all

'opening file
'immediate read/write actions
'closing file
- missing 'declare' if coding in non-spaghetti style
- use '()' with sub's name - if empty - speedy difference for what is subroutine and what variable only
- don't use VERSALES for writing code, very bad for eye's reading. Write it as a book.

In first what I see, try only simple code for changing fileattribs of some real text file via shell.
After success add to your snippet lines for read/write data to existing file.That's all.

Pete
Pete, that is not an actual code, I wrote that just to show that the [OPEN as output] fails if the file was flagged as hidden. I thank you for your recommendations.
Theunis Jansen
Posts: 248
Joined: Jul 01, 2010 9:35

Re: Open "filename" for output as #1

Post by Theunis Jansen »

Right you are using # lang "QB" - you can also use #Lang "fblite" which gives a few more goodies such as DJPeter's sound with still standard QB language.
1. Your SHELL routine is the only easy way to change the attribute from within your program.
2. Something to consider. Why not use an encrypted file ? For instance parse each line letter by letter(/digit) and add say 40 or 80 to the ascii code; The total should not exceed 255, then just give it a standard *.DAT or any name.
There used to be a few of these progs in QB and perhaps someone like Fxm or Dodicat has already written or changed one for FB. (Sorry you two but you have been this old man's pillars)

Perhaps a Forum search may give you one.

If you are worried that someone will mess around with your +H *.INI file it is already quite easy for them to do so by merely changing a folder's properties to allow read and write etc. i.e just unselect read only or hidden or both. I personally keep doing this just for the heck of it and to see if someone is trying to hide something from me.
sir_mud
Posts: 1401
Joined: Jul 29, 2006 3:00
Location: US
Contact:

Re: Open "filename" for output as #1

Post by sir_mud »

Seriously though, your config file doesn't need to be made hidden, just put it in a standard location like APPDATA. Normal users will never find it there. If there is sensitive data stored in the config file then you should encrypt it either using the winapi functions or a trusted 3rd party library. I will run a test to see exactly what's happening when a hidden file is being written to later so i can see if it is a bug and how to fix it.
WAGNERLIP
Posts: 8
Joined: May 27, 2013 6:30

Re: Open "filename" for output as #1

Post by WAGNERLIP »

sir_mud wrote:Seriously though, your config file doesn't need to be made hidden, just put it in a standard location like APPDATA. Normal users will never find it there. If there is sensitive data stored in the config file then you should encrypt it either using the winapi functions or a trusted 3rd party library. I will run a test to see exactly what's happening when a hidden file is being written to later so i can see if it is a bug and how to fix it.
Thank you for all suggestions.

May be I am exaggerating about the hidden file, may be I should keep user responsible for what he does in the machine, by the way, this application is not to be used by children, and adults need to know better, before delete some "strange file" they find in their harddisk.

One issue though, remains that the application *may* run in a pure DOS bootable machine. Nowadays a very simple and small PC can run wonders with QB or FB in industrial applications. A DOS 6.22 platform runs very fast in a minimalist machine, no blue screens, no longer boot time, no complications, small media requirements, and best of all, pure and simple fully dedicated I/O ports.

But well, back to the main issue; FB can't "open xx for output" hidden files.
petan
Posts: 683
Joined: Feb 16, 2010 15:34
Location: Europe
Contact:

Re: Open "filename" for output as #1

Post by petan »

Short job for un/Hide attrib of file "txt.cfg" in actual location. Wokrs for me.

Code: Select all

Screen 14

Dim As String jobIs1,jobIs2
jobIs1="attrib +h txt.cfg"
jobIs2="attrib -h txt.cfg"
Shell jobIs1
? "hidden > see on file icon in your Commander .."
Sleep

Shell jobIs2
? "un-hidden > see on file icon in your Commander .."
Sleep
End
 
As I have no needs to write on +h files I cannot discuss this , and FB capability to write on hidden files, but now you have way howto make such job as shown in example.One and only what you need is access and rights to hidden file.
sir_mud
Posts: 1401
Joined: Jul 29, 2006 3:00
Location: US
Contact:

Re: Open "filename" for output as #1

Post by sir_mud »

try changing your open call from

Code: Select all

open file for output as #whatever
to

Code: Select all

open file for binary as #whatever
Based on how the runtime library opens files this should work as it does in other languages. Here's a StackOverflow that explains the issue and another solution that uses the winapi although it's in python: http://stackoverflow.com/questions/1321 ... e-in-w-mod
Post Reply