Problem Reading Text File

Just starting out? Need help? Post your questions and find answers here.
ColBoy
Enthusiast
Enthusiast
Posts: 143
Joined: Fri Feb 13, 2004 2:37 pm
Location: Ottawa, Canada
Contact:

Problem Reading Text File

Post 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?
Colin
ColBoy
Enthusiast
Enthusiast
Posts: 143
Joined: Fri Feb 13, 2004 2:37 pm
Location: Ottawa, Canada
Contact:

Re: Problem Reading Text File

Post by ColBoy »

Forgot to mention, tested on both 5.0 and 5.1 (R2) Windows 32bit and both show the same problem.
Colin
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Problem Reading Text File

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Problem Reading Text File

Post 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
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Problem Reading Text File

Post 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?
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: Problem Reading Text File

Post by Bisonte »

Maybe an invisible char like CHR(9) (Tab)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
FlixFlax
User
User
Posts: 18
Joined: Sat Apr 26, 2003 10:35 pm
Location: Denmark

Re: Problem Reading Text File

Post 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
Win10 , PB5.72
FlixFlax
User
User
Posts: 18
Joined: Sat Apr 26, 2003 10:35 pm
Location: Denmark

Re: Problem Reading Text File

Post by FlixFlax »

Sorry, my fault :oops:
Win10 , PB5.72
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Problem Reading Text File

Post by Demivec »

FlixFlax wrote:Sorry, my fault :oops:
What was the fault?
FlixFlax
User
User
Posts: 18
Joined: Sat Apr 26, 2003 10:35 pm
Location: Denmark

Re: Problem Reading Text File

Post 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.
Win10 , PB5.72
User avatar
skywalk
Addict
Addict
Posts: 4219
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Problem Reading Text File

Post 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)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
ColBoy
Enthusiast
Enthusiast
Posts: 143
Joined: Fri Feb 13, 2004 2:37 pm
Location: Ottawa, Canada
Contact:

Re: Problem Reading Text File

Post 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
Colin
FlixFlax
User
User
Posts: 18
Joined: Sat Apr 26, 2003 10:35 pm
Location: Denmark

Re: Problem Reading Text File

Post 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!
Win10 , PB5.72
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Problem Reading Text File

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply