Page 1 of 1
Problem Reading Text File
Posted: Fri Jan 04, 2013 10:08 pm
by ColBoy
I am reading a simple text file in one of my projects, but I seem to get extra lines no in the file I am reading.
A simplified version of my code is as follows:
Code: Select all
If ReadFile(0, file)
While Eof(0) = 0
lLine.s=ReadString(0)
lLength=Len(Trim(lline))
If llength>1 And Left(lline,1)<>";"
lCount=CountString(lLine,",")
Debug lline
If lcount<>2
MessageRequester("AVBuddy - Error","One or more lines has an incorrect number of parameters.")
End
EndIf
EndIf
Wend
EndIf
I've added the line
Code: Select all
if llength>1 And Left(lline,1)<>";"
because even if the line is blank, it still has a length of 1. Also even though my file has 3 lines, including a blank line, the loop reads an extra 3 lines:
Code: Select all
; IDE Options = PureBasic 5.00 (Windows - x86)
; CursorPosition = 2
; EnableXP
Which I thought were from the bottom of the source, as read outside the IDE, but my source does not have the last of the 3 lines.
Anyone got any ideas?
Re: Problem Reading Text File
Posted: Fri Jan 04, 2013 10:10 pm
by ColBoy
Forgot to mention, tested on both 5.0 and 5.1 (R2) Windows 32bit and both show the same problem.
Re: Problem Reading Text File
Posted: Fri Jan 04, 2013 10:40 pm
by IdeasVacuum
Code: Select all
If ReadFile(0, file)
While Not Eof(0)
sLine.s = ReadString(0)
iLength.i = Len(Trim(sLine))
If ((iLength > 1) And Not (Left(sLine,1) = ";")
iCount = CountString(sLine,",")
Debug sLine
If Not(iCount = 2)
MessageRequester("AVBuddy - Error","One or more lines has an incorrect number of parameters.")
End
EndIf
EndIf
Wend
EndIf
To help you, what we really need is a sample file to process.
Re: Problem Reading Text File
Posted: Fri Jan 04, 2013 10:45 pm
by infratec
Hi Colin,
it is not clear (for me) what you mean.
Have you opened the 'file' in a text editor?
How many lines have the file there ?
The following codes works Ok
Code: Select all
Filename$ = OpenFileRequester("", "", "*.*|*.*", 0)
ReadFile(0, Filename$)
While Not Eof(0)
Line$ = ReadString(0)
Line + 1
Debug "Line: " + str(Line) + " Length: " + str(Len(Line$))
Wend
CloseFile(0)
And if a line is empty (only #CRLF$), the length is 0.
So please, offer us a sample file.
Bernd
Re: Problem Reading Text File
Posted: Sat Jan 05, 2013 2:51 am
by MachineCode
ColBoy wrote:even if the line is blank, it still has a length of 1
How can a blank line have a length of 1? It should be 0, unless it consists of a space or something. Is that the case?
Re: Problem Reading Text File
Posted: Sun Jan 06, 2013 1:55 am
by Bisonte
Maybe an invisible char like CHR(9) (Tab)
Re: Problem Reading Text File
Posted: Sun Jan 06, 2013 3:52 pm
by FlixFlax
I can confirm the file-problem.
Simply try running the file.pb from the examples folder.
It does not give the right result!
It seems the problem is with the ReadString command.
I'm running v.5.00 on win 8
Re: Problem Reading Text File
Posted: Sun Jan 06, 2013 4:10 pm
by FlixFlax
Sorry, my fault

Re: Problem Reading Text File
Posted: Sun Jan 06, 2013 4:29 pm
by Demivec
FlixFlax wrote:Sorry, my fault

What was the fault?
Re: Problem Reading Text File
Posted: Sun Jan 06, 2013 5:04 pm
by FlixFlax
There is nothing wrong with file.pb
However, I do have a ReadString problem (I think)
I will try to fix it, and if not, return here

Thx for your interest.
Re: Problem Reading Text File
Posted: Sun Jan 06, 2013 6:06 pm
by skywalk
Instead of ReadString(), use nBytes = ReadData(#File, *MemoryBuffer, LengthToRead).
Then peek the *MemoryBuffer with different encodings to verify any problems.
s$ = PeekS(*MemoryBuffer, nBytes, Encoding)
Re: Problem Reading Text File
Posted: Mon Jan 07, 2013 3:32 pm
by ColBoy
I solved half the problem. I was playing with projects for the first time and added the text file to the project, which seems to add the extra lines. Didn't realise as the text file was open and the editor didn't warn me that the contents had changed. Sure enough when I opened the file the next day, the extra lines were there. Not got to the problem of the line length =1 yet, even though it is blank.
Colin
Re: Problem Reading Text File
Posted: Sun Jan 13, 2013 12:58 am
by FlixFlax
I'm sorry colboy for interfering your post. My problem turned out not to be the same as yours.
Just for the record, I tried to write a hiscore-input-procedure, using KeyboardInkey().
The input ends with >Return<
But this Carriage Return was then added to the saved file. And became a problem, when the file was reloaded.
Eventually I found the problem, and removed the CR before saving, and now it works.
Learning by doing!
Re: Problem Reading Text File
Posted: Sun Jan 13, 2013 2:35 am
by IdeasVacuum
ColBoy wrote:Not got to the problem of the line length =1 yet, even though it is blank
As mentioned, that could be caused by a 'hidden' character. If you were to make the file available to us, we could help you more.