读入整形 c语言,C++ read函数读入int整形数据

Read函数定义

通过read函数将文件中的数据按照一定的长度读取出来并且存放在新的数组中。用于从文件中读取数据。

函数原型istream& read (char* s, streamsize n);

参数char* s取出数据的流向的char类型数组指针,streamsize n表示数组的长度

#include

using namespace std;

int read()//read函数主体部分

{

int x=0,f=1;char ch=getchar();

while(ch'9')

{

if(ch=='-')f=-1;

ch=getchar();

}

while(ch>='0'&&ch<='9')

{

x=x*10+ch-'0';

ch=getchar();

}

return x*f;

}

int main()

{

int n=read()//这就是读入了n(注意只能用来读入int类型的数据,long long还需更改)

system("pause");

return 0;

}

Read函数使用例子

#include // std::cout

#include // std::ifstream

int main () {

std::ifstream is ("test.txt", std::ifstream::binary);

if (is) {

// get length of file:

is.seekg (0, is.end);

int length = is.tellg();

is.seekg (0, is.beg);

char * buffer = new char [length];

std::cout << "Reading " << length << " characters... ";

// read data as a block:

is.read (buffer,length);

if (is)

std::cout << "all characters read successfully.";

else

std::cout << "error: only " << is.gcount() << " could be read";

is.close();

// ...buffer contains the entire file...

delete[] buffer;

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要了解图片文件的结构。常见的图片文件格式有BMP、JPEG、PNG等,这里以BMP格式为例进行说明。 BMP图片文件由文件头、位图信息头、调色板和位图数据四部分组成。其中,文件头和位图信息头是固定的,调色板和位图数据则根据图片的位深度和颜色数目而不同。 接下来,我们将以C语言为例,介绍如何读入一张BMP图片,并显示出来。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma pack(2) // 结构体按2字节对齐 // BMP文件头结构体 typedef struct BMPFileHeader { char bfType[2]; // 文件类型,必须为"BM" unsigned int bfSize; // 文件大小,单位为字节 unsigned short bfReserved1; // 保留,必须为0 unsigned short bfReserved2; // 保留,必须为0 unsigned int bfOffBits; // 位图数据的起始位置,单位为字节 } BMPFileHeader; // BMP位图信息头结构体 typedef struct BMPInfoHeader { unsigned int biSize; // 信息头的大小,单位为字节 int biWidth; // 图像的宽度,单位为像素 int biHeight; // 图像的高度,单位为像素 unsigned short biPlanes; // 必须为1 unsigned short biBitCount; // 每个像素所占位数 unsigned int biCompression; // 压缩方式,0表示不压缩 unsigned int biSizeImage; // 图像数据的大小,单位为字节 int biXPelsPerMeter; // 水平分辨率,单位为像素/m int biYPelsPerMeter; // 垂直分辨率,单位为像素/m unsigned int biClrUsed; // 使用的颜色数目,0表示使用所有颜色 unsigned int biClrImportant; // 重要的颜色数目,0表示所有颜色都重要 } BMPInfoHeader; // 读取BMP图片 unsigned char* readBMP(char* filename, int* width, int* height, int* channels) { FILE* fp = fopen(filename, "rb"); if (!fp) { printf("Failed to open file: %s\n", filename); return NULL; } BMPFileHeader fileHeader; BMPInfoHeader infoHeader; fread(&fileHeader, sizeof(BMPFileHeader), 1, fp); fread(&infoHeader, sizeof(BMPInfoHeader), 1, fp); if (strcmp(fileHeader.bfType, "BM") != 0) { printf("Not a BMP file: %s\n", filename); fclose(fp); return NULL; } if (infoHeader.biBitCount != 24 && infoHeader.biBitCount != 32) { printf("Only support 24-bit or 32-bit BMP file: %s\n", filename); fclose(fp); return NULL; } *width = infoHeader.biWidth; *height = abs(infoHeader.biHeight); *channels = infoHeader.biBitCount / 8; int dataSize = *width * *height * *channels; unsigned char* data = (unsigned char*)malloc(dataSize); fseek(fp, fileHeader.bfOffBits, SEEK_SET); fread(data, dataSize, 1, fp); fclose(fp); return data; } // 显示图片 void showImage(unsigned char* data, int width, int height, int channels) { // TODO: 实现显示图片的代码 } int main() { int width, height, channels; unsigned char* data = readBMP("test.bmp", &width, &height, &channels); if (!data) { return 1; } showImage(data, width, height, channels); free(data); return 0; } ``` 在上面的代码中,我们定义了两个结构体`BMPFileHeader`和`BMPInfoHeader`,分别对应BMP文件头和位图信息头。在`readBMP`函数中,我们首先打开指定的BMP文件,然后依次读取文件头和位图信息头。接着,我们判断文件类型是否为BMP格式,以及位深度是否为24位或32位。最后,我们根据图片的宽、高、通道数计算出位图数据的大小,读取位图数据并返回。 在`showImage`函数中,我们需要根据读取到的图片数据进行显示,这里就不再展开讲解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值