|
-
Nov 22nd, 2011, 08:27 AM
#1
Thread Starter
Hyperactive Member
cannot save to text file
I have this program that extract a text file.
Later with the same program, I open this text file for reading and editing and then save what was edited.
But when I try to save it, the program tells the file is open. What could be the problem?
But when I delete this extracted text file and replace it manually with my backup same text file for the same purpose, I can save successfully.
I guess the text file is extracted in somewhat way that cannot be used for editing and later for saving.
It must be noted that I have already tried to restart the computer and tried saving but still the program says the the file is already open.
The file is set to achive properties in windows.
Last edited by iamresearcher; Nov 22nd, 2011 at 08:31 AM.
-
Nov 22nd, 2011, 08:45 AM
#2
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?
-
Nov 25th, 2011, 08:42 AM
#3
Thread Starter
Hyperactive Member
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?
Last edited by iamresearcher; Nov 25th, 2011 at 08:46 AM.
-
Nov 25th, 2011, 08:47 AM
#4
Re: cannot save to text file
-
Nov 25th, 2011, 08:52 AM
#5
Thread Starter
Hyperactive Member
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.
-
Nov 25th, 2011, 09:01 AM
#6
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.
-
Nov 25th, 2011, 09:08 AM
#7
Thread Starter
Hyperactive Member
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.
-
Nov 25th, 2011, 09:31 AM
#8
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.
-
Nov 25th, 2011, 09:41 AM
#9
Re: cannot save to text file
 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.
-
Nov 25th, 2011, 01:34 PM
#10
Hyperactive Member
Re: cannot save to text file
Can you post your code, searcher? So that we can have a better look on your problem.
If your problem is solved, then drag down the Thread Tools and mark your thread as Resolved.
If I helped you solve your problem, inflate some air into my ego by rating my post and adding a comment too.
For notorious issues (elaborate yourself) contact me via PM. I don't answer them in the forums EVER.
-
Nov 26th, 2011, 08:40 PM
#11
Thread Starter
Hyperactive Member
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.
-
Nov 26th, 2011, 08:42 PM
#12
Re: cannot save to text file
Are you trying to save data to a text file but using the same format as ini?
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 26th, 2011, 08:44 PM
#13
Thread Starter
Hyperactive Member
Re: cannot save to text file
-
Nov 26th, 2011, 08:48 PM
#14
Re: cannot save to text file
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Nov 26th, 2011, 08:52 PM
#15
Thread Starter
Hyperactive Member
Re: cannot save to text file
I have tried that already. and still same error.
-
Nov 26th, 2011, 10:18 PM
#16
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|