Re: cannot save to text file
The problem could be that the file is open.
If you posted the code you are using then I wouldn't need to ask this, but how are you "opening" the file?
How do you make changes?
How are you trying to save it again?
Re: cannot save to text file
Hack,
Like I said, when that certain file is extract from my program and I read it, I cant do a modification and save it.
But that certain file is not coming from my program, like I copy and paste it there on the application path from my usb or somewhere, I can read, modify and save it.
And even if I restart my computer, so that means if my program is still doing something on that file, it will be stopped from doing so since I have restarted my computer. But when I read, modify and save, it is still failure.
So I guess there is something on the extraction that makes it read-only file something like that but when I check the windows file property of that file, it is set to archive.
What could it be?
Re: cannot save to text file
Re: cannot save to text file
si_the_geek,
thanks for your response. but that file is settings file of the program that needs to be read for proper execution of the program. if can be modified thru the program if there is a need to change some settings.
Re: cannot save to text file
So what? The same rules apply no matter what the purpose of the file is.
Simply moving the file to a valid location is very likely to solve your problem.
Re: cannot save to text file
while it is possibly good to be on the valid location but the program is suppose to be portable, thus the setting must always accompanying the software.
Re: cannot save to text file
It isn't just "good" to use a valid location, it is mandatory if you want your program to work.
Some locations not listed in the article (such as on a USB drive) are valid if security of the folder is set appropriately, but there are plenty of locations that aren't (such as the Program Files folders, the root of a hard drive, non-writeable DVDs, etc).
Try temporarily using a valid location to see if it stops the problem. If it does, you can then think about how to pick a valid location at run time.
Re: cannot save to text file
Quote:
Originally Posted by
iamresearcher
but that file is settings file of the program that needs to be read for proper execution of the program. if can be modified thru the program if there is a need to change some settings.
So in your program you read the settings file and later on try to write to it. Sounds like you haven't closed it before trying to update it.
Perhaps if you posted the code you are using it would help.
Re: cannot save to text file
Can you post your code, searcher? So that we can have a better look on your problem.
Re: cannot save to text file
Code:
Option Explicit
Public Function ReadIniValue(INIpath As String, Key As String, Variable As String) As String
On Error GoTo MyError
Dim NF As Integer
Dim Temp As String
Dim LcaseTemp As String
Dim ReadyToRead As Boolean
AssignVariables:
NF = FreeFile
ReadIniValue = ""
Key = "[" & LCase$(Key) & "]"
Variable = LCase$(Variable)
EnsureFileExists:
Open INIpath For Binary As NF
Close NF
SetAttr INIpath, vbArchive
LoadFile:
Open INIpath For Input As NF
While Not EOF(NF)
Line Input #NF, Temp
LcaseTemp = LCase$(Temp)
If InStr(LcaseTemp, "[") <> 0 Then ReadyToRead = False
If LcaseTemp = Key Then ReadyToRead = True
If InStr(LcaseTemp, "[") = 0 And ReadyToRead = True Then
If InStr(LcaseTemp, Variable & "=") = 1 Then
ReadIniValue = Mid$(Temp, 1 + Len(Variable & "="))
Close NF: Exit Function
End If
End If
Wend
Close NF
Exit Function
MyError:
Exit Function
Resume Next
End Function
Code:
Public Function WriteIniValue(INIpath As String, PutKey As String, PutVariable As String, PutValue As String)
Dim Temp As String
Dim LcaseTemp As String
Dim ReadKey As String
Dim ReadVariable As String
Dim LOKEY As Integer
Dim HIKEY As Integer
Dim KEYLEN As Integer
Dim VAR As Integer
Dim VARENDOFLINE As Integer
Dim NF As Integer
Dim X As Integer
AssignVariables:
NF = FreeFile
ReadKey = vbCrLf & "[" & LCase$(PutKey) & "]" & Chr$(13)
KEYLEN = Len(ReadKey)
ReadVariable = Chr$(10) & LCase$(PutVariable) & "="
EnsureFileExists:
Open INIpath For Binary As NF
Close NF
SetAttr INIpath, vbArchive
LoadFile:
Open INIpath For Input As NF
Temp = Input$(LOF(NF), NF)
Temp = vbCrLf & Temp & "[]"
Close NF
LcaseTemp = LCase$(Temp)
LogicMenu:
LOKEY = InStr(LcaseTemp, ReadKey)
If LOKEY = 0 Then GoTo AddKey:
HIKEY = InStr(LOKEY + KEYLEN, LcaseTemp, "[")
VAR = InStr(LOKEY, LcaseTemp, ReadVariable)
If VAR > HIKEY Or VAR < LOKEY Then GoTo AddVariable:
GoTo RenewVariable:
AddKey:
Temp = Left$(Temp, Len(Temp) - 2)
Temp = Temp & vbCrLf & vbCrLf & "[" & PutKey & "]" & vbCrLf & PutVariable & "=" & PutValue
GoTo TrimFinalString:
AddVariable:
Temp = Left$(Temp, Len(Temp) - 2)
Temp = Left$(Temp, LOKEY + KEYLEN) & PutVariable & "=" & PutValue & vbCrLf & Mid$(Temp, LOKEY + KEYLEN + 1)
GoTo TrimFinalString:
RenewVariable:
Temp = Left$(Temp, Len(Temp) - 2)
VARENDOFLINE = InStr(VAR, Temp, Chr$(13))
Temp = Left$(Temp, VAR) & PutVariable & "=" & PutValue & Mid$(Temp, VARENDOFLINE)
GoTo TrimFinalString:
TrimFinalString:
Temp = Mid$(Temp, 2)
Do Until InStr(Temp, vbCrLf & vbCrLf & vbCrLf) = 0
Temp = Replace(Temp, vbCrLf & vbCrLf & vbCrLf, vbCrLf & vbCrLf)
Loop
Do Until Right$(Temp, 1) > Chr$(13)
Temp = Left$(Temp, Len(Temp) - 1)
Loop
Do Until Left$(Temp, 1) > Chr$(13)
Temp = Mid$(Temp, 2)
Loop
OutputAmendedINIFile:
Open INIpath For Output As NF
Print #NF, Temp
Close NF
End Function
Reading file:
ReadIniValue(App.Path & "\config.txt", "Section1", "Labor")
Write to file:
WriteIniValue App.Path & "\config.txt", "Section1", "Labor", Rule1
As you can see, the readinivalue and writeinivalue it opens and closes the connection on each use of the function.
Re: cannot save to text file
Are you trying to save data to a text file but using the same format as ini?
Re: cannot save to text file
Re: cannot save to text file
Re: cannot save to text file
I have tried that already. and still same error.
Re: cannot save to text file
In your ReadIniValue routine, if you successfully open the file after "Open INIpath For Input As NF" and an error occurs, your routine jumps to the MyError label and exits the function. The file is never closed in this case. Suggestions:
1) Whenever you get to a line where you close the file, reset the file number to zero after closing, i.e:
2) In your error handler check to see if NF is zero & if not, then close it, i.e:
Code:
MyError:
If NF <> 0 Then Close NF
Don't know if that is the exact problem or not, but it is a potential problem nonetheless
Also, you have no error handling in your WriteIniValue function. A similar situation can occur where an error occurs and the routine exits with a file still opened. Suggest adding error checking there too