473,396 Members | 2,052 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,396 software developers and data experts.

I think I need to direct my output to a file.

Hello, I have recently started programming again, and right now everything I do is "printf" to the screen. If I want everything to go to a file, say for 1 or 10 million numbers below, could you please tell me how? I have done fread and fwrite OK, but I need more help.
Thank you.
Jim Devaney

/* Copyright (c) 1990, 1992, Borland International */
#include <stdio.h>
#include <conio.h>
#include <time.h>
long prime(long n)
{
long l;

if (n % 2 == 0)
return (n==2);
if (n % 3 == 0)
return (n==3);
if (n % 5 == 0)
return (n==5);
for (l=7; l*l <= n; l+=2)
if (n % l == 0)
return 0;
return 1;
}

void main(void)
{
long int l, n;
time_t s, t;
time(&s);

n = 1000000L;
for (l=2; l<=n; l++) {
if (l % 100000 == 0)
getch();
if (prime(l))
printf("%ld ", l);
}
time(&t);
printf("\nToday's date and time: %s\n", ctime(&s));
printf("Today's date and time: %s\n", ctime(&t));
getch();
}
Oct 22 '08 #1
3 1164
donbock
2,426 Expert 2GB
Most run-time environments permit you to redirect console output to a file. This is accomplished when you command the shell to execute your program; not when you write your program.
For example,
dir > file
sends the console output of the dir command to file.
Oct 22 '08 #2
Hello, thanks for the reply. I find that dir going to file works OK, but all of the stuff, the prime numbers, are inside the program. Like the line:
if (prime(l))
printf("%ld ", l);
remains in the program.
What are the ways to get printf ("%") into an output file? The results will keep going, but the other output will need to come back to itself, over and over for large numbers.
I hope I have explained it right.
Thank you.
Jim Devaney
Oct 22 '08 #3
Laharl
849 Expert 512MB
You can run your program at the shell with the same metacharacters that work for dir or any other builtin. If you're asking how to do file I/O, to write it directly to a file, you're best off Googling for "C file I/O".
Oct 23 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: C. Sengstock | last post by:
Hi, i want to save the keywords of an ini file in a struct, together with a fpos_t. I think i´m right with the concept, but the access through fsetpos() doesn´t work. The position is always wrong...
2
by: Johann Schuler | last post by:
I'm learning C#. I want to direct a console application's output to a screen where I can select and copy some of the text. How do I do this? Many thanks!
1
by: SMichal | last post by:
Hi, how can I start new process with this kind of application ? C:\Direct.exe -a -b -c <C:\Script.src C:\log.txt As you can see...I'll start appliaction ConnectDirect with 5 paramaters ...
3
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <string> using namespace std; class Test { public:
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.