如何使用C语言使用fopen

如何使用C语言使用fopen

本文介绍了如何使用C语言使用fopen()读取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用fopen()函数读取c中的图像后,我尝试转换到表格时,某些像素(caracter)无法转移并且只能错误地转移

例如某些像素当我用记事本打开图像=XGll ??®Ís(VGuëåëïëëïïëïïë)

在标签中变为ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ

哪里有问题?



我尝试了什么:



char ** tab;

nb = 1;

longth =(图像大小)按fseek函数计算



pfile =image.pgm;



tab =(char **)malloc((nb)* sizeof(char *));



fp = fopen(pfile,r);



for(i = 0; i< nb; i ++)>

{

tab [i] =(char *)malloc((40)* sizeof(char));

}



ff = fopen(tab [i],w);



for(j = 0; j< longth; j ++)>

{

c = fgetc(fp);



fputc((char)c,ff);

}

fclose(ff);

fclose( fp)

when i tried to read an image in c with fopen () function after i try to transfert to table , some pixel (caracter) can't transfert and only trasfert incorrectly
example some pixel in image when i opened with notepad= "XGll®Ís(VGuë åëïëëëïåëïìë")
in tab becomes "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ"
where are the probleme?

What I have tried:

char** tab;
nb=1;
longth=(size of the image) calculate by fseek function

pfile="image.pgm";

tab=(char**)malloc((nb)*sizeof(char*));

fp=fopen(pfile,"r");

for(i=0;i<nb;i++)>
{
tab[i]=(char*)malloc((40)*sizeof(char));
}

ff=fopen(tab[i],"w");

for(j=0;j<longth;j++)>
{
c=fgetc(fp);

fputc((char)c,ff);
}
fclose(ff);
fclose(fp)

推荐答案



fputc((char)c,ff);



c 大于127(字节的MSB设置)MSB将被您的转换清除写入不同的值(值128 / 0x80将变为零,129 / 0x81将变为1,依此类推)。只需传递 c 而不进行强制转换, fputc()函数将写入正确的值。如果你想对依赖于符号的值(比如移位)做一些事情,请声明 c 作为无符号类型。



使用Windows时,您还必须以二进制模式打开文件(rbresp。wb)。



您发布的代码不完整,它是不清楚你想要实现什么。因此无法提供更多帮助。


When c is greater than 127 (the MSB of the byte is set) the MSB will be cleared by your casting writing a different value (the value 128 / 0x80 will become zero, 129 / 0x81 will become 1 and so on). Just pass c without casting and the fputc() function will write the correct value. If you want to do something with the value that is sign dependent (like shifting) declare c as an unsigned type.

When using Windows you must also open the file in binary mode ("rb" resp. "wb").

Your posted code is incomplete and it is not clear what you want to achieve. So it is impossible to provide more help.


这篇关于如何使用C语言使用fopen()读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:45
查看更多