f



Creating a TCL Channel from a C FILE* giving strange file handles

Hey Everybody! Having a problem with the Tcl C Library calls I cant seem to=
 figure out. I'm trying to create a channel from opened FILE *, and use it =
in a TCL script that sources the module. So, what I am running so far is:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
int openCFileCmd(ClientData clientData,=20
                             Tcl_Interp *interp, int objc, Tcl_Obj *CONST o=
bjv[])=20
{

    const char *fname =3D "cTest.txt";
    FILE *fp =3D fopen(fname, "w+");
    if (fp =3D=3D NULL) {
        fprintf(stderr,"File didn't create.\n");
        return TCL_ERROR;
    }
    const Tcl_Channel chan =3D Tcl_MakeFileChannel(fp, TCL_READABLE|TCL_WRI=
TABLE);
    if (chan =3D=3D NULL) {
        fprintf(stderr,"Bad channel returned!\n");
        return TCL_ERROR;
    }
    Tcl_RegisterChannel(interp, chan);

   //Tcl File name
    const char *name =3D Tcl_GetChannelName(chan);
    fprintf(stderr,"Got channel from GetChannel, name is %s\n", name);
    Tcl_SetObjResult(interp, Tcl_NewStringObj(name, strlen(name)));
    return TCL_OK;
}
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
Which ends up printing out the file name as something like "file32112312". =
Using it in the Tcl script that sources it, I can see that name registered =
properly with [chan names] and call other chan x commands, EXCEPT anything =
that reads/writes/flushes or closes the channel! It gives me back an error =
messages like so:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
tclsh8.5 [~/tcl_c]set c_fh [getCFile]
Got channel from GetChannel, name is file34710112
tclsh8.5 [~/tcl_c]puts $c_fh "Did this work?"
tclsh8.5 [~/tcl_c]cat cTest.txt
tclsh8.5 [~/tcl_c]flush $c_fh
error flushing "file34710112": bad file number
while evaluating {flush $c_fh}
tclsh8.5 [~/tcl_c]close $c_fh

while evaluating {close $c_fh}
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
The name looks way wrong, and its unusable. However, Its a proper channel i=
n the C, as I can call status calls on it and even get a writeable FILE* ou=
t of the channel using Tcl_GetChannelHandle. However, if I use the Tcl_Open=
FileChannel command things work great (const Tcl_Channel chan =3D Tcl_OpenF=
ileChannel(interp, fname, "w+", 777);) and I can use it Tcl scripts just fi=
ne. Its name also is more normal, being file4 or file3, etc.

Does anybody know what I am doing wrong? I've banged my head on this one fo=
r a few days now to no avail.
0
Wrenky
9/13/2015 3:39:49 AM
comp.lang.tcl 23429 articles. 2 followers. Post Follow

4 Replies
694 Views

Similar Articles

[PageSpeed] 6

Tcl_MakeFileChannel expects a operating system handle such as a file descriptor on Unix (eg returned by open()) or a HANDLE on Windows. You are passing it a FILE* as returned by fopen() which is not an operating system handle. It's a C RTL structure.

/Ashok

0
palmtcl
9/13/2015 4:01:04 AM
On Saturday, September 12, 2015 at 9:01:07 PM UTC-7, pal...@yahoo.com wrote:
> Tcl_MakeFileChannel expects a operating system handle such as a file descriptor on Unix (eg returned by open()) or a HANDLE on Windows. You are passing it a FILE* as returned by fopen() which is not an operating system handle. It's a C RTL structure.
> 
> /Ashok

Thanks for the reply! That makes more sense, it even says that in the man page! However, I am still having the same problems using open:
=================================================================
int openCFileCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) {

    const char *fname = "cTest.txt";
    if (objc != 1) {
        Tcl_WrongNumArgs(interp, 2, objv, "Just the call you dummy");
        return TCL_ERROR;
    }
    mode_t pmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
    int fp;
    if ((fp = open(fname,(O_RDWR|O_CREAT|O_TRUNC),pmode)) == -1) {
        fprintf(stderr, "Unable to open file!\n");
        return TCL_ERROR;
    }
    // does it write?
    write(fp,fname,strlen(fname));
    write(fp,"\n",2);
    fprintf(stderr,"Got a filehandle: --%d--\n",fp);
    const Tcl_Channel chan = Tcl_MakeFileChannel(&fp, (TCL_READABLE|TCL_WRITABLE));
    fprintf(stderr,"Converted it to a channel!\n");
    Tcl_RegisterChannel(interp, chan);
    fprintf(stderr,"Registered the channel\n");
    const char *name = Tcl_GetChannelName(chan);
    fprintf(stderr,"My channels name is --%s--\n",name);
    Tcl_SetObjResult(interp, Tcl_NewStringObj(name, strlen(name)));
    return TCL_OK;
}
=================================================================
which gives me this:
=================================================================
tclsh8.5 [~/tcl_c]package require Files
1.0
tclsh8.5 [~/tcl_c]set c_fh [getCFile]
Got a filehandle: --3--
Converted it to a channel!
Registered the channel
My channels name is --file-1801709324--
Channels match!
Done here!
file-1801709324
tclsh8.5 [~/tcl_c]puts $c_fh "test"
tclsh8.5 [~/tcl_c]cat cTest.txt
cTest.txt
tclsh8.5 [~/tcl_c]flush $c_fh
error flushing "file-1801709324": bad file number
while evaluating {flush $c_fh}
tclsh8.5 [~/tcl_c]close $c_fh

while evaluating {close $c_fh}
tclsh8.5 [~/tcl_c]
=================================================================
Same bad name, and same sort of error! Any advice?
0
Wrenky
9/13/2015 4:46:32 AM
You need to pass the file descriptor, not the address of the file descriptor.
So instead of 

Tcl_MakeFileChannel(&fp, (TCL_READABLE|TCL_WRITABLE))

it should be

Tcl_MakeFileChannel(fp, (TCL_READABLE|TCL_WRITABLE))

/Ashok
0
palmtcl
9/13/2015 4:52:04 AM
On Saturday, September 12, 2015 at 9:52:06 PM UTC-7, pal...@yahoo.com wrote:
> You need to pass the file descriptor, not the address of the file descriptor.
> So instead of 
> 
> Tcl_MakeFileChannel(&fp, (TCL_READABLE|TCL_WRITABLE))
> 
> it should be
> 
> Tcl_MakeFileChannel(fp, (TCL_READABLE|TCL_WRITABLE))
> 
> /Ashok

Thank you so much, this fixed the issue!
=======================================
 const Tcl_Channel chan = Tcl_MakeFileChannel((ClientData)fp, (TCL_READABLE|TCL_WRITABLE));
=======================================
0
Wrenky
9/13/2015 5:35:16 AM
Reply:

Similar Artilces:

Sourcing a TCL file from within a tcl file
I am a newbie which I am sure I don't need to state :) I am trying to create a variable with a TCL file that will source another tcl file when I type the variable name something like this if I enter A through my keyboard then source a.tcl Can someone show me how to do this thanks guy On Dec 10, 1:15=A0pm, guy <gcoletta2...@gmail.com> wrote: > I am a newbie which I am sure I don't need to state :) > > I am trying to create a variable with a TCL file that will source > another tcl file when I type the variable name > > something like this > > if I e...

Windows file handle from tcl channel
Is it possible to figure out the Windows HANDLE corresponding to the file channel ID returned by [file open foo] Or do I need to use some WinAPI/DLL/Tcl contraption (ffidl is wonderful by the way :-) and use the native FileOpen API call? I have an API to a USB device that I can open, and do I/O from pure TCL, but under windows I can also call device control functions from a DLL using the wonderful ffidl package. Unfortunately, to do the most interesting stuff, I need the native Windows HANDLE that results when I open the device for IO... Ralph rhempel wrote: > Is it possible to figure...

Getting a Tcl Channel from a C FILE*
Hey Everybody! Having a problem with the Tcl C Library calls that I cant seem to figure out. I'm trying to create a Tcl channel from opened FILE *, and use it in a TCL script that sources the module. Here is what I have so far: =========================================================================== int openCFileCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]) { const char *fname = "cTest.txt"; FILE *fp = fopen(fname, "w+"); if (fp == NULL) { fprintf(stderr,"File didn'...

How to create log file and command journal files for a TCL session
I am using TCL as the command language for my C++ application. How do I add the following? 1. Command journal file that records all TCL commands that the user issues during the session. The purpose of this file is to allow someone to reproduce the same command sequence at a later time. 2. Log file that contains everything that was displayed to the user by my application, as well as by TCL. In other words, everything that was output to stdout and stderr. I can imagine how to do this with a redirect, but I wonder if there is something more straightforward. Thank you for your help with on...

Tcl file from .txt file
Hi, I have a excel file. I need to change the excel file in to tcl file. I did = change the excel file in to txt file. but unable to change the txt file in = to tcl file. let me know any idea to change the txt file in to tcl file or = directly from the excel file in to tcl file.=20 your help is much more appreciated. thank you. On 11/14/14, 1:30 PM, yenilsuwal@gmail.com wrote: > Hi, > > I have a excel file. I need to change the excel file in to tcl file. I did change the excel file in to txt file. but unable to change the txt file in to tcl file. let me know any idea to c...

How does the Tcl interpret handle results returned from C-created Tcl commands ?
I've ventured on creating a Tcl command trough a C routine. I've followed the manual guidelines. My C routine checks the validity of a selected option for the currently displayed histogram. Therefore it returns a Boolean value that, according to the C <-> Tcl communucation protocol, I have encoded as TCL_ERROR (= FALSE) and TCL_OK (TRUE). WHenthis command is called in a Tcl proc, the interpreter is supposed to perform two different actions according to the returned Boolean value. How can I fetch this returned Boolean result (TCL_ERROR/ TCL_OK) from the Tcl proc that executes th...

Can I compile a tcl/tk extended C file into an exe file on windows?
I'm new to tcl/tk. I have a simple tk/tcl extended C file like this, #include <stdio.h> #include <stdlib.h> #include <tcl.h> #include <tk.h> int Tcl_AppInit(Tcl_Interp* interp) { int status; status = Tcl_Init(interp); if (status != TCL_OK) { return TCL_ERROR; } /* Initialize Tk values. */ status = Tk_Init(interp); if (status != TCL_OK) { return TCL_ERROR; } return TCL_OK; } int main(int argc, char** argv) { Tk_Main(argc, argv, Tcl_AppInit); return 0; } Can I compile this file into an exe file on windows? How? Thank you ysrise@gmail.com (yan) writes: [snip[ ...

How do I create a mex file from my C File?
I have this program and I need to make it a mex in matlab, how do you do it? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char *argv[]) { char text [100]; //Declare a File Pointer FILE *file_ptr; //Open the log file file_ptr=fopen("BlackHostLog.txt", "r"); //Test if(file_ptr != NULL) { printf("BlackHostLog.txt opened.\nContents:\n"); //Read the text int passed=0; while(fgets(text, 100, file_ptr)!=NULL && !passed) { printf("%s", text); passed=strstr(text, "Sending B...

new URL("c:/dir/file") OR new URL("c:\dir\file") OR new URL("file://c:/dir/file")
new URL("c:/dir/file") OR new URL("c:\dir\file") OR new URL("file://c:/dir/file") What is the correct syntax ? Is it the same as a new File ?FileDialog send: "c:\Program Files\dir\file". I have the feeling that different classes (File, URL, URl) have the different syntax. Please say a), b) or c) and answer me does it is same for all java classes which ask for a file path? FreeStyler =E3=81=AE=E3=83=A1=E3=83=83=E3=82=BB=E3=83=BC=E3=82=B8: > new URL("c:/dir/file") OR new URL("c:\dir\file") OR new > URL("file://c:/dir/fil...

Creating a mex file from more than one c file?
I have a C project that has a few files in it. The structure of the project is: main.c (this file has the mexFunction) network.h - included by main.c network.c - includes functions bodies from network.h I try to compile it from Matlab using: mex main.c and it gives: error LNK2001: unresolved external symbol Tried making network.obj and then: mex main.c Network.obj and it gives: error LNK2005: _wVersionRequested already defined in This is what the Matlab help says I should do- but it doesn't seem to work. Does anybody know of any resource that explains how to do that? Or did anybody try ...

Mex FIles, TCL Files, amd Simulink.
Hi, Everyone. I'm new at Matlab & Simulink, and although I've been making pretty good progress, I find myself rather stuck. I am writing a bit of C code to determine the time between too events: When event one happens, I record the current time in the state vactor, and when event 2 happends, I record the divverence in the state vator. My output is simply the difference. I have been trying to use the S-Function builder, but it doesn't seem to like ssGetT(S), where S is the SimStruct thingy - I don't know hot to get it passed into the Builder routine. So what I ha...

How to do data parsing from library file to tcl file?
Hi I have to copy some data from a library file and use it in a tcl command. Can some one tell me how to do that? I am new to tcl so finding it hard. On 07/01/2013 02:53 PM, Paromita Banerjee wrote: > Hi I have to copy some data from a library file and use it in a tcl command. Can some one tell me how to do that? I am new to tcl so finding it hard. Please explain what the phrase "library file" means to you. An example might be helpful. -- | Don Porter Applied and Computational Mathematics Division | | donald.porter@nist.gov Information Technol...

create a new .c-file from m-file?
Dear all. I am writing an m-file, from which I want to create a new .c- or .h-file and fill it with some lines of code. Does anyone know a M-command to create/open a new c-file? Thanks a lot Maike In article <flvidb$52j$1@fred.mathworks.com>, Maike <m.haug@fh-wolfenbuettel.de> wrote: >I am writing an m-file, from which I want to create a >new .c- or .h-file and fill it with some lines of code. >Does anyone know a M-command to create/open a new c-file? fopen() the file, and fprintf() the text you want. -- "No one has the right to destroy another person'...

Creating ZIP files with Tcl ?
Hi all, I'd like to create a ZIP file containing a bunch of text / binary files (log-files from our server) automatically for backup reasons. I've tried to use vfs::zip but it seems to work in read mode only. Next I tried to use Michels mkZiplib but this crashes as i try to close my ZIP file in the end (using the code from the examples on the WEBsite). I'm running WinXP with ActiveTcl 8.4.11. Does anyone have a clue how to continue? Regards - Leo We ended up spending $40 to get the latest ZIP version that has a command line option to do this - it works pretty well. Dave...

Web resources about - Creating a TCL Channel from a C FILE* giving strange file handles - comp.lang.tcl

Wikipedia:Creating a bot - Wikipedia, the free encyclopedia
It is often far simpler to request a bot job from an existing bot. If you have only periodic requests or are uncomfortable with programming languages, ...

U.K. Woman Faces 20 Months For Creating Fake Facebook Accounts, Sending Herself Abusive Messages
A woman in Cornwall in the U.K. came up with a creative way to use Facebook to accelerate a family spat, and it earned her 20 months in prison. ...

Sandi Krakowski’s Tips for Creating Great Content
Quick tips to help you create great content for your business.

Bug - Creating a Video.Watches Action stopped working - Facebook-Entwickler
Since today executing a POST on a me/video.wacthes endpoints no longer works, it was working around 10 hours ago since then the call always returns ...

Facebook simplifies process for creating repeat events
Facebook introduced a new feature for events this week that allows users to quickly create a similar event as one they previously hosted or attended. ...

The Obama campaign won by creating their own Facebook, with unique features for political campaigns.
... seen as too dangerous to nominate someone who is so thoroughly narcissistic, arrogant and dishonest. The Obama campaign won basically by creating ...

Creating A Road Map To Market Your Startup
Spreading the word can be challenging when you’re new to an industry, don’t have a big budget, and have little helpbut it’s possible.You’ll need ...

Dance Machine - The Fun Video Editor for Creating Music Videos to share on Instagram, Facebook, Vine ...
Get Dance Machine - The Fun Video Editor for Creating Music Videos to share on Instagram, Facebook, Vine, and Twitter on the App Store. See screenshots ...

Creating Economic Dynamism: Joseph E. Stiglitz
DAVOS/SWITZERLAND, 25JAN13 - Joseph E. Stiglitz, Professor, Columbia University, USA talks during the session 'Creating Economic Dynamism' at ...

YouTube - Super Human Abilities: Tila Tequila Creating Energy Balls & Electricity Out of Thin Air!
Veröffentlicht am 24.04.2013 Hey Guys! For a long time now I have been able to do some pretty supernatural things that most would consider "SUPER ...

Resources last updated: 1/25/2016 6:09:03 AM