Jump to content

Can't open a file and use FileRead


Hylia
 Share

Recommended Posts

Hi, I'm trying to read a file one character at a time and store it in an array, however, I keep getting error 1 on FileRead, after searching on the forums for some time and trying all solutions mentioned nothing seems to help.

 

I would greatly appreciate some help with my code.

 

#include <file.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
Global $lines[36]
$fileloc = "Z:\Autoit Projects\text\001.txt"
Func ParseMain()
   msgbox(0, "FILE", $fileloc)
   $fileOpened = FileOpen($fileloc)

   for $c = 0 to _FileCountLines($fileOpened) Step 1
      $lines[$c] = FileRead($fileOpened, $c+1)
      msgbox(0, @error, $lines[$c])
   Next
   FileClose($fileOpened)
EndFunc

 

 

Edited by Hylia
I can't edit my post or reply to this post??!?
Link to comment
Share on other sites

I believe FileRead is reading a set number of characters from the entire file.  You can read one line at a time with FileReadLine and then use string methods to parse the characters one at a time for each line.

@‌Danyfirex has the solution

Edited by Jfish
mistake

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

I don't see where you set the $lines array to store anything?  That array looks empty?  In either case, I believe FileRead is reading a set number of characters from the entire file.  You can read one line at a time with FileReadLine and then use string methods to parse the characters one at a time for each line.

$lines[$c] = FileRead($fileOpened, $c+1), that's where the array stores the gotten data.

FileReadLine also will not work, and still give me error 1.

 

Here's the msgbox resulting: http://i.imgur.com/XQ7JbpM.png

The error is 1 and the line read is nothing.

Edited by Hylia
Link to comment
Share on other sites

Try this:

#include <file.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
Local $aLines=FileReadToArray("1.txt")

_ArrayDisplay($aLines)

Local $aChars[UBound($aLines)]

For $i=0 to UBound($aLines)-1
$aChars[$i]=StringSplit($aLines[$i],"")
_ArrayDisplay($aChars[$i])
Next

_FileCountLines need a file path not a handle.

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

Try this:

#include <file.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
Local $aLines=FileReadToArray("1.txt")

_ArrayDisplay($aLines)

Local $aChars[UBound($aLines)]

For $i=0 to UBound($aLines)-1
$aChars[$i]=StringSplit($aLines[$i],"")
_ArrayDisplay($aChars[$i])
Next

_FileCountLines need a file path not a handle.

Saludos

I'm not sure what it's supposed to show but it's not showing anything.

And yes, I changed "1.txt"

Edited by Hylia
Link to comment
Share on other sites

be sure you're using Lastest AutoIt Version.

Pass some txt example. And show in details how you need the output. 

 

Saludos

Link to comment
Share on other sites

Telling us what you intend to do by reading "character by character" would also greatly help us help you. Guesswork: you're making your life difficult.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

and at last you could join the arrays to a single array:

#include <Array.au3>

local $aryy[2] = ["Autoit","Programming"]
local $bryy[2] = ["is","good"]

_arraydisplay(_ArrayJoin($aryy,$bryy))


Func _ArrayJoin($ary1,$ary2)
$total = UBound($ary1) + UBound ($ary2) -2
ConsoleWrite (@CRLF &"total =" &$total)
Local $ary[$total*2]
For $i = 0 To UBound($ary1) -1
$ary[$i] = $ary1[$i]
Next
_ArrayDisplay($ary)
ConsoleWrite (@CRLF &UBound($ary1))
For $i = UBound($ary1) To $total
$ary[$i] = $ary2[$i - UBound($ary1)]
Next
$ary[UBound($ary)-1] = $ary2[UBound($ary2)-1]
Return $ary
EndFunc

my best choice goes to Kingbobs code

Edited by Surya

No matter whatever the challenge maybe control on the outcome its on you its always have been.

MY UDF: Transpond UDF (Sent vriables to Programs) , Utter UDF (Speech Recognition)

Link to comment
Share on other sites

If all you want to do is read the text file and store each character into different elements of an array, then all you need is this.

#include <Array.au3>
Local $sLines = FileRead("1.txt")
Global $aTextArray = StringSplit($sLines, "")

_ArrayDisplay($aTextArray)

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Reading one character from file. So strange. Anyways, you can use FileRead function as KingBob said.

Local $file_path = "K:\Your_Folder\Your_File.txt"
Local $readed_char = FileRead($file_path,1) ; read the help file for the second parameter of this function.
MsgBox(0,"",$readed_char)

 

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

If all you want to do is read the text file and store each character into different elements of an array, then all you need is this.

#include <Array.au3>
Local $sLines = FileRead("1.txt")
Global $aTextArray = StringSplit($sLines, "")

_ArrayDisplay($aTextArray)

 

That code works perfectly for what I want to do, thanks.

Edited by Hylia
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...