Trouble Writing Multiple Lines
Blitz3D Forums/Blitz3D Beginners Area/Trouble Writing Multiple Lines
| ||
Hi all, I'm trying to create a program that writes multiple lines of input to a file. However, for some reason, so far I've only been able to write the last line (which is "Close", because that's what stops the loop) to a file. How can I set this up so that that dosen't happen without using hundreds of thousands of variables? Sorry... I thought I posted it. Here you go: Thanks for you help. |
| ||
We'll need to take a looky at your code... :) |
| ||
Open the file (WriteFile()) before your loop starts - you only want to open it once. Write each line (WriteLine()) inside your loop. P.S. Indent each line in a block (e.g. inside a loop) with a Tab character to improve readability. |
| ||
Thank you all! This really helps! *Edit* Hello again, I finished with the creation part of my program, but now I'm having trouble with the part for opening files. I'm getting the error "Stream Does Not Exist". Here's my code: |
| ||
hmm I would use local-folder addresses rather than start at the top of the tree drive letters. Try just reading and writing to a file within the same folder as the .bb first |
| ||
Now I'm getting "Memory Acess Violations". I haven't changed anything... |
| ||
Maybe it's the Stream Does Not Exist error, but it's referred to as Memory Access Violation due to the Debugger being turned off? |
| ||
Well, this bit's wrong, for starters:fopen = OpenFile ("C:\temp\Test Docs\"+open$+".rcn") filetext= ReadFile (fopen) That opens the specified file and assigns the file handle to fopen; you then use this file handle as the name to open another file for reading (ReadFile). And the OpenFile line should be using the string variable openf$, not open$. |
| ||
@Malice: Yeah, you were right about the debug thing. It's giving me the stream error now. @big10p: I fixed the open$ error, thanks. But what should I use instead of ReadFile? |
| ||
Try this, Mr.Bob94:Graphics 1024,768 .start open$ = Input ("Would you like to open or create a file? O = Open, C = Create, Q = Quit.") If open$ = "O" Then Goto openscript If open$ = "C" Then Goto createscript If open$ = "Q" Then End .openscript openf$ = Input ("Please type the file you wish to open.") fopen = OpenFile ("C:\temp\Test Docs\"+openf$+".rcn") While Not Eof(fopen) Print ReadLine (fopen) Wend readf$ = Input ("Ready.") Repeat mainfile2$ = Input ("") WriteLine (fopen,mainfile2$) Until mainfile2$ = "Close" CloseFile fopen WaitKey Goto start .createscript save$ = Input ("Under what name do you want this log saved?") rcon = WriteFile("C:\temp\Test Docs\"+save$+".rcn") Writef$ = Input ("Ready.") Repeat mainfile$ = Input ("") WriteLine (rcon,mainfile$) Until mainfile$ = "Close" CloseFile rcon WaitKey Goto start |
| ||
Ok, I changed my sript to that, but I'm still getting the error. Here's my code now:.start Graphics 1024,768 open$ = Input ("Would you like to open or create a console session? O = Open, C = Create, Q = Quit.") If open$ = "O" Then Goto openscript If open$ = "C" Then Goto createscript If open$ = "Q" Then End .openscript openf$ = Input ("Please type the file you wish to open.") fopen = OpenFile ("\Documents\"+openf$+".rcn") While Not Eof(fopen) Print ReadLine (fopen) Wend readf$ = Input ("Ready.") Repeat mainfile2$ = Input ("") WriteLine (fopen,mainfile2$) Until mainfile2$ = "Close" CloseFile fopen WaitKey Goto start .createscript save$ = Input ("Under what name do you want this log saved?") Writef$ = Input ("Rynet Console Loaded.") rcon = WriteFile("C:\Documents and Settings\Max Zimon\My Documents\IDL Files\Programs\Documents\"+save$+".rcn") Repeat WriteLine (rcon,mainfile$) mainfile$ = Input ("") Until mainfile$ = "Close" WriteLine (rcon,mainfile$) CloseFile rcon WaitKey Goto start Thanks again for all your help. :) |
| ||
This bit looks like an error to me:Until mainfile$ = "Close" WriteLine (rcon,mainfile$) CloseFile rcon WaitKey look at big10p's code |
| ||
No, that part works fine for me. It's just in the Open File part that I'm getting errors. |
| ||
Try...fopen = OpenFile ("C:\Documents and Settings\Max Zimon\My Documents\IDL Files\Programs\Documents\"+openf$+".rcn") If fopen=0 Then Print "Unable to open logfile" Goto openscript EndIf To see why the change to an abolute path might be relevant: Print SystemProperty("appdir") Relative paths from your program will work when you create an executable. rcon = WriteFile("C:\Documents and Settings\Max Zimon\My Documents\IDL Files\Programs\Documents\"+save$+".rcn") If rcon=0 Then Print "Unable to create logfile" Goto createscript EndIf |
| ||
I can't use the "Print SytemProperty("appdir")" command, it gives me a "Function Not Found" error. As for your suggestion, that does stop the error, but I still can't read and then write to the file, and that's what I'm trying to do. Thanks for the help. |
| ||
Sorry, typo - corrected. It is SystemProperty(). If you want to append to a file check out the Filesize() and Seekfile() functions. They might be helpful. |
| ||
Thanks. This is interesting, but I really could use some help with getting the files to open. I don't mean to push, it's just that I need to get this working soon. |
| ||
It seems to work ok here with a couple of changes. |
| ||
That's amazing! Wow!! Thank you so much, it works perfectly! |