473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

print to file question

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

Aug 1 '06 #1
14 2405
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
Aug 1 '06 #2
philbo30 <ta***@vt.eduwrote:
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.
Aug 1 '06 #3
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 <ta***@vt.eduwrote:
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.
Aug 2 '06 #4
[Please don't top-post in comp.lang.c. Corrected...]

philbo30 wrote:
Christopher Benson-Manica wrote:
philbo30 <ta***@vt.eduwrote:
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

Aug 2 '06 #5
"philbo30" <ta***@vt.eduwrites:
Christopher Benson-Manica wrote:
>philbo30 <ta***@vt.eduwrote:
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) ks***@mib.org <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.
Aug 2 '06 #6
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.

Aug 2 '06 #7
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" <ta***@vt.eduwrites:
Christopher Benson-Manica wrote:
philbo30 <ta***@vt.eduwrote:

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) ks***@mib.org <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.
Aug 2 '06 #8
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) ks***@mib.org <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.
Aug 2 '06 #9
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) ks***@mib.org <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

Aug 2 '06 #10

Peter Nilsson wrote:
[Please don't top-post in comp.lang.c. Corrected...]

philbo30 wrote:
Christopher Benson-Manica wrote:
philbo30 <ta***@vt.eduwrote:
>
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.

Aug 2 '06 #11
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
Aug 2 '06 #12
"philbo30" <ta***@vt.eduwrites:
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) ks***@mib.org <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.
Aug 2 '06 #13
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 '06 #14
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

Aug 6 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: MouseHart | last post by:
I've written a simple program in VB 6.0 to list all my MP3 files. To show them on the screen I used an MSFlexGrid named TextGrid (which is not associated with any table or text file) in the...
9
by: François Pinard | last post by:
Hi, people. I hope someone would like to enlighten me. For any application handling Unicode internally, I'm usually careful at properly converting those Unicode strings into 8-bit strings before...
2
by: Rob McLennan - ZETLAND | last post by:
Hi, I have set up an external stylesheet, named "print.css", to format the style of all pages printed from my company's website. I've been previewing my changes to the stylesheet by doing...
7
by: Pat | last post by:
I would like to send the Print Preview of a MS Access form to a Snapshot file. The form contains an OLE graph. BACKGROUND A snapshot of a report is possible. If I could I would use a report to...
2
by: Rainbow | last post by:
I know that print funciton will be available in the application for each type of file. My question is, can I print a file(any type, e.g. word, bmp, html, etc) in program? No printer dialog...
9
by: jim-on-linux | last post by:
This is the situation I'm in. I've built a single file utility using py2exe. I zip the dist directory and send it to the client. For clients that use win95, win98 machines, They unpack the...
3
by: AWasilenko | last post by:
I'm still in the process of learning python via a handful of books I bought. One book I am reading just introduced Base Class Methods. I found that I needed more understanding on this concept and...
45
by: Umesh | last post by:
please help. thanks.
0
by: =?Utf-8?B?RGFpc3k=?= | last post by:
Situation: 1. have one default.aspx file 2. inside default.aspx file, contains several usercontrol, .ascx file, e.g. 1.ascx, 2.ascx 3. in the 1.ascx file, there is a print button or link 4. when...
18
by: =?Utf-8?B?SGVyYg==?= | last post by:
I'm using a ReportViewer in ASP.NET. From the development environment it works fine. When published and I try to open the page with the ReportViewer I get the error: An internal error occurred...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.