You are viewing our Forum Archives. To view or take place in current topics click here.
[VB (6.0)] Creating new file
Posted:
[VB (6.0)] Creating new filePosted:
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Ok so I need to create a new file in program where the user enters a name and the corresponding score.
I have already created a private type called User, it consists of String * 10 followed by an Integer
I don't have VB at home so I can only produce what I think should work atm and then add it to my form at school.
Will this work correctly?
I have already created a private type called User, it consists of String * 10 followed by an Integer
Dim Index As Integer
Dim Counter As Integer
Counter = 1
Filename = App.Path & "\HighScores.txt"
txtFilename.Text = Filename
Open Filename For Random As #1
For Index = 1 To 10
NewUser.Name = InputBox("Please enter name " & Counter)
NewUser.Score = InputBox("Please enter score " & Counter)
Put #1, Index, NewUser
Counter = Counter + 1
Next Index
Close #1
MsgBox "New file created", _
vbExclamation + vbOKOnly, _
"Highscores Database"
I don't have VB at home so I can only produce what I think should work atm and then add it to my form at school.
Will this work correctly?
#2. Posted:
Status: Offline
Joined: Feb 06, 201014Year Member
Posts: 996
Reputation Power: 64
Status: Offline
Joined: Feb 06, 201014Year Member
Posts: 996
Reputation Power: 64
-Deano wrote Ok so I need to create a new file in program where the user enters a name and the corresponding score.
I have already created a private type called User, it consists of String * 10 followed by an Integer
Dim Index As Integer
Dim Counter As Integer
Counter = 1
Filename = App.Path & "\HighScores.txt"
txtFilename.Text = Filename
Open Filename For Random As #1
For Index = 1 To 10
NewUser.Name = InputBox("Please enter name " & Counter)
NewUser.Score = InputBox("Please enter score " & Counter)
Put #1, Index, NewUser
Counter = Counter + 1
Next Index
Close #1
MsgBox "New file created", _
vbExclamation + vbOKOnly, _
"Highscores Database"
I don't have VB at home so I can only produce what I think should work atm and then add it to my form at school.
Will this work correctly?
Not sure about VB,but I did this for C#:
string fileName = @"C:\Temp\Mahesh.txt";
try
{
// Check if file already exists. If yes, delete it.
if (File.Exists(fileName))
{
File.Delete(fileName);
}
// Create a new file
using (FileStream fs = File.Create(fileName))
{
// Add some text to file
Byte[] title = new UTF8Encoding(true).GetBytes("New Text File");
fs.Write(title, 0, title.Length);
byte[] author = new UTF8Encoding(true).GetBytes("uyyuyuyu");
fs.Write(author, 0, author.Length);
}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}
- 0useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Jul 08, 201212Year Member
Posts: 68
Reputation Power: 2
Status: Offline
Joined: Jul 08, 201212Year Member
Posts: 68
Reputation Power: 2
#4. Posted:
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
TTGXMODsX wrote-Deano wrote Ok so I need to create a new file in program where the user enters a name and the corresponding score.
I have already created a private type called User, it consists of String * 10 followed by an Integer
Dim Index As Integer
Dim Counter As Integer
Counter = 1
Filename = App.Path & "\HighScores.txt"
txtFilename.Text = Filename
Open Filename For Random As #1
For Index = 1 To 10
NewUser.Name = InputBox("Please enter name " & Counter)
NewUser.Score = InputBox("Please enter score " & Counter)
Put #1, Index, NewUser
Counter = Counter + 1
Next Index
Close #1
MsgBox "New file created", _
vbExclamation + vbOKOnly, _
"Highscores Database"
I don't have VB at home so I can only produce what I think should work atm and then add it to my form at school.
Will this work correctly?
Not sure about VB,but I did this for C#:
string fileName = @"C:\Temp\Mahesh.txt";
try
{
// Check if file already exists. If yes, delete it.
if (File.Exists(fileName))
{
File.Delete(fileName);
}
// Create a new file
using (FileStream fs = File.Create(fileName))
{
// Add some text to file
Byte[] title = new UTF8Encoding(true).GetBytes("New Text File");
fs.Write(title, 0, title.Length);
byte[] author = new UTF8Encoding(true).GetBytes("uyyuyuyu");
fs.Write(author, 0, author.Length);
}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}
I appreciate you doing that, however it must be in VB 6.0 because it's what we have to use for my A level coursework due to it being the most similar to the pseudo code we have to use in the exams.
AvP_Coding wrote VB6 is very different to the .Net languages.
@OP
PLEASE, use the resources microsoft have provided you with. MSDN is so useful you will never want to come on TTG again.
[ Register or Signin to view external links. ]
Like with the other guy, I wish I could use the resources however they are not to the same designs as I have for my coursework. What I've put is very similar to how we have to put it in my exam, therefore I can't use what resources are offered. Which is why I asked if my code was correct, hoping someone could verify it.
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.