print to file question

Discussion in 'C Programming' started by philbo30, Aug 1, 2006.

  1. philbo30

    philbo30 Guest

    Silly question, I know, but I have a code snippet that looks like this:

    printf("blah blah");
    printf("more blah");

    x = a + b - c;
    printf($%3.2f\n", x);

    y = d + e;
    printf($%3.2f\n, y);

    printf("stuff");


    and instead of it printing to the DOS window (as it does now), I want
    the output to print to a file so that the file may then be printed as a
    receipt by a receipt printer. Not that it really matters, but this is
    all on XP Pro. -Phil
    philbo30, Aug 1, 2006
    #1
    1. Advertising

  2. > and instead of it printing to the DOS window (as it does now), I want
    > the output to print to a file so that the file may then be printed as
    > a receipt by a receipt printer.


    Use the DOS command
    program > filename

    Or if you want to solve it in C, use
    FILE *f = fopen("filename", "w");
    if (f == NULL) { ERROR... }
    fprintf(f, "format string", arguments...);
    if (fclose(f) != 0) { ERROR...; }

    --
    Hallvard
    Hallvard B Furuseth, Aug 1, 2006
    #2
    1. Advertising

  3. philbo30 <> wrote:

    > printf("blah blah");


    (etc)

    > and instead of it printing to the DOS window (as it does now), I want
    > the output to print to a file so that the file may then be printed as a
    > receipt by a receipt printer.


    You have asked FAQ 12.33:

    http://c-faq.com/stdio/freopen.html

    Be sure to continue on to 12.34.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Aug 1, 2006
    #3
  4. philbo30

    philbo30 Guest

    freopen works to a point. It appears to write only 62 lines. Since I
    have about 70 lines, perhaps I need to change a buffer setting
    somewhere?

    Christopher Benson-Manica wrote:
    > philbo30 <> wrote:
    >
    > > printf("blah blah");

    >
    > (etc)
    >
    > > and instead of it printing to the DOS window (as it does now), I want
    > > the output to print to a file so that the file may then be printed as a
    > > receipt by a receipt printer.

    >
    > You have asked FAQ 12.33:
    >
    > http://c-faq.com/stdio/freopen.html
    >
    > Be sure to continue on to 12.34.
    >
    > --
    > C. Benson Manica | I *should* know what I'm talking about - if I
    > cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
    philbo30, Aug 2, 2006
    #4
  5. [Please don't top-post in comp.lang.c. Corrected...]

    philbo30 wrote:
    > Christopher Benson-Manica wrote:
    > > philbo30 <> wrote:
    > >
    > > > printf("blah blah");

    > >
    > > (etc)
    > >
    > > > and instead of it printing to the DOS window (as it does now), I want
    > > > the output to print to a file so that the file may then be printed as a
    > > > receipt by a receipt printer.

    > >
    > > You have asked FAQ 12.33:
    > >
    > > http://c-faq.com/stdio/freopen.html
    > >
    > > Be sure to continue on to 12.34.

    >
    > freopen works to a point. It appears to write only 62 lines. Since I
    > have about 70 lines, perhaps I need to change a buffer setting
    > somewhere?


    Perhaps you need to post the smallest compilable snippet of code that
    exhibits
    the problem.

    Our crystal balls may be digital these days, but it's still better to
    work
    on actual source code than vague ideas and errors.

    --
    Peter
    Peter Nilsson, Aug 2, 2006
    #5
  6. "philbo30" <> writes:
    > Christopher Benson-Manica wrote:
    >> philbo30 <> wrote:
    >>
    >> > printf("blah blah");

    >>
    >> (etc)
    >>
    >> > and instead of it printing to the DOS window (as it does now), I want
    >> > the output to print to a file so that the file may then be printed as a
    >> > receipt by a receipt printer.

    >>
    >> You have asked FAQ 12.33:
    >>
    >> http://c-faq.com/stdio/freopen.html
    >>
    >> Be sure to continue on to 12.34.

    >
    > freopen works to a point. It appears to write only 62 lines. Since I
    > have about 70 lines, perhaps I need to change a buffer setting
    > somewhere?


    Please don't top-post. See <http://www.caliburn.nl/topposting.html>.
    (I've corrected it here.)

    There's no reason I can think of why freopen() would cause you to be
    limited to printing 62 lines. Possibly you might get only partial
    output if you fail to close the file, but it should be closed
    implicitly when the program terminates.

    Show us an actual program that exhibits the problem and we might be
    able to help. By "actual program", I mean an *exact* copy of the
    program you actually compiled and ran; copy-and-paste it, don't
    paraphrase or re-type it. Keep it reasonably short.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 2, 2006
    #6
  7. philbo30

    philbo30 Guest

    > Use the DOS command
    > program > filename



    This works fine once. However, subsequent output is appended in the
    same file, which doesn't work. This .exe is an infinite loop, so the
    overall picture should be:

    input -> output to file -> print file -> delete file -> next input


    Interestingly, the DOS command writes all lines of output (about 70)
    while the freopen approach only gets 62 lines into the file. I prefer
    the freopen approach, since it doesn't append, but I'll have to solve
    the 62 line issue.
    philbo30, Aug 2, 2006
    #7
  8. philbo30

    philbo30 Guest

    Exact code, but in the interest of keeping it relatively short, not all
    calculations and printf shown. Compiler is "Pacific C for MS-DOS".

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    int main ()
    {
    time_t t;
    double payment; //Total dollars paid
    double volume; //Total volume purchased
    double costm3; //Cost per cubic meter
    double rate1; //Refill rate 1
    double rate2; //Refill rate 2

    int j;
    for (j=13207; j>0; j++) //Infinite Loop Code

    {
    FILE * config;

    config = fopen("config.txt", "r"); //opens config file
    rewind(config); //moves to beginning of config file

    //Next line reads data
    fscanf(config, "%lf %lf", &rate1,&rate2);
    fclose(config); //close config file*/

    printf("Enter volume bought: "); //Entered by user
    scanf("%lf", &volume);
    printf("Enter amount paid: "); //Entered by user
    scanf("%lf", &payment);

    freopen("out.txt","w",stdout);

    time(&t); //Time

    calculations and printf (1)
    calculations and printf (2)
    ...
    calculations and printf (70)
    }
    return 0;
    }



    Keith Thompson wrote:
    > "philbo30" <> writes:
    > > Christopher Benson-Manica wrote:
    > >> philbo30 <> wrote:
    > >>
    > >> > printf("blah blah");
    > >>
    > >> (etc)
    > >>
    > >> > and instead of it printing to the DOS window (as it does now), I want
    > >> > the output to print to a file so that the file may then be printed as a
    > >> > receipt by a receipt printer.
    > >>
    > >> You have asked FAQ 12.33:
    > >>
    > >> http://c-faq.com/stdio/freopen.html
    > >>
    > >> Be sure to continue on to 12.34.

    > >
    > > freopen works to a point. It appears to write only 62 lines. Since I
    > > have about 70 lines, perhaps I need to change a buffer setting
    > > somewhere?

    >
    > Please don't top-post. See <http://www.caliburn.nl/topposting.html>.
    > (I've corrected it here.)
    >
    > There's no reason I can think of why freopen() would cause you to be
    > limited to printing 62 lines. Possibly you might get only partial
    > output if you fail to close the file, but it should be closed
    > implicitly when the program terminates.
    >
    > Show us an actual program that exhibits the problem and we might be
    > able to help. By "actual program", I mean an *exact* copy of the
    > program you actually compiled and ran; copy-and-paste it, don't
    > paraphrase or re-type it. Keep it reasonably short.
    >
    > --
    > Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    > We must do something. This is something. Therefore, we must do this.
    philbo30, Aug 2, 2006
    #8
  9. philbo30

    Flash Gordon Guest

    philbo30 wrote:

    You are continuing to top post despite a request not to. This is highly
    impolite and likely to mean that some of the best experts here will
    start to ignore you.

    > Exact code, but in the interest of keeping it relatively short, not all
    > calculations and printf shown. Compiler is "Pacific C for MS-DOS".


    <snip>

    > calculations and printf (1)
    > calculations and printf (2)
    > ...
    > calculations and printf (70)
    > }
    > return 0;
    > }


    This is NOT an actual program. It is a paraphrase of it. Keith explictly
    requested you post actual code that fails NOT a paraphrase of it. How
    can andone tell you what you have done wrong without seeing what you
    have done?

    > Keith Thompson wrote:


    <snip>

    >> Please don't top-post. See <http://www.caliburn.nl/topposting.html>.
    >> (I've corrected it here.)


    See, a request not to top post.

    <snip>

    >> Show us an actual program that exhibits the problem and we might be
    >> able to help. By "actual program", I mean an *exact* copy of the
    >> program you actually compiled and ran; copy-and-paste it, don't
    >> paraphrase or re-type it. Keep it reasonably short.


    See, it says to not do what you have done.

    >> Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    >> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    >> We must do something. This is something. Therefore, we must do this.


    Also, please don't quote peoples signatures unless you are commenting on
    them.
    --
    Flash Gordon,
    Still sigless on this computer.
    Flash Gordon, Aug 2, 2006
    #9
  10. philbo30

    philbo30 Guest

    Flash Gordon wrote:
    > philbo30 wrote:
    >
    > You are continuing to top post despite a request not to. This is highly
    > impolite and likely to mean that some of the best experts here will
    > start to ignore you.
    >
    > > Exact code, but in the interest of keeping it relatively short, not all
    > > calculations and printf shown. Compiler is "Pacific C for MS-DOS".

    >
    > <snip>
    >
    > > calculations and printf (1)
    > > calculations and printf (2)
    > > ...
    > > calculations and printf (70)
    > > }
    > > return 0;
    > > }

    >
    > This is NOT an actual program. It is a paraphrase of it. Keith explictly
    > requested you post actual code that fails NOT a paraphrase of it. How
    > can andone tell you what you have done wrong without seeing what you
    > have done?
    >
    > > Keith Thompson wrote:

    >
    > <snip>
    >
    > >> Please don't top-post. See <http://www.caliburn.nl/topposting.html>.
    > >> (I've corrected it here.)

    >
    > See, a request not to top post.
    >
    > <snip>
    >
    > >> Show us an actual program that exhibits the problem and we might be
    > >> able to help. By "actual program", I mean an *exact* copy of the
    > >> program you actually compiled and ran; copy-and-paste it, don't
    > >> paraphrase or re-type it. Keep it reasonably short.

    >
    > See, it says to not do what you have done.
    >
    > >> Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    > >> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    > >> We must do something. This is something. Therefore, we must do this.

    >
    > Also, please don't quote peoples signatures unless you are commenting on
    > them.
    > --
    > Flash Gordon,
    > Still sigless on this computer.


    With all due respect, on the "top posting" link, the link was not
    working the last few times I tried to access it in order to figure out
    what "top posting" is. The link is working this morning and after
    reviewing it, I will be sure not to "top post again." -Philbo
    philbo30, Aug 2, 2006
    #10
  11. philbo30

    philbo30 Guest

    Peter Nilsson wrote:
    > [Please don't top-post in comp.lang.c. Corrected...]
    >
    > philbo30 wrote:
    > > Christopher Benson-Manica wrote:
    > > > philbo30 <> wrote:
    > > >
    > > > > printf("blah blah");
    > > >
    > > > (etc)
    > > >
    > > > > and instead of it printing to the DOS window (as it does now), I want
    > > > > the output to print to a file so that the file may then be printed as a
    > > > > receipt by a receipt printer.
    > > >
    > > > You have asked FAQ 12.33:
    > > >
    > > > http://c-faq.com/stdio/freopen.html
    > > >
    > > > Be sure to continue on to 12.34.

    > >
    > > freopen works to a point. It appears to write only 62 lines. Since I
    > > have about 70 lines, perhaps I need to change a buffer setting
    > > somewhere?

    >
    > Perhaps you need to post the smallest compilable snippet of code that
    > exhibits
    > the problem.
    >
    > Our crystal balls may be digital these days, but it's still better to
    > work
    > on actual source code than vague ideas and errors.
    >
    > --
    > Peter


    I posted the relevant portions. As an aside, I did some additional line
    counts and it is now writing anywhere from 62 to 66 lines to the .txt
    file. Bizarre.
    philbo30, Aug 2, 2006
    #11
  12. philbo30

    Default User Guest

    philbo30 wrote:

    >
    > Peter Nilsson wrote:


    > > Perhaps you need to post the smallest compilable snippet of code
    > > that exhibits
    > > the problem.
    > >
    > > Our crystal balls may be digital these days, but it's still better
    > > to work
    > > on actual source code than vague ideas and errors.


    > I posted the relevant portions. As an aside, I did some additional
    > line counts and it is now writing anywhere from 62 to 66 lines to the
    > .txt file. Bizarre.


    If you don't know what the problem is, how do you know which portions
    are relevant?

    As Peter said, try to work up a complete, minimal program that
    demonstrates the problem. That way people can compile and run exactly
    what you do and see what results they get.




    Brian
    Default User, Aug 2, 2006
    #12
  13. "philbo30" <> writes:
    > Peter Nilsson wrote:

    [...]
    >> Perhaps you need to post the smallest compilable snippet of code
    >> that exhibits the problem.
    >>
    >> Our crystal balls may be digital these days, but it's still better
    >> to work on actual source code than vague ideas and errors.

    >
    > I posted the relevant portions. As an aside, I did some additional line
    > counts and it is now writing anywhere from 62 to 66 lines to the .txt
    > file. Bizarre.


    No, you didn't. If you had, we'd be able to try your program
    ourselves and possibly reproduce your problem.

    One more thing: it's rarely necessary to quote the entire article to
    which you're replying. Just quote enough so your followup makes sense
    on its own. In particular, don't quote signatures unless you're
    commenting on them.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 2, 2006
    #13
  14. philbo30

    Guest

    philbo30 wrote:

    > Exact code, but in the interest of keeping it relatively short, not all
    > calculations and printf shown. Compiler is "Pacific C for MS-DOS".
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <time.h>
    >
    > int main ()
    > {
    > time_t t;
    > double payment; //Total dollars paid
    > double volume; //Total volume purchased
    > double costm3; //Cost per cubic meter
    > double rate1; //Refill rate 1
    > double rate2; //Refill rate 2
    >
    > int j;
    > for (j=13207; j>0; j++) //Infinite Loop Code
    >
    > {
    > FILE * config;
    >
    > config = fopen("config.txt", "r"); //opens config file
    > rewind(config); //moves to beginning of config file


    I have no experience with dos but I don't think
    that rewind is needed.

    >
    > //Next line reads data
    > fscanf(config, "%lf %lf", &rate1,&rate2);
    > fclose(config); //close config file*/
    >
    > printf("Enter volume bought: "); //Entered by user
    > scanf("%lf", &volume);
    > printf("Enter amount paid: "); //Entered by user
    > scanf("%lf", &payment);
    >
    > freopen("out.txt","w",stdout);
    >
    > time(&t); //Time
    >
    > calculations and printf (1)
    > calculations and printf (2)
    > ...
    > calculations and printf (70)
    > }
    > return 0;
    > }


    I'll take a shot in the dark based on how things work
    with Unix. Put a fflush(stdout) just before freopen

    Spiros Bousbouras
    , Aug 2, 2006
    #14
  15. philbo30

    philbo30 Guest


    > I'll take a shot in the dark based on how things work
    > with Unix. Put a fflush(stdout) just before freopen
    >
    > Spiros Bousbouras



    Thanks SB; that works with all of the testing I have done so far. Also,
    thanks to everyone who responded prior. As a rookie on the boards, I'm
    still getting used to proper etiquette and procedures. -Phil
    philbo30, Aug 6, 2006
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryan Shaw

    Lock Print Screen and File Print

    Bryan Shaw, Apr 19, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,943
    Kevin Spencer
    Apr 20, 2005
  2. Arun Kumar Srinivasan

    Using javax.print classes to print text/file

    Arun Kumar Srinivasan, Jan 13, 2004, in forum: Java
    Replies:
    5
    Views:
    6,141
    Gregory A. Swarthout
    Jan 14, 2004
  3. keto
    Replies:
    0
    Views:
    923
  4. Lew
    Replies:
    0
    Views:
    629
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    345
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page