用C语言读取限制字节 [英] Read limit byte with C language

查看:184
本文介绍了用C语言读取限制字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿大家

i有一个图像,我想当我开始通过c编程中的文件读取图像,按块读取或者之前定义的限制字节数,这意味着读取限制字节例如32字节,在我想要计算该块中每个字节的概率之后

thankx



我有什么试过:



FILE * fp = fopen(image.pgm,rb)

while(c = fgetc(fp)!= EOF)

{

//读取第32个字节

//计算每个字节的概率

//返回文件中的位置32

}

解决方案

你什么也没做,我们没有做你的作业。



你最好学习C,并按照tutos:



C编程语言 - 维基百科,免费的百科全书 [ ^ ]

https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf [ ^ ]

http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf [ ^ ]


不要使用 fgetc 来读取文件,尤其是二进制数据。按照 https:// msdn中所述使用 fread 。 microsoft.com/en-us/library/kt0etdcs.aspx [ ^ ]。


这是读取文件的基本代码

 < span class =code-keyword> unsigned   char  buffer [ 64 ];  //  缓冲区可以是任意大小 
FILE * fp = fopen( image.pgm RB);
int numread;

while (!feof(fp)) // 重复,直到达到文件结尾
{
int index; // 缓冲区索引
numread = fread(buffer, sizeof unsigned char ), 64 ,fp);
if (numread == 0
断裂; // 无需读取数据
for (index = 0 ; index< numread; ++ index)
{
// 处理缓冲区的每个字节
int b = buffer [指数]; // b包含当前字节的值
printf( byte%d:%d \ n,index,b);
}
}


hey everyone
i have a image , i want when i start to read the image by the file in c programming , read by block or a limit number of byte defined before , it means read a limit byte for example 32 byte , after i want to calculate the probability for every byte in this block
thankx

What I have tried:

FILE *fp=fopen("image.pgm","rb")
while (c=fgetc(fp)!=EOF)
{
//read the 32 first byte
//calcule the probability of every byte
//return to position 32 in the file
}

解决方案

You have done nothing and we don't do your homework.

You better learn C properly, and follow tutos:

The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]


Do not use fgetc to read files, especially binary data. Use fread as described at https://msdn.microsoft.com/en-us/library/kt0etdcs.aspx[^].


This is the very basic code to read the file

unsigned char buffer[64];	// buffer may be any size
FILE *fp = fopen("image.pgm", "rb");
int numread;

while (!feof(fp))  // repeat until reached end of file
{
    int index;	// index into the buffer
    numread = fread(buffer, sizeof(unsigned char), 64, fp);
    if (numread == 0)
        break;  // no data left to read
    for (index = 0; index < numread; ++index)
    {
        // process each byte of the buffer
        int b = buffer[index];	// b contains the value of the current byte
        printf("byte %d: %d\n", index, b);
    }
}


这篇关于用C语言读取限制字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆