Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > print to file question

Reply
Thread Tools

print to file question

 
 
philbo30
Guest
Posts: n/a
 
      08-01-2006
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

 
Reply With Quote
 
 
 
 
Hallvard B Furuseth
Guest
Posts: n/a
 
      08-01-2006
> 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
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      08-01-2006
philbo30 <(E-Mail Removed)> 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.
 
Reply With Quote
 
philbo30
Guest
Posts: n/a
 
      08-02-2006
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 <(E-Mail Removed)> 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.


 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      08-02-2006
[Please don't top-post in comp.lang.c. Corrected...]

philbo30 wrote:
> Christopher Benson-Manica wrote:
> > philbo30 <(E-Mail Removed)> 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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-02-2006
"philbo30" <(E-Mail Removed)> writes:
> Christopher Benson-Manica wrote:
>> philbo30 <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
philbo30
Guest
Posts: n/a
 
      08-02-2006
> 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.

 
Reply With Quote
 
philbo30
Guest
Posts: n/a
 
      08-02-2006
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" <(E-Mail Removed)> writes:
> > Christopher Benson-Manica wrote:
> >> philbo30 <(E-Mail Removed)> 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) (E-Mail Removed) <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.


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      08-02-2006
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) (E-Mail Removed) <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.
 
Reply With Quote
 
philbo30
Guest
Posts: n/a
 
      08-02-2006
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) (E-Mail Removed) <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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
Lock Print Screen and File Print Bryan Shaw ASP .Net 3 04-20-2005 02:54 PM
How to make use of "print to file" option by print =?Utf-8?B?enljZ3M=?= MCSE 1 01-04-2005 06:50 PM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM



Advertisments