Writing a simple text file

Post Reply
tcoulon
Posts: 3
Joined: Tuesday 13th February 2018 9:05am

Writing a simple text file

Post by tcoulon » Wednesday 14th February 2018 10:08am

Hello. I'm totaly new to Gambas, and although I did some programming in RealBasic (which I am trying to port to Gambas) I can't say I'm a good programmer.

I've tried to understand something rather basic, but I can't manage it in Gambas: writing a simple text file.

I have the following code:

(...)
mainfilename As String
hFile As File

Public Sub Main()
(...)
hFile = <filename> For Write Create
Write #hFile, <string to write>
Close #hFile

I get "Access forbidden". All I can find about this is "The requested access to the file is not allowed, or search permission is denied for one of the directories in the path prefix of pathname, or the file does not exist yet and write access to the parent directory is not allowed."

As access rights to the program's folder are OK (and I understand the file would be created there). So I guess I can't simply use a string variable as a file name?

Thierry

User avatar
jornmo
Site Admin
Posts: 171
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: Writing a simple text file

Post by jornmo » Wednesday 14th February 2018 11:29am

Hi and welcome to the forum!

You need to open the file stream first.
Check out the example on the bottom of this page:
http://gambaswiki.org/wiki/lang/open

User avatar
cogier
Site Admin
Posts: 187
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Writing a simple text file

Post by cogier » Thursday 15th February 2018 10:30am

Hi and welcome.

If I read your request correctly there is a very easy way to do this in Gambas. The reason you are getting "Access forbidden" is because you are trying to write your file to the root directory which you are not allowed to do. Your 'Home' directory is the place to save your file and Gambas can point to that with 'User.Home'. Have a look at the code below, if you run it you will find that a new file "MyFile.txt" has been created in your Home folder. Also notice the '&/' which will sort out the necessary '/' needed in the path. (It can also remvove too many '/' if required.)
Public Sub Main()
Dim sTextForFile As String = "Hello world\nThis will end up in your file"
Dim sFileName As String = User.Home &/ "MyFile.txt"

File.Save(sFileName, sTextForFile)

End
If you post the original RealBasic code we may be able to help you further.

tcoulon
Posts: 3
Joined: Tuesday 13th February 2018 9:05am

Re: Writing a simple text file

Post by tcoulon » Thursday 15th February 2018 8:33pm

jornmo wrote:
Wednesday 14th February 2018 11:29am
Hi and welcome to the forum!

You need to open the file stream first.
Check out the example on the bottom of this page:
http://gambaswiki.org/wiki/lang/open
Thanks. I had read this but it did not help. What I had not understand is that I was trying to write to the root directory.

tcoulon
Posts: 3
Joined: Tuesday 13th February 2018 9:05am

Re: Writing a simple text file

Post by tcoulon » Thursday 15th February 2018 8:41pm

cogier wrote:
Thursday 15th February 2018 10:30am
Hi and welcome.

If I read your request correctly there is a very easy way to do this in Gambas. The reason you are getting "Access forbidden" is because you are trying to write your file to the root directory which you are not allowed to do. Your 'Home' directory is the place to save your file and Gambas can point to that with 'User.Home'. Have a look at the code below, if you run it you will find that a new file "MyFile.txt" has been created in your Home folder. Also notice the '&/' which will sort out the necessary '/' needed in the path. (It can also remvove too many '/' if required.)

(...)

If you post the original RealBasic code we may be able to help you further.
Thanks. Why does Gambas default to the root directory? I never thought of that. But OK, now I managed to write to a subdirectory of home, so I can put my files somwhere and keep home tidy :)

>If you post the original RealBasic code we may be able to help you further.

I'll probably *have* to do that while my work progresses. I'm at the start and I try to get the basic stuff to run (which should not be too difficult I hope). WHen I get to the more complicated things I'll probably be back :)

User avatar
cogier
Site Admin
Posts: 187
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Writing a simple text file

Post by cogier » Thursday 15th February 2018 9:38pm

Why does Gambas default to the root directory?
If you were running a Distro like Puppy that gives you access to all the directories without the need to use 'sudo' or root passwords Gambas will happily put your new file in the 'root' directory. This is fine but allows you to really screw things up very easily if you don't know what you are doing, or make a genuine mistake.

I suspect you are using Ubuntu, Mint, Fedora, SUSE or similar that protects the 'root' files from accidental mishaps by requiring the need to use 'sudo' or a root password. This can be done in Gambas but to protect your system requires a little more work.

I see you are not using a GUI program, that is where Gambas has some real strengths. Have a look at the Gambas Farm. You will find example programs that may help you, some written by me and hopefully some by you in the near future.

Thanks for joining the site and we look forward to hearing from you soon. :D
gambas_open.png
gambas_open.png (2.24 MiB) Viewed 557 times

Post Reply