saving and opening files ?
BlitzMax Forums/BlitzMax Programming/saving and opening files ?
| ||
Hey, guys, I'm trying to make a simple text editor and want it to open and save special formats.could you give me any info on how to do that ? I already know how to use the requestfile(). twinprogrammer |
| ||
Local file1:TStream = WriteFile("mydoc.txt") If Not file1 Then RuntimeError "Unable to open file" file1.WriteString("Good day sir") file1.WriteLine("This will end with a new line") file1.Close() Local file2:TStream = ReadFile("mydoc.txt") If Not file2 Then RuntimeError "Unable to open file" Local text2:String = file2.ReadString(12) '12 chars Local line2:String = file2.ReadLine() 'until new line file2.Close() Print text2 Print line2 I don't know what you mean by special formats. Last edited 2011 Last edited 2011 |
| ||
kind of like "myfile.specialfile" |
| ||
Do you mean file extensions? You can give the files any file extension you like. They are just the name. If you want to write a special format such as pdf, doc, then things are a lot more tricky. |
| ||
Alright thanks ! Twinprogrammer |
| ||
If you want to write a special format, you'll generally need to find the file format specifications. In a lot of cases, this is public information, available either from the "owner"/creator of the format or just from general interest websites. In some cases, there might be an SDK available to aid you. There's no generic answer though, it's specific to the "special formats" you actually want to write. Note that many of these special formats will have multiple versions. PDF, for example, has a dozen or so. So it's a pretty substantial task and can require some strategic planning to decide which file version(s) you want to support. |
| ||
One thing to think about is if by special format you mean *your own* proprietary binary format, or if you are just encoding straight up text/strings in your file. For example, a .txt file you can open up anywhere and see the encoded characters in just about any text editor. A Microsoft .doc file, while still semantically a text file, it is binary encoded to include text along with lots of other information (like fonts, styling, images, etc). To go the binary route, you will want to look to the WriteInt, WriteByte, WriteXXX/ReadXXX functions. These will allow you to write in any type of arbitrary data into a file and read it back out. If it's anything other than just straight up strings, that's enough to consider it a "special format" in that you need a "special program" that knows what to do with all the data contained inside. Hope that helps/makes sense. Last edited 2011 |
| ||
That's really helpful, do you know how to open a file without the requestfile() function ? |
| ||
ReadFile, WriteFile or OpenFile (for both reading and writing) RequestFile on its own doesn't do anything.. Local filename:String = RequestFile("Select a file") Local file1:TStream = OpenFile(filename) If Not file1 Then RuntimeError "Unable to open " + filename |
| ||
Could i use that to open a notepad txt format and save it to a .specialtext format ? |
| ||
To load a notepad txt you can simply use: Local Text:String = LoadText("TheFileName.txt") what you do then with that string depends on you. Saving the string again as text can be done with SaveText(Text:String , "TheOutputFileName.txt") or even SaveText(Text:String , "TheOutputFileName.specialtext") but the file would still be a text file, like a notepad file, regardless of the .specialtext extension. |
| ||
Could I open the text and show it on screen ? |
| ||
If you have MaxGui installed here is a simple example: |
| ||
Hey guys! I'm writing a text editor too, but I'm having problems with lines saved in the .txt file: they doesn't apply! I tried the jsp's code above and it saves fine, but when I open the .txt in Notepad (Windows), the text has only 1 line... all lines are mixed in just 1 line. And I'm having the same problem with my code. So, I tried the above code again and tried to open the .txt with the "Load" option and guess what? The lines are read! Would you or someone know what the cause is? Thank you! |
| ||
Solved! http://www.blitzbasic.com/Community/posts.php?topic=89396#1015166 |
| ||
I would suggest, unless you really need the Windows version of txt file, that you don't make the substitution linked above. Should you port your program to Mac or Linux, the file could give you unexpected results depending on what it is used for. Instead, keep it in the standard format and use a better Windows program for reading it. Wordpad will show those files properly. I would suggest installing Notepad++, it has many more features than Notepad and it also reads standard .txt files as well as Windows .txt files. |
| ||
Thank you! |