如何同时输出到控制台和文本文件,使他们是相同的 [英] How to output simultaneously onto console and a text file such that they are identical
问题描述
以下内容创建并打印一个随机数字表到控制台上。我如何修改我所做的createtxt函数,以便控制台上的输出同时生成一个文本文件。
#include< stdio.h>
#include< stdlib.h>
#include< time.h>
FILE * createtxt(char * fnam){
FILE * ofp;
ofp = fopen(fnam,w);
if(ofp == NULL){
printf(无法打开输出文件%s \ n,fnam);
exit(EXIT_FAILURE);
void closetxt(FILE * ofp){
fclose(ofp);
int main(void){
printf(用于绘制不同颜色,大小和不透明度的几何图形的随机数表);
int rn = 0;
unsigned int seed =(unsigned int)time(NULL);
srand(seed);
int k = 0;
printf(shape#\ tRSCE \ tx \\\\\\\\\\\\\\'); (k printf(%6d,k);
。
rn = rand()%SHAPE_RANGE;
printf(\t%4d,rn);
rn = rand()%X_RANGE;
printf(\t%d,rn);
rn = rand()%Y_RANGE;
printf(\t%d,rn);
rn = rand()%WIDTH_RANGE;
printf(\t%5d,rn);
rn = rand()%HEIGHT_RANGE;
printf(\t%6d,rn);
rn = rand()%RED_RANGE;
printf(\t%3d,rn);
rn = rand()%GREEN_RANGE;
printf(\t%5d,rn);
rn = rand()%BLUE_RANGE;
printf(\t%4d,rn);
rn = rand()%OPACITY_RANGE;
printf(\t%.1f\\\
,rn / 100.0);
k ++;
FILE * ofp = createtxt(myrandom.txt)
closetxt(ofp);
return EXIT_SUCCESS;
使用printf()语句执行:(参见下面的 createtxt()
在printf语句之前打开文件:
pre $ FILE * ofp = createtxt(myrandom.txt);
char buf [20];
rn = rand()%SHAPE_RANGE;
sprintf(buf,\t%4d,rn); //使用sprintf()将信息放入buf
printf(buf); //输出到stdout
fputs(buf,ofp); //输出到文件
///为X_RANGE,Y_RANGE等重复行
fclose(ofp);
如果来自于createtxt:
将原型更改为 int createtxt(char * fnam,char * buf)
p>
而不是使用
sprintf(buf,\t%4d ,rn); //使用sprintf()把信息放入buf
printf(buf); //输出到stdout
使用:
//创建并初始化一个更大的缓冲区。
char buf [1000]; //或其他适当大小的缓冲区
buf [0] = 0; //将NULL设置为buf
的第一个位置//然后,在main的所有printf语句之间调用:
sprintf(buf,%s\t%4d,buf,rn); //将连接所有bufs直到结束
然后将buf作为参数传递给 createtxt ()
。
void createtxt(char * fnam,char * buf)
{
FILE * ofp;
ofp = fopen(fnam,w);
if(ofp == NULL){
printf(无法打开输出文件%s \
exit(EXIT_FAILURE);
{
fputs(buf,ofp);
fclose(ofp);
}
The following creates and prints a table of random numbers onto the console. How can I modify the createtxt function I made, so that the output on the console is generated into a text file at the same time.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
FILE* createtxt(char* fnam){
FILE* ofp;
ofp = fopen(fnam, "w");
if (ofp == NULL) {
printf("Cannot open output file %s\n", fnam);
exit(EXIT_FAILURE);
}
void closetxt(FILE* ofp){
fclose(ofp);
}
int main (void){
printf("Table of random numbers for drawing geometric shapes in different colours, sizes and opacities\n");
int rn = 0;
unsigned int seed = (unsigned int)time(NULL);
srand(seed);
int k = 0;
printf("shape#\tRSCE\tx\ty\twidth\theight\tred\tgreen\tblue\tgreen\topacity\n");
while (k < NO_SHAPES){
printf("%6d", k);
rn = rand() % SHAPE_RANGE;
printf( "\t%4d",rn);
rn = rand() % X_RANGE;
printf("\t%d",rn);
rn = rand() % Y_RANGE;
printf("\t%d",rn);
rn = rand() % WIDTH_RANGE;
printf("\t%5d",rn);
rn = rand() % HEIGHT_RANGE;
printf("\t%6d",rn);
rn = rand() % RED_RANGE;
printf("\t%3d",rn);
rn = rand() % GREEN_RANGE;
printf("\t%5d",rn);
rn = rand() % BLUE_RANGE;
printf("\t%4d",rn);
rn = rand() % OPACITY_RANGE;
printf("\t%.1f\n",rn/100.0);
k++;
}
FILE* ofp = createtxt("myrandom.txt")
closetxt(ofp);
return EXIT_SUCCESS;
}
This version is Done in-line with the printf() statements: (see below for in createtxt()
open the file before the printf statements:
FILE* ofp = createtxt("myrandom.txt");
char buf[20];
rn = rand() % SHAPE_RANGE;
sprintf(buf, "\t%4d",rn);//use sprintf() to put information into buf
printf(buf); //output to stdout
fputs(buf, ofp ); //output to file
/// repeat lines for X_RANGE, Y_RANGE, etc.
fclose(ofp);
If from within createtxt:
Change the prototype to int createtxt(char *fnam, char *buf)
rather than using
sprintf(buf, "\t%4d",rn);//use sprintf() to put information into buf
printf(buf); //output to stdout
Use:
//create and initialize a bigger buffer.
char buf[1000]; //or some other appropriately sized buffer
buf[0]=0; //set NULL to first position of buf
//then, between all the printf statements in main, call this:
sprintf(buf, %s\t%4d", buf, rn);//will concatenate all bufs until end
Then pass buf as argument to createtxt()
.
void createtxt(char* fnam, char *buf)
{
FILE* ofp;
ofp = fopen(fnam, "w");
if (ofp == NULL) {
printf("Cannot open output file %s\n", fnam);
exit(EXIT_FAILURE);
{
fputs(buf, ofp);
fclose(ofp);
}
这篇关于如何同时输出到控制台和文本文件,使他们是相同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!