Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] Saving To Text Files
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
good_fella




PostPosted: Fri Oct 31, 2003 6:23 pm   Post subject: [Tutorial] Saving To Text Files

I noticed there's not too many Visual Basic Tutorials, so I decided I'd try and write a few.

Here's one that is very useful on how to save text to a text file.

code:

Private Sub Command1_Click()

Open "C:\My Documents\TextSaver.txt" For Append As 1
Print #1, Text1.Text
Close 1

End Sub


In the above code, The 'Open "C:\My Documents\TextSaver.txt" can be changed to any drive or folder on your computer.

The 'TextSaver.txt' can be any file name you want with the '.txt' extension. If the file does not already exist on the computer, it will be created. If it does exist, it will be opened.

'Print #1, Text1.text' prints the text from Text1 (Can be any textbox) to the file.

Close 1 then closes the file.

The file can be opened and closed any number of times. It automatically saves the text.

Any questions let me know. I'm not to good at these tutorial things.

Feel free to copy the code and do what you want with it.
Maybe I'll write some more tutorials another time.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri Oct 31, 2003 6:50 pm   Post subject: (No subject)

hey, VB tutorials Surprised Awesome! +30Bits

I'm just going to add that "1" used in "For append as 1"

is file identifier. Such as you can use "2" instead. If you have 10 different files opened, you need to have 10 identifiers.

There's also something like fileNum.nextAvailable or other that returns a value for the next available integer to use as a file identifier. Useful if you're opening files in a loop Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
FDisk87




PostPosted: Fri Dec 05, 2003 5:04 pm   Post subject: (No subject)

Or you can use FreeFile to get the first available filenumber

intFileNum = FreeFile
Open "C:\Filename.txt" for Output as #intFileNum
etc
w00rm




PostPosted: Thu Jan 01, 2004 10:55 am   Post subject: (No subject)

but how would you open the text back up after closing the app. i mean getting the values back into the app. For instance say you entered in a value in Text1.Text and then it saved it to a .txt file. When you open the app the next time how would you load that text you typed back into text1.text
Random




PostPosted: Fri Nov 05, 2004 9:16 pm   Post subject: Reading From Files

Well... I know this is an old subject but i saw it and if it helps...
code:

Open "C:\File.txt" For Append As #1

Open "C:\File.txt" For Input As #1

Open "C:\File.txt" For Output As #1

There are 3 main ways of opening a file. Below I will describe them:

Append: What good_fella used. It opens a file to write to the end if it. If there is no file, one will be created so that data can be written to it.

Input: This tells VB that you will be reading from the file. If there is no file, you will get an error message.

Output: This tells VB that your program need to write to a file. If the file already exists, it will delete it and then create a new one under the same name. If no original file existed, one is created.

To read from a file, you use the input option.
code:

Dim info As String

Open App.Path & "\text.txt" For Input As #1
Do Until (EOF(1) = True)
Input #1, info
list1.additem info
Loop
Close #1

We make a string (info) where we will store data from the text file. We open the text file for input. the key line is:
code:

Input #1, info

This will take the line and store it into the variable info. Then we can use it to add it to a list box or what not. I put this line in a loop so that it will read a line, add it to a list box (list1) and then go to the loop. The loop will go until it is the end of the file. That is what eof() does. It will return true when the end of file is reached for #1.

Im not the greatest at explaining, but i hoped this helped anyone who wants to read from files. I added an attachment so that it might help you understand better.



reading files.zip
 Description:
A real example of reading files

Download
 Filename:  reading files.zip
 Filesize:  1.3 KB
 Downloaded:  561 Time(s)

mjwest10




PostPosted: Tue Mar 20, 2007 9:34 am   Post subject: Re: [Tutorial] Saving To Text Files

If you want to learn how to read text files in different formats and use them in your program. I think this might help you , A tutorial that explains how to read text files using Visual Basic 6. I think you will find it useful for what you are trying to do.
isaiahk9




PostPosted: Mon Nov 03, 2008 1:24 pm   Post subject: RE:[Tutorial] Saving To Text Files

@anybody : will any of this code work if I just want to add a new line to a file. For example, if I was making a picture album with a guest book, and I just wanted to add the entered name to the end of the file.
Would any of these methods work?
delparnel




PostPosted: Mon Nov 03, 2008 2:56 pm   Post subject: Re: [Tutorial] Saving To Text Files

You want to use "Open for Append" if you're looking to add a line to the end of an existing file.
Sponsor
Sponsor
Sponsor
sponsor
isaiahk9




PostPosted: Mon Nov 03, 2008 3:17 pm   Post subject: RE:[Tutorial] Saving To Text Files

That's it - adding a line to the end of an existing file.
So how would I do that?
delparnel




PostPosted: Mon Nov 03, 2008 3:19 pm   Post subject: Re: [Tutorial] Saving To Text Files

The same way that you would add a line to a new file, except instead of using "Open for Output" use "Open for Append", which goes to the end of the file.
isaiahk9




PostPosted: Mon Nov 03, 2008 5:24 pm   Post subject: RE:[Tutorial] Saving To Text Files

ummm . . . okay.
So, say I had a file named "GuestList.txt" in my C Drive. How would I add the TextBox1.Text as a line to that.
Sorry, but I'm a complete noob to VB.
Vertico




PostPosted: Mon Nov 03, 2008 7:04 pm   Post subject: Re: [Tutorial] Saving To Text Files

it seems most people on this forum don't like giving answers which is to be expected when the possibility of this, or any project, being a class assignment.


Your question can be answered pretty easily with a few Google searches. For example:
http://www.xtremevbtalk.com/showthread.php?t=148864

I didn't read through it all, but it seems to have some merit. If you still have some questions, I will be glad to assist.



(If you haven't used the following before, I suggest taking advantage)
http://msdn.microsoft.com/en-us/library/default.aspx
isaiahk9




PostPosted: Mon Nov 03, 2008 7:34 pm   Post subject: RE:[Tutorial] Saving To Text Files

And I don't receiving answers - i just wanted examples so that I could solve it myself.

Thanx for the tutorials.
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: