redirect function output

This is a discussion on redirect function output within the C Programming forums, part of the General Programming Boards category; I checked the faq and searched the archives, but haven't found a clear answer. - and I'm very new at ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    15

    Question redirect function output

    I checked the faq and searched the archives, but haven't found a clear answer. - and I'm very new at this so type slowly when you answer....

    In a Linux OS using gcc

    I've written a function that accepts arguments passed from keyboard input. It outputs correctly to the screen right now, but I'd like to redirect the output of the FUNCTION to a file. (Ultimately I'd like to inject it into smtp - but for now if I can get it to a file I can get it into smtp via a perl script)

    I've tried :
    thefunction > newfile

    which I didn'nt think would work - and it doesn't

    any clues would be greatly appreciated,

    rm

  2. #2
    Unregistered
    Guest
    int myFunction(ostream &out, ...);
    .
    .
    .
    ostream fout(myfile);
    myFunction(cout);
    myFunction(fout); //
    fout.close();

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    I didnt realize I was on the C board (i forgot to log in last time)

    int myFunction(FILE *out, ...);
    .
    .
    .
    FILE *fp = fopen(myfile, "w");
    myFunction(stdout);
    myFunction(fp); // outputs to file instead of screen
    fclose(fp);

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    15

    clarification?

    Thanks for the reply Clown, I think I see what you're doing here..but I'm not sure I've got the format right. Couple of questions...


    int myFunction(FILE *out, ...);


    Are you saying that in my function declaration I should add "FILE *out," and then continue with the original arguments? or should this line be inserted somewhere else.
    .

    .
    I'm assuming that by these dots you mean the body of the function.
    .

    is this last statement intended to be inside the brackets of the function, or in it's own brackets? and do I need to declare a variable *fp somewhere?

    FILE *fp = fopen(myfile, "w");

    myFunction(stdout);

    myFunction(fp); // outputs to file instead of screen

    fclose(fp); [/B][/QUOTE]

    Sorry if the questions seem lame, I'll catch on eventually...

    rm

  5. #5
    Unregistered
    Guest

    clarifying my clarification ... I guess

    Ok, I figured this out. Using part of the previous suggestions and improvising some. Just to round out the thread in case anyone else searches the archive.


    This worked for me.




    in the body of the function i.e. inside the



    {



    FILE *newfile; //created a pointer for newfile



    newfile = fopen("file.to.be.opened","w");







    fprintf(newfile,"character string that is to be sent to the file that is now opened or created by the above statements",variables, used, in, text,);



    fclose(newfile)



    }







    and voila - a new file is created with the function output!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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