gpt4 book ai didi

c - 为什么txt文件的结果不符合预期?

转载 作者:行者123 更新时间:2023-11-30 20:25:18 25 4
gpt4 key购买 nike

下面的程序没有按我的预期工作。它应该读取具有特定格式的文件并写入具有相同值的文件。

sample.txt 已加载,输出为 sample_color.txt,请参阅下面的链接。

因此,文件的内容不相同。

让我知道如何解决它。

输入文件:sample.txt https://www.dropbox.com/s/jogg7rxp9fk7nyi/sample.txt?dl=0

输出文件:sample_color.txt https://www.dropbox.com/s/1v1bw7tntmwlkny/sample_color.txt?dl=0

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
using namespace std;

int main(int argc,char *argv[])
{

/*****************************************************
1.Definition of Variables
******************************************************/
FILE *fpr,*fpw; //file pointer for input&output file

int color;//
//int dn1,dn2,dn3,dn4,dn5;
unsigned char r,g,b;
float x,y,z;

if(argc!=3)
{
fprintf(stderr,"Usage: %s (1)input_org.txt\n(2)write_xyz FILENAME\n",argv[0]);
exit(1);
}

printf("OPEN FILE NAME:%s\n",argv[1]);


if((fpr=fopen(argv[1],"r"))==NULL)
{
printf("error\n");
exit(1);
}


if((fpw=fopen(argv[2],"w"))==NULL)
{
fprintf(stderr,"DSM by GSI.raw\n");
exit(1);
}

while(fscanf(fpr,"%f %f %f %d %d %d",&x,&y,&z,&r,&g,&b)!= EOF)
{
cout << x << endl;
//color=1000000*(int)r+1000*(int)g+(int)b;
//fprintf(fpw,"%f %f %f %d\n",x,y,z,color);
fprintf(fpw,"%f %f %f %d %d %d\n",x,y,z,r,g,b);
}

fclose(fpr);
fclose(fpw);
}

最佳答案

r, g, b 的类型与格式说明符不匹配。

由于 char 小于 int,输入将覆盖变量,这就是 x 值错误的原因。

您应该将类​​型更改为 int 以使其按您想要的方式工作:

int r, g, b;

如果您想知道 float 值的奇怪舍入错误,您应该阅读 What Every Computer Scientist Should Know About Floating-Point Arithmetic

关于c - 为什么txt文件的结果不符合预期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29343529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com