how to copy stdout??

This is a discussion on how to copy stdout?? within the C Programming forums, part of the General Programming Boards category; Problem:: i want to copy my stdout file into another temporary file from my program itself i.e., i want to ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    73

    how to copy stdout??

    Problem::
    i want to copy my stdout file into another temporary file from my program itself
    i.e., i want to update the changes i've made in the stdout to another file
    and i want a solution for the same in case of graphics

    i ve tried output redirection but couldn't proceed any further
    i don't think it is necessary to post the code as my question is quite general in nature
    if my code is necessary i'll post it...

    Can you guys out there help me please!!!!!!!!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I have no idea what you're talking about. stdout is a FILE *, yes, and hence can be thought of and dealt with as a file, but it's not a physical file necessarily. I don't understand what you mean when you say you want to copy it.

    Can you try to elaborate on your problem?

    Edit: Are you trying to duplicate all output to stdout to another file as well? So like all calls to printf() will go stdout and your file?
    Last edited by MacGyver; 03-07-2008 at 12:36 PM.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    If you want to write to more than one destination, you have to use more than one stream. A good way to do that is passing around an array of file pointers and looping over it each time you want to write something.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void print(const char *data, int size, FILE *outp[])
    {
        // An invalid size means data is a string
        if ( size <= 0 )
        {
            size = strlen(data);
        }
        
        // Write the data to every file pointer
        while ( *outp )
        {
            fwrite(data, 1, size, *outp);
            ++outp;
        }
    }
    
    int main(void)
    {
        FILE *outp[3] = {0};
    
        outp[0] = stdout;
        outp[1] = fopen("test.txt", "w");
    
        print("I am Banana Man!\n", 0, outp);
        print("This goes to stdout and a file\n", 0, outp);
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    yes i want to duplicate all the data in my stdout file to another file
    i then want to seek and change the file accordingly and copy it back to stdout....

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    BananaMan , your code works fine if the data is text ... but actually i am dealing with graphics in particular ...
    actually i want to store a picture from stdout into a file....

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by ElemenT.usha View Post
    yes i want to duplicate all the data in my stdout file to another file
    i then want to seek and change the file accordingly and copy it back to stdout....
    So what you're actually asking to do, is write all your output to a file INSTEAD of standard out; then go back and reprocess it, changing some things; and then display it?

    Because printing it to BOTH stdout and a file, then manipulating it and printing it again, makes no sense to me.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by brewbuck View Post
    So what you're actually asking to do, is write all your output to a file INSTEAD of standard out; then go back and reprocess it, changing some things; and then display it?

    Because printing it to BOTH stdout and a file, then manipulating it and printing it again, makes no sense to me.
    Is it just me or does it seem easier if he just figured out what the heck to print the first time around and printed it correctly to stdout?

  8. #8
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by ElemenT.usha View Post
    actually i want to store a picture from stdout into a file....
    ASCII art?
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    BananaMan , your code works fine if the data is text ...
    It doesn't matter what your data is because it's always going to be a list of bytes. If it's not text, you have to pass the number of bytes in the data, but it still works fine.
    Code:
    int main(void)
    {
        FILE *outp[3] = {0};
        int x = 256;
    
        outp[0] = stdout;
        outp[1] = fopen("test.txt", "wb");
    
        print(&x, sizeof(int), outp);
    
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    ok i think i need to post the code now....

    following is my code ::

    what does the code do:: it actually creates a virtual paint type environment where we can draw whatever we want (lines arcs only);
    all my problem is with my cursor... whenever my cursor moves over a drawn line it acts as an eraser ...
    it is because of my cursor function....


    so now i want to change my cursor function to something like this ... whenever the user moves the cursor it should clear screen and then the cursor moves and the picture which was before should load again as clearing screen causes a loss of my previous picture ... i want to store it in a file of something before clr screen....

    [red]WARNING:: this works only in dos based compilers i suppose and we need conio.h cant avoid it.....[/red]


    Code:
    #include<stdio.h>
    #include<dos.h>   //for delay
    #include<graphics.h>  
    #include<conio.h>   // for getch and clrscr
    void pendn(char);     /*acts as if pen    is down*./
    void penup(char);
    void erase(char);
    void cur(int,int,int);   /*cursor function*/
    void (*p)();    /*pointer to call functions*/
    void ul(int,int,int,int);   /*to draw a line*/
    static int x,y,x1=0,y1=0,x2,y2;
    
    
    int main()
    {
    	int gd=DETECT,gm;
    	char ch;int tmpx,tmpy,tmpr,st,et,i,j;
    	initgraph(&gd,&gm,"");
    	delay(1000);
    	x=getmaxx()/2;
    	y=getmaxy()/2;
    	rectangle(x+1,y+1,x-1,y-1);
    	p=penup;
    	do{
    		ch=getch();
    		if (ch=='s')
    		{
    			if(x1==0)
    			 {	x1=x;y1=y;
    				putpixel(x,y,15);
    				printf("\r&#37;d%d\t",x1,y1);
    			 }
    			else
    			 {
    				x2=x;y2=y;
    				printf("\r%d%d\t",x2,y2);
    				ch=getch();
    				if(ch=='1')
    					ul(x1,y1,x2,y2);
    				else if(ch=='2')
    				{
    					tmpx=(x1+x2)/2;
    					tmpy=(y1+y2)/2;
    					if(y1==y2)
    						tmpr=x2-tmpx;
    					if(x1==x2)
    						tmpr=y2-tmpy;
    					if(tmpr<0)
    						tmpr*=-1;
    					printf("\r enter angles:");
    					scanf("%d%d",&st,&et);
    					setcolor(15);
    					arc(tmpx,tmpy,st,et,tmpr);
    					putpixel(x1,y1,15);
    					putpixel(x2,y2,15);
    				}
    				x1=0;
    			 }
    		}
    		else
    		{
    			if(ch=='1')
    			{
    				ch=getch();
    				printf("\r pendown  ");
    				cur(x,y,0);
    				p=pendn;
    			}
    			else if(ch=='2')
    			{
    				ch=getch();
    				printf("\r penup   ");
    				p=penup;
    			}
    			else if(ch=='3')
    			{
    				ch=getch();
    				printf("\r eraser on");
    				p=erase;
    			}
    			(*p)(ch);
    		}
    	}while(ch!='i');
    	for(j=0;j<6;j++)
    	for(i=0;i<6;i++)
    	{
    		clrscr();
    		settextstyle(1,0,i);
    		settextjustify(1,1);
    		outtextxy(getmaxx()/2,getmaxy()/2,"an elemeNtal corp :))");
    		sound(200*j-100*i);
    		delay(50);
    	}
    	nosound();
    	getch();
    	return 0;
    }
    void pendn(char ch)
    {
    		switch(ch)
    			{
    			  case 'w' :putpixel(x,y-1,15);y--;break;
    			  case 'x' :putpixel(x,y+1,15);y++;break;
    			  case 'a' :putpixel(x-1,y,15);x--;break;
    			  case 'd' :putpixel(x+1,y,15);x++;break;
    			  case 'q' :putpixel(x-1,y-1,15);y--;x--;break;
    			  case 'e' :putpixel(x+1,y-1,15);y--;x++;break;
    			  case 'z' :putpixel(x-1,y+1,15);y++;x--;break;
    			  case 'c' :putpixel(x+1,y+1,15);y++;x++;break;
    			}
    
    }
    void penup(char ch)
    {
    	   cur(x,y,0);
    			switch(ch)
    			{
    			  case 'w' :y--;break;
    			  case 'x' :y++;break;
    			  case 'a' :x--;break;
    			  case 'd' :x++;break;
    			  case 'q' :y--;x--;break;
    			  case 'e' :y--;x++;break;
    			  case 'z' :y++;x--;break;
    			  case 'c' :y++;x++;break;
    			}
    	   cur(x,y,15);
    }
    void erase(char ch)
    {
    	cur(x,y,0);
    		switch(ch)
    			{
    			  case 'w' :putpixel(x,y-1,0);y--;break;
    			  case 'x' :putpixel(x,y+1,0);y++;break;
    			  case 'a' :putpixel(x-1,y,0);x--;break;
    			  case 'd' :putpixel(x+1,y,0);x++;break;
    			  case 'q' :putpixel(x-1,y-1,0);y--;x--;break;
    			  case 'e' :putpixel(x+1,y-1,0);y--;x++;break;
    			  case 'z' :putpixel(x-1,y+1,0);y++;x--;break;
    			  case 'c' :putpixel(x+1,y+1,0);y++;x++;break;
    			}
    	cur(x,y,15);
    }
    void cur(int x,int y,int col)
    {
    	setcolor(col);
    	rectangle(x+1,y+1,x-1,y-1);
    }
    void ul(int x1,int y1,int x2,int y2)
    {
    	int i,y,t;float m;
    	if(x1>x2 || y1>y2)
    	{
    		t=x1;x1=x2;x2=t;
    		t=y1;y1=y2;y2=t;
    	}
    	if(x1==x2)
    		for(i=y1;i<=y2;i++)
    		{
    			putpixel(x1,i,15);
    			delay(100);
    		}
    	else
    	{
    		m=(y2-y1)/(x2-x1);
    		for(i=x1;i<=x2;i++)
    		{
    			y=(m*(i-x1))+y1;
    			putpixel(i,y,15);
    			delay(100);
    		}
    	}
    }
    Last edited by ElemenT.usha; 03-08-2008 at 05:45 AM.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    It's 2008. I think you should be writing for something slightly newer than DOS.

  12. #12
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Quote Originally Posted by MacGyver View Post
    It's 2008. I think you should be writing for something slightly newer than DOS.
    Why? It might be a fun game for him to hack around plain old rocksteady DOS.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by xuftugulus View Post
    Why? It might be a fun game for him to hack around plain old rocksteady DOS.
    Because he's probably running it on Windows. That's basically asking us how to use 20+ year old technology to be run on top of a modern machine. Isn't that asking to be in the realm of undefined behavior to some degree? Programs compiled in 16-bit mode will run by a DOS emulator, not actually by Windows directly. That's why so many DOS games and programs don't work properly.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    ok so no one here knows how to copy the stdout file... thanxxx

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by ElemenT.usha View Post
    ok so no one here knows how to copy the stdout file... thanxxx
    Considering you don't even know how to properly ask the question, I think you would be wise not to speak about what we know and don't know.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Case Statement
    By danlee58 in forum C Programming
    Replies: 16
    Last Post: 11-23-2008, 07:46 PM
  2. Replies: 2
    Last Post: 07-12-2008, 12:00 PM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 09:42 AM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21