reading from a file

If you have a script you think might be useful to others post it
here. Hopefully we will be able to get the most useful of these incorporated in the manuals.

Moderators: silvia, selimgunay, Moderators

Post Reply
majid-OS
Posts: 10
Joined: Thu Apr 21, 2011 2:01 pm

reading from a file

Post by majid-OS » Wed May 04, 2011 11:09 am

Hi,

I want to read values from a file. Can anyone tell me what command I should use?
The command "read" gets the whole numbers from a file as a string but I want numbers in an array or in a numerical format.

Thanks,
Majid

fmk
Site Admin
Posts: 5866
Joined: Fri Jun 11, 2004 2:33 pm
Location: UC Berkeley
Contact:

Re: reading from a file

Post by fmk » Fri May 06, 2011 10:18 am

i suggest you read this: http://wiki.tcl.tk/367

for small files:

foreach line [split [read $inFile] \n] {
foreach word [split $line] {

}
}

for larger files you might want to use gets (it is slower)

while { [gets $inFile line] >= 0 } {
foreach word [slit $line] {

}
}

majid-OS
Posts: 10
Joined: Thu Apr 21, 2011 2:01 pm

Re: reading from a file

Post by majid-OS » Sat May 07, 2011 1:21 pm

Thanks Frank, It was a great help. I have still one problem in this regard.
These commands work perfectly when I have a *.txt file ready before running the program, but it doesn't work when I make the *.txt file inside the code.
For example if I make a *.txt file using a recorder and then analyze the problem, I can't read that file in the same .tcl file but after the previous stage of analysis. It seems that the *.txt file is not accessible during running the .tcl file. Is there a way to end the recorder and access the file while running a single .tcl code?
Thanks,
Majid

fmk wrote:
> i suggest you read this: http://wiki.tcl.tk/367
>
> for small files:
>
> foreach line [split [read $inFile] \n] {
> foreach word [split $line] {
>
> }
> }
>
> for larger files you might want to use gets (it is slower)
>
> while { [gets $inFile line] >= 0 } {
> foreach word [slit $line] {
>
> }
> }

fmk
Site Admin
Posts: 5866
Joined: Fri Jun 11, 2004 2:33 pm
Location: UC Berkeley
Contact:

Re: reading from a file

Post by fmk » Mon May 16, 2011 9:56 am

you can remove recorder $tag, where tag is returned by the recorder command.

the reason you can't oen file during the analysis, is that to greatly improve performance files are opened and not closed again until a wipe, the program terminates, or a remove recorders or remove recorder command is issured.

afshari
Posts: 1
Joined: Fri Feb 03, 2012 1:23 am
Location: iran

Re: reading from a file

Post by afshari » Sun Feb 05, 2012 12:09 am

hi dear fmk
i try to open file by command:
set fb [open"نام.File extension" r]
but this command get me error.
is this command true?
the file name is arabic. Is this okay?
thank you

Travel to usa with ESTA-register.org
Last edited by afshari on Tue Jun 16, 2015 11:10 pm, edited 1 time in total.

fmk
Site Admin
Posts: 5866
Joined: Fri Jun 11, 2004 2:33 pm
Location: UC Berkeley
Contact:

Re: reading from a file

Post by fmk » Mon Feb 06, 2012 2:53 pm

you are opening a file to read .. are you sure the file exists, if that is not it, i am not sure about the arabic (try another name and i presume "File extension" is not the actual file extension you provide, if it is change it)

davidboon912
Posts: 1
Joined: Sat Jun 09, 2012 6:03 am
Location: NewYork City

Re: reading from a file

Post by davidboon912 » Sat Jun 09, 2012 6:14 am

i suggest you read this: http://wiki.tcl.tk/367

for small files:

foreach line [split [read $inFile] \n] {
foreach word [split $line] {

}
}

for larger files you might want to use gets (it is slower)

while { [gets $inFile line] >= 0 } {
foreach word [slit $line] {

}
}

hey thanks for this information it helps me and i have worked out...... :))

dikase
Posts: 1
Joined: Tue Sep 11, 2012 12:08 am

Re: reading from a file

Post by dikase » Tue Sep 11, 2012 12:11 am

you can try this....
$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;

Post Reply