Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何同时输出到控制台和文本文件,使它们完全相同_C_File Io_Text Files - Fatal编程技术网

C 如何同时输出到控制台和文本文件,使它们完全相同

C 如何同时输出到控制台和文本文件,使它们完全相同,c,file-io,text-files,C,File Io,Text Files,下面创建一个随机数表并将其打印到控制台上。如何修改我创建的createtxt函数,使控制台上的输出同时生成文本文件 #include <stdio.h> #include <stdlib.h> #include <time.h> FILE* createtxt(char* fnam){ FILE* ofp; ofp = fopen(fnam, "w"); if (ofp == NULL) { printf("Cann

下面创建一个随机数表并将其打印到控制台上。如何修改我创建的createtxt函数,使控制台上的输出同时生成文本文件

#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;
}
#包括
#包括
#包括
文件*createtxt(字符*fnam){
文件*ofp;
ofp=fopen(fnam,“w”);
如果(ofp==NULL){
printf(“无法打开输出文件%s\n”,fnam);
退出(退出失败);
}
void closetxt(文件*ofp){
fclose(ofp);
}
内部主(空){
printf(“用于绘制不同颜色、尺寸和不透明度的几何形状的随机数表”);
int rn=0;
无符号整数种子=(无符号整数)时间(NULL);
srand(种子);
int k=0;
printf(“shape\tRSCE\tx\ty\twidth\theight\tred\tgreen\tblue\tgreen\topacity\n”);
while(k
此版本是根据printf()语句完成的:(参见下文中的
createtxt()

在printf语句之前打开文件:

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);     
如果从createtxt中选择:

将原型更改为
intcreatetxt(char*fnam,char*buf)

而不是使用

sprintf(buf, "\t%4d",rn);//use sprintf() to put information into buf
printf(buf);             //output to stdout  
用法:
//创建并初始化一个更大的缓冲区

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  
然后将buf作为参数传递给
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);
}
此版本是根据printf()语句完成的:(参见下面的
createtxt()

在printf语句之前打开文件:

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);     
如果从createtxt中选择:

将原型更改为
intcreatetxt(char*fnam,char*buf)

而不是使用

sprintf(buf, "\t%4d",rn);//use sprintf() to put information into buf
printf(buf);             //output to stdout  
用法:
//创建并初始化一个更大的缓冲区

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  
然后将buf作为参数传递给
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);
}

我可能会考虑创建一个
ffprintf()
函数:

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>

extern int ffprintf(FILE *fp1, FILE *fp2, char const *fmt, ...);

int ffprintf(FILE *fp1, FILE *fp2, char const *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    int rc1 = vfprintf(fp1, fmt, args);
    va_end(args);
    va_start(args, fmt);
    int rc2 = vfprintf(fp2, fmt, args);
    va_end(args);
    assert(rc1 == rc2);
    return rc1;
}
extern int ffprintf(FILE *fp1, FILE *fp2, char const *fmt, int value);

int ffprintf(FILE *fp1, FILE *fp2, char const *fmt, value)
{
    int rc1 = fprintf(fp1, fmt, value);
    int rc2 = vfprintf(fp2, fmt, value);
    assert(rc1 == rc2);
    return rc1;
}
现在必须写入两次标题:

char const header[] =
     "shape#\tRSCE\tx\ty\twidth\theight\tred\tgreen\tblue\tgreen\topacity\n";

fputs(header, stdout);
fputs(header, ofp);
其余的大部分可以保持不变,但不透明度(浮动)需要特别处理,以及标题行

显然,如果您不喜欢函数名会侵犯函数的标准名称,那么函数名是可以更改的。如果使用更简单的方法,函数名应该更改为类似
ffprint\u int()
,因为它一次只打印一个
int

assert()
的使用是惰性的,但它确实确保两个打印操作都成功(或都失败)。如果您对此感到担心,请将该断言更改为类似以下内容:

if (rc2 < rc1)
    rc1 = rc2;
if(rc2

这将返回两个值中较小的一个;如果其中一个值为-1(完全失败),则将返回该值。当然,由于我编写的代码忽略了
ffprintf()
中的返回值,正如您的代码忽略了
printf()中的返回值一样
,这是一个基本上不会被注意到的细微差别。

我可能会考虑创建一个
ffprintf()
函数:

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>

extern int ffprintf(FILE *fp1, FILE *fp2, char const *fmt, ...);

int ffprintf(FILE *fp1, FILE *fp2, char const *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    int rc1 = vfprintf(fp1, fmt, args);
    va_end(args);
    va_start(args, fmt);
    int rc2 = vfprintf(fp2, fmt, args);
    va_end(args);
    assert(rc1 == rc2);
    return rc1;
}
extern int ffprintf(FILE *fp1, FILE *fp2, char const *fmt, int value);

int ffprintf(FILE *fp1, FILE *fp2, char const *fmt, value)
{
    int rc1 = fprintf(fp1, fmt, value);
    int rc2 = vfprintf(fp2, fmt, value);
    assert(rc1 == rc2);
    return rc1;
}
现在必须写入两次标题:

char const header[] =
     "shape#\tRSCE\tx\ty\twidth\theight\tred\tgreen\tblue\tgreen\topacity\n";

fputs(header, stdout);
fputs(header, ofp);
其余的大部分可以保持不变,但不透明度(浮动)需要特别处理,以及标题行

显然,如果您不喜欢函数名会侵犯函数的标准名称,那么函数名是可以更改的。如果使用更简单的方法,函数名应该更改为类似
ffprint\u int()
,因为它一次只打印一个
int

assert()
的使用是惰性的,但它确实确保两个打印操作都成功(或都失败)。如果您对此感到担心,请将该断言更改为类似以下内容:

if (rc2 < rc1)
    rc1 = rc2;
if(rc2

这将返回两个值中较小的一个;如果其中一个值为-1(完全失败),则将返回该值。当然,由于我编写的代码忽略了
ffprintf()
中的返回值,正如您的代码忽略了
printf()
中的返回值一样,这是一个基本上不会被注意到的细微差别。

printf()
文件的所有内容
“myrandom.txt
。将其读取并打印到
stdout
(假设这不是同时进行的)这样做是不可能的,因为你不能同时做两件事,即使你使用线程,它们也不能保证同时工作。你要么在创建线程时捕获它,然后将对屏幕的写操作与对文件的写操作交织在一起,要么继续将新的日期行连接到一个更大的文件中缓冲区,然后作为参数传递给
createtxt()
。两者如下所示。
printf()
文件的所有内容
“myrandom.txt
。读取并打印到
stdout
(假设这不是同时进行的)这样做是不可能的,因为你不能同时做两件事,即使你使用线程,它们也不能保证同时工作。你要么在创建线程时捕获它,然后将对屏幕的写操作与对文件的写操作交织在一起,要么继续将新的日期行连接到一个更大的文件中缓冲区,然后作为参数传递给
createtxt()
。两者如下所示。显然,更值得永久应用程序使用的解决方案。很好。显然,更值得永久应用程序使用的解决方案。很好。