Basic4ppc - Windows Mobile Development  

Go Back   Basic4ppc - Windows Mobile Development > Main Category > Code Samples & Tips > Tutorials
Home Register FAQ Members List Search Today's Posts Mark Forums Read

Tutorials Basic4ppc tutorials


Text Files


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-05-2007, 09:23 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 2,964
Default Text Files

Many applications use text files to store textual data, store user settings or many other usages.
It is pretty simple to work with text files, but still there are some points that you should be aware of.

The default encoding for text files is UTF8. This allows you to read or write any Unicode character.
You could choose to use ASCII encoding instead. ASCII encoding supports only the lower values of the ASCII table (0-127).
Note that UTF8 encoded files will start with a special Unicode marking.
Most applications recognize this marking and do not show it.
However, some older applications will show something like ";fe" at the beginning of the text. In such cases use ASCII encoding instead.
If you write a DOS batch file (on the desktop) then you should use ASCII encoding for that reason.

As an example we will create a small application with some user data. The user data will be loaded when the application starts (Sub App_Start) and saved when the application ends (Sub Form1_Close).
The source code file and the settings file are attached to the post.



Saving code:
Code:
Sub SaveINI
  ErrorLabel(errSaveINI)
  FileOpen( c1,"TextFiles.ini",cWrite)
  FileWrite(c1,txtFirstName.Text)
  FileWrite(c1,txtLastName.Text)
  FileWrite(c1,chkValidAccount.Checked) 'writing a boolean value.
  For i = 0To lstData.Count-1
    FileWrite(c1,lstData.Item(i))
  Next
  FileClose(c1)
  Return'If there were no "Return" here, the error message would have shown. 
  errSaveINI:
  Msgbox("Error writing INI file.","",cMsgboxOK,cMsgboxHand)
  FileClose(c1)
End Sub
File operations could fail, if for example the file is used by another application.
We are using ErrorLabel to handle unexpected errors and show a message to the user.

Now for the real part...
First we open the file with FileOpen.
The first parameter is the name that we give to this connection.
The second parameter is the path and file name.
In this case the file is located in the same folder of the source code (or compiled executable).
The third parameter could be cRead, cWrite or cRandom.
Text files should be opened for reading (cRead) or writing (cWrite), only binary files should use cRandom.
When using cWrite or cRandom, FileOpen will create a new file if it doesn't exist.
Text files are usually written one line after another and reading is done in a similar way.
FileWrite writes a string to the file, each string in a new line.
FileWrite receives two parameters, the connection name and the value to write.
Using FileWrite we store the form's data to the file.
As we've done writing we now close the connection with FileClose.
Only one connection could be made to a file, so it is important to close the connection when it is no longer required.

Now for the reading...
Code:
Sub LoadINI
  ErrorLabel(errLoadINI)
  If Not(FileExist("TextFiles.ini")) Then Return
  FileOpen(c2,"TextFiles.ini",cRead)
  txtFirstName.Text = FileRead(c2)
  txtLastName.Text = FileRead(c2)
  chkValidAccount.Checked = FileRead(c2)
  s = FileRead(c2)
  DoUntil s = EOF 'Read the ListBox items.
   lstData.Add(s)
   s = FileRead(c2)
  Loop
  FileClose(c2)
  Return
  errLoadINI:
  Msgbox("Error reading INI file.","",cMsgboxOK,cMsgboxHand)
  FileClose(c2)
End Sub
Again we handle unexpected errors with ErrorLabel.
First we check if the file exists. If it doesn't exist we exit this sub using Return.
A new reading connection is created using FileOpen and cRead flag.
We could have used 'c1' instead of 'c2' as the connection name (c2 is used to prevent confusion between the two parts).
The settings file is built of several known fields and an unknown number of fields as the ListBox items.
FileRead reads a single line from the connection.
Each time the next line will be read.
In the same order we previously saved the data, we now read the first three known lines.
The remaining data is now fetched and a new item is added to the ListBox for each line.
Each line is compared to the EOF constant which symbols the End Of File.
When the value equals to EOF we know that there are no more items left.
Another approach would have been to write the number of items before the items.

FileReadToEnd reads the entire remaining data (unlike FileRead which only reads the next single line).

Handling binary files could be done with FileGet and FilePut and with the BinaryFile library.
Attached Files
File Type: zip TextFiles.zip (969 Bytes, 372 views)
Reply With Quote
  #2 (permalink)  
Old 01-03-2008, 09:49 PM
Newbie
 
Join Date: Dec 2007
Posts: 4
Send a message via Yahoo to mick
Question Saving the second file???

Hello, Thats a very handy piece of code. Thanks so much for posting it. I was wondering however if anyone knows what to do with saving another file without
overwriting the first one. In other words, how do you check to see if a file exists and modify the file name if it does. Like test.txt exists so it saves as test2.txt. ect. Thanks, Mick
Reply With Quote
  #3 (permalink)  
Old 01-03-2008, 10:18 PM
Senior Member
 
Join Date: Apr 2007
Location: Copenhagen
Posts: 130
Default

Dear Mick,

you can use the "FileExist(FileName)" (see the helpfile)

e.g. in your example

FileName="test"
suffix=".txt"

If FileExist(FileName & Suffix) = true Then FileName=FileName & "2"

... and then save the text as FileName & Suffix


all the best / Björn

Last edited by BjornF : 01-03-2008 at 10:22 PM.
Reply With Quote
  #4 (permalink)  
Old 01-06-2008, 02:59 AM
Newbie
 
Join Date: Dec 2007
Posts: 4
Send a message via Yahoo to mick
Default Worked Like a Charm

GREAT! Thanks so much. That worked perfectly. I had a little trouble because
I left the brackets around (FileName & Suffix), once I removed them like
If FileExist FileName & Suffix = true Then FileName=FileName & "2"
Then it worked great. I really appreciate the info. Thanks again, Mick
Reply With Quote
  #5 (permalink)  
Old 01-06-2008, 05:53 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 2,964
Default

The brackets should be there.
Also note that FileName is a keyword so don't use it as a variable.
Reply With Quote
  #6 (permalink)  
Old 01-19-2008, 04:26 AM
Newbie
 
Join Date: Dec 2007
Posts: 4
Send a message via Yahoo to mick
Default One More Step

Hello again. You are right, I went back and looked and the brackets are there.
Now I need to wade out a little deeper.
Since I am using the FileWrite method to read the text in some controls and write it to a file, all the files are saved in the program folder of the app. ie
Program Files\My App.
How can I modify the file FileWrite to have the files saved in My Documents
so they are sync't automatically. I saw an example in here that used
Table1.SaveCSV("\My Documents\file.csv"...) To save a csv file but I can't see how I can use that, especially when I start the routine with "If FileExists" Can I get my app to look in my documents first and then write the file there. Thanks so much, Basic4PPC is the greatest development tool I've ever bought. I love it. Mick
Reply With Quote
  #7 (permalink)  
Old 01-19-2008, 06:12 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 2,964
Default

Just use "\My Documents\YourFile".
Code:
If FileExist ("\My Documents\YourFile") Then
  FileOpen(c1,"\My Documents\YourFile",cWrite)
End If
Reply With Quote
  #8 (permalink)  
Old 01-19-2008, 12:59 PM
Newbie
 
Join Date: Dec 2007
Posts: 4
Send a message via Yahoo to mick
Default Getting Closer

Thanks Erel. I'm almost there .I'm still having a bit of trouble because I'm first using
a variable to declare my file name.
MyFileName = "TestFile"
Suffix = ".txt"
If FileExist(MyFileName & Suffix) = true Then
MyFileName = "TestFile2"

That works Great,so when I try to inject the path like


MyFileName = "My Documents\TestFile"
Suffix = ".txt"
If FileExist(MyFileName & Suffix) = true Then
MyFileName = "My DocumentsTestFile2"

My error handler grabs it.
I Feel like such a pest here. I really do appreciate the amazing help I get here.
Thanks again Mick.
Reply With Quote
  #9 (permalink)  
Old 01-19-2008, 01:14 PM
specci48's Avatar
Basic4ppc Expert
 
Join Date: Apr 2007
Location: Germany
Posts: 611
Default

Quote:
Originally Posted by mick View Post
MyFileName = "My Documents\TestFile"
Suffix = ".txt"
If FileExist(MyFileName & Suffix) = true Then
MyFileName = "My Documents\TestFile2"
Is this missing \ a typo in you post or even in your code?

specci48
Reply With Quote
  #10 (permalink)  
Old 03-17-2008, 06:52 PM
Newbie
 
Join Date: Mar 2008
Posts: 2
Default not working for me :(

I copied and pasted the code then tweaked it for my fields. I get an error when trying to run the program:

Error compiling program.
Error description: lstdata is not a known control or object.

Did I miss something? I can paste all my code (for this Sub) if you like.

Thanks.
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Coverting text files with binary file page code functionality kolbe Code Samples & Tips 1 06-21-2008 05:06 PM
Label/Text Control text centering TWELVE Basic4ppc Wishlist 1 06-04-2008 03:20 PM
xls files instituto Questions & Help Needed 2 11-20-2007 12:29 AM
Text editor for INI files Softselect Questions & Help Needed 2 11-15-2007 06:50 AM
Dividing the code into text files... Cableguy Questions & Help Needed 5 05-21-2007 03:21 PM


All times are GMT. The time now is 11:29 PM.


Powered by vBulletin® Version 3.6.10
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.1.0