print to file question

P

philbo30

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
 
H

Hallvard B Furuseth

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...; }
 
C

Christopher Benson-Manica

philbo30 said:
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.
 
P

philbo30

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?
 
P

Peter Nilsson

[Please don't top-post in comp.lang.c. Corrected...]
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.
 
K

Keith Thompson

philbo30 said:
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.
 
P

philbo30

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.
 
P

philbo30

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;
}
 
F

Flash Gordon

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".

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:

See, a request not to top post.


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

Also, please don't quote peoples signatures unless you are commenting on
them.
 
P

philbo30

Flash said:
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.




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?


See, a request not to top post.



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


Also, please don't quote peoples signatures unless you are commenting on
them.

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
 
P

philbo30

Peter said:
[Please don't top-post in comp.lang.c. Corrected...]
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.

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.
 
D

Default User

philbo30 said:
Peter Nilsson wrote:
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
 
K

Keith Thompson

philbo30 said:
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.
 
S

spibou

philbo30 said:
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
 
P

philbo30

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top