Thread: Writing a script

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    Writing a script

    I know that C probably isn't the best language to be scripting in but I'm not too familiar with my linux virtual machine so I'm not sure how easy it would be to install python or java or something.

    I'm trying to write a script and I need to be able to get the last line of output from the console. Here's what I have so far:
    Code:
    void main()
    {
    	char text[200];
    	FILE *f1,*f2 ;
    	f1 = popen("./program1"
    ,"w");
    	f2 = popen("./program2","w");
    	while (fgets(text, 200, f2) != NULL)
        		printf("Output is %s", text);
    }
    I think I need the two popens because program1 blocks and I want program2 to still execute. This doesn't print anything even though there's output from program1 and 2

    Thanks

    Here's the tutorial I've been following:
    http://www.opengroup.org/onlinepubs/...ons/popen.html
    Last edited by jcafaro10; 04-14-2009 at 12:44 PM.

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I started a little smaller with this code:
    Code:
    char text[200];
    	FILE *f1,
            f1 = popen("./program1","w");
    	while (fgets(text, 200, f1) == NULL)
        		{}
    	while (fgets(text,200,f1)!=NULL)
    		printf("Text is %s\n",text);
    	pclose(f1);
    Because program1 takes some time to execute, so I need to wait but I don't think I'm waiting right.

  3. #3
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    I know that C probably isn't the best language to be scripting in but I'm not too familiar with my linux virtual machine so I'm not sure how easy it would be to install python or java or something.
    it depends on the distro you are using...if you are using ubuntu or fedora or opesuse or mandriva or debian or similar distro its only a matter of having internet access and typing 1 line of command into the terminal...and the distros package manager would automatically fetch the entire thing from the internet(its repositories) and install them (and mostly auto-configure them) for you - all by itself.

    I'm trying to write a script and I need to be able to get the last line of output from the console.
    could you maybe be more clear , maybe give a simple illustration of what you would want to output to look as or something....
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    not related to the problem but since you use c for scripting i wanted to let you know about this compiler that supports scripting by adding the #! line in top of the c sources. just in case you did not know about it. i think it looks very interesting.

    TCC : Tiny C Compiler

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Ok, so here's my updates with some more comments to show what I'm trying to do:
    Code:
           char text[200];
    	FILE *f2 ;
    	f2 = popen("./program1","r");//this program takes a little while to produce output, a few seconds
    	while(strlen(text)==0)//just sit here until there's something on the output
    	{
    		text = fread(text,sizeof(char),200,f2);
    	}
    	printf("Text is %s\n",text);//just print out the output for now to make sure we received any
    	pclose(f2);

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    BTW, C is not a "scripting" language. It's a language that requires compiling source code into executable files. There's a difference.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by jcafaro10 View Post
    Ok, so here's my updates with some more comments to show what I'm trying to do:
    Code:
           char text[200];
    	FILE *f2 ;
    	f2 = popen("./program1","r");//this program takes a little while to produce output, a few seconds
    	while(strlen(text)==0)//just sit here until there's something on the output
    	{
    		text = fread(text,sizeof(char),200,f2);
    	}
    	printf("Text is %s\n",text);//just print out the output for now to make sure we received any
    	pclose(f2);
    fread() function reads from a stream and returns the size of the data it reads...why are you assigning that to a character array?
    Last edited by creeping death; 04-14-2009 at 09:25 PM.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Script in games
    By Shakti in forum Game Programming
    Replies: 7
    Last Post: 09-27-2006, 12:27 AM
  2. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 11:30 AM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. help! fifo read problem
    By judoman in forum C Programming
    Replies: 1
    Last Post: 08-16-2004, 09:19 AM
  5. Game structure, any thoughts?
    By Vorok in forum Game Programming
    Replies: 2
    Last Post: 06-07-2003, 01:47 PM