Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / VB / VB.net - Bad File Mode

Author
Message
Michael P
20
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 9th Jan 2008 18:06 Edited at: 9th Jan 2008 18:07
I've been trying for ages now to work out why the following simple code wont work in Microsoft Visual Basic 2008 Express Edition. There is a command button object called BtnSave and 2 text boxes called TBMaxPlayers and TBServerName and I'm trying to write what text is in them to config.cfg when the command button is clicked. Currently with the code below I get the error: 'Bad file mode.' The only open mode that doesn't churn this error out is 'random' but when looking at the file in notepad it has lots of unreadable characters. I want the file to be readable in notepad.

Benjamin
23
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 9th Jan 2008 18:28 Edited at: 9th Jan 2008 18:56
I'm guessing it expects the file to already exist (hence 'append') else it'll fail. After looking on MSDN I see you need OpenMode.Output. See this page.

As for not being able to read the file in notepad, that's probably because it's storing the data as binary data rather than text. If you want to be able to read the data in notepad, you'll have to write strings.

Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
Michael P
20
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 9th Jan 2008 19:09 Edited at: 9th Jan 2008 19:16
The file mode command creates the file if it doesn't already exist automatically. That also irritated me because I spent ages looking for a 'make file' type command... Damn microsoft to hell!

I tried all the open modes. The modes that generate the error are:
Output - according to the help files this is used for writing to a file
Input
Append

The modes that worked are:
Random - which generated unreadable text

Binary - I tried this just now, and the name made me think it would generate lots of 1's and 0's but in fact when opened in notepad I can read the text

What are the other modes used for and why do they generate an error when used in the code on the original post?

[edit] Also, when using the open mode binary - how do I create a new line with the fileput command?
[edit2] Worked out the above question: FilePut(1, Chr(10))
Benjamin
23
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 9th Jan 2008 23:15
Quote: "What are the other modes used for and why do they generate an error when used in the code on the original post?"

http://msdn2.microsoft.com/en-us/library/w3a39hs6(VS.80).aspx

Have you read the page I linked you to yet? It shows an example of opening a file in the output mode. Try this:



Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
Michael P
20
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 9th Jan 2008 23:31
Yes, have a look:

This is the code I'm executing:


Here is a screenshot of what happens when compiled/run:
Benjamin
23
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 9th Jan 2008 23:46
Reading the MSDN page on FilePut I see that the only valid modes for this are random and binary.

I haven't actually used VB before so I can't say how writing text to files is done, although this looks like a good place to look.

Tempest (DBP/DBCe)
Multisync V1 (DBP/DBCe)
Michael P
20
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 10th Jan 2008 00:05
Thanks for your help All I can say is that VB is not my friend right now...
CattleRustler
Retired Moderator
22
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 10th Jan 2008 00:06
is this vb6 or .net
either way youre making it way more difficult than it needs to be

My DBP plugins page is now hosted [href]here[/href]
Michael P
20
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 10th Jan 2008 08:38
I'm not sure, but I think its .net - I downloaded it from the same place as dark gdk so it's downloaded from this link: http://www.microsoft.com/express/vb/Default.aspx
CattleRustler
Retired Moderator
22
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 10th Jan 2008 12:44
ok so its .NET, good...

in your button click event code, or better yet, in a separate routine called from your button click event, you can write to a file (note if using Append, it will create and append if the file doesnt exist)




thats just one way of many to write to a file. if you are writing text thats gonna be the quickest and easiest. that uses append but you could use output there as well which will delete and recreate the file specified, without asking.

My DBP plugins page is now hosted [href]here[/href]
Michael P
20
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 10th Jan 2008 13:17
ah thankyou
Tutelage Systems
18
Years of Service
User Offline
Joined: 12th Dec 2007
Location:
Posted: 12th Jan 2008 06:30 Edited at: 12th Jan 2008 06:30
Because it is .NET you could use the Streamwriter

Login to post a reply

Server time is: 2026-06-21 12:13:45
Your offset time is: 2026-06-21 12:13:45