a newbie question about file output

This is a discussion on a newbie question about file output within the C Programming forums, part of the General Programming Boards category; hey guys, i am a newbie here and in programming. recently i have a problem that i have no idea ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    4

    Unhappy a newbie question about file output

    hey guys,
    i am a newbie here and in programming.
    recently i have a problem that i have no idea to solve with the basic file input/ouput function.

    i was told to write a function, call plot, to plot the graph on the screen that i can solve easily.
    in that plot function i have a bunch of printf("xxxx") stuff;
    how ever, here is the problem
    another part of the assignment ask me to have the output of that plot function written into a "report.txt" file.
    i am wondering if there is some function that i can call to pass the plot function, then it would get what is supposed to be printed transfer to the report.txt file?

    thx a lot guys

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You can use fopen() to open up a file, and then use fprintf() to print the text to the file.

    Code:
    FILE *f = fopen("report.txt","w");
    fprintf(f,"Writing something to the text file");
    fclose(f);

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    4
    Quote Originally Posted by bithub
    You can use fopen() to open up a file, and then use fprintf() to print the text to the file.

    Code:
    FILE *f = fopen("report.txt","w");
    fprintf(f,"Writing something to the text file");
    fclose(f);

    but then i need to have another copy of that plot function again and replace the printf with fprintf, is there other way to do it?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I guess I dont understand what your problem is. Why is it a problem to add in a fprintf() statement every time you have a printf() statement?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Code:
    void plot ( FILE *fp ) {
      // do a bunch of stuff
    }
    To print it all to stdout, do this
    Code:
    plot( stdout );
    To print it all to a file, do this
    Code:
    FILE *fp = fopen( "result.txt", "w" );
    plot( fp );
    close( fp );
    Easy money
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    4
    Quote Originally Posted by Salem
    Code:
    void plot ( FILE *fp ) {
      // do a bunch of stuff
    }
    To print it all to stdout, do this
    Code:
    plot( stdout );
    To print it all to a file, do this
    Code:
    FILE *fp = fopen( "result.txt", "w" );
    plot( fp );
    close( fp );
    Easy money

    gd call Salem, thx a lot, its really a gd point that i miss.
    and sorry bithub, i didnt explain clearly (english is not my first language >_<)

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by maybe
    gd call Salem, thx a lot, its really a gd point that i miss.
    and sorry bithub, i didnt explain clearly (english is not my first language >_<)
    Well since English isn't your first language, let me give you a pointer. The word good has two Os in it. It's not spelled "gd".

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    4
    Quote Originally Posted by quzah
    Well since English isn't your first language, let me give you a pointer. The word good has two Os in it. It's not spelled "gd".

    Quzah.
    haha, dont pick on me at this word plz, u know what it means.

  9. #9
    Banned
    Join Date
    Aug 2004
    Posts
    128
    i didn't im glad he pointed that out, he taught me something,
    do take that to heart quzah!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 05:02 PM

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