reading from a file
Moderators: silvia, selimgunay, Moderators
reading from a file
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
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
Re: reading from a file
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] {
}
}
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] {
}
}
Re: reading from a file
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] {
>
> }
> }
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] {
>
> }
> }
Re: reading from a file
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.
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.
Re: reading from a file
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
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.
Re: reading from a file
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)
-
- Posts: 1
- Joined: Sat Jun 09, 2012 6:03 am
- Location: NewYork City
Re: reading from a file
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...... )
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...... )
Re: reading from a file
you can try this....
$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;