更新时间:2023-12-05 13:17:04
使用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);
}