void reading(char *filename, list* l) { FILE* fp; int i = 0; if ((fp = fopen(filename, "rb")) == NULL) { printf("读取内存失败\n"); exit(0); } while (!feof(fp)) { fread(&l->a[i++], sizeof(music), 1, fp); l->size++; } }这段代码哪里出错了
时间: 2023-02-10 19:11:02 浏览: 175
void reading(char *filename, list* l) 是一个 C 语言函数,它的作用是打开名为 filename 的文件,并将文件中的内容读入到 l 指向的列表结构体中。
在函数的开始,会定义一个文件指针 fp,并使用 fopen 函数尝试打开文件。如果打开失败,会输出一条消息 "读取内存失败",并使用 exit(0) 终止程序的执行。如果文件打开成功,会进入一个无限循环,每次使用 fread 函数从文件中读取一个数据块,并将数据块添加到 l 指向的列表中。当文件已经读完时,feof 函数会返回真,循环就会终止。
相关问题
void Zoom(const char *filename,double lx,double ly){ FILE *fp = fopen(filename,"rb"); if (fp == NULL) printf("Error"); BITMAPFILEHEADER fileHead; fread(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp); BITMAPINFOHEADER infoHead; fread(&infoHead, sizeof(BITMAPINFOHEADER), 1, fp); int width = infoHead.biWidth; int height = infoHead.biHeight; int biCount = infoHead.biBitCount; RGBQUAD *pColorTable; int pColorTableSize = 0; pColorTable = new RGBQUAD[256]; fread(pColorTable, sizeof(RGBQUAD), 256, fp); pColorTableSize = 1024; unsigned char *pBmpBuf; int lineByte = (width*biCount / 8 + 3) / 4 * 4; pBmpBuf = new unsigned char[lineByte*height]; fread(pBmpBuf, lineByte*height, 1, fp); fclose(fp); int dstWidth = round(double(lx*width)); int dstHeight = round(double(ly*height)); int lineByte2 = (dstWidth*biCount / 8 + 3) / 4 * 4; unsigned char*pBmpBuf2; pBmpBuf2 = new unsigned char[lineByte2*dstHeight]; for (int i = 0; i < dstHeight; ++i){ for (int j = 0; j < dstWidth; ++j){ unsigned char *p; p = (unsigned char *)(pBmpBuf2 + lineByte2*i + j); (*p) = 255; } } int x = 0; int y = 0; for (int i = 0; i < height; ++i){ for (int j = 0; j < width; ++j){ unsigned char *p1, *p2; x = round(double(lx*j)); y = round(double(ly*i)); p1 = (unsigned char *)(pBmpBuf + i*lineByte + j); p2 = (unsigned char *)(pBmpBuf2 + y*lineByte2 + x); (*p2) = (*p1); } }
这段代码是一个图像缩放的函数,下面是对代码的中文注释:
```cpp
// 将指定文件中的图像进行缩放后保存
// 参数filename为图像文件名,lx和ly为横向和纵向的缩放比例
void Zoom(const char *filename, double lx, double ly){
FILE *fp = fopen(filename,"rb");
if (fp == NULL)
printf("Error");
// 读取位图文件头和位图信息头
BITMAPFILEHEADER fileHead;
fread(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp);
BITMAPINFOHEADER infoHead;
fread(&infoHead, sizeof(BITMAPINFOHEADER), 1, fp);
// 获取图像的宽度、高度和颜色位数
int width = infoHead.biWidth;
int height = infoHead.biHeight;
int biCount = infoHead.biBitCount;
RGBQUAD *pColorTable;
int pColorTableSize = 0;
// 读取调色板数据
pColorTable = new RGBQUAD[256];
fread(pColorTable, sizeof(RGBQUAD), 256, fp);
pColorTableSize = 1024;
// 读取图像数据
unsigned char *pBmpBuf;
int lineByte = (width * biCount / 8 + 3) / 4 * 4;
pBmpBuf = new unsigned char[lineByte * height];
fread(pBmpBuf, lineByte * height, 1, fp);
fclose(fp);
// 计算缩放后的图像宽度和高度,并计算缩放后的每行字节数
int dstWidth = round(double(lx * width));
int dstHeight = round(double(ly * height));
int lineByte2 = (dstWidth * biCount / 8 + 3) / 4 * 4;
// 创建缩放后的图像数据缓冲区,并初始化为白色
unsigned char *pBmpBuf2;
pBmpBuf2 = new unsigned char[lineByte2 * dstHeight];
for (int i = 0; i < dstHeight; ++i){
for (int j = 0; j < dstWidth; ++j){
unsigned char *p;
p = (unsigned char *)(pBmpBuf2 + lineByte2 * i + j);
(*p) = 255;
}
}
// 对每个像素进行缩放处理
int x = 0;
int y = 0;
for (int i = 0; i < height; ++i){
for (int j = 0; j < width; ++j){
unsigned char *p1, *p2;
x = round(double(lx * j));
y = round(double(ly * i));
p1 = (unsigned char *)(pBmpBuf + i * lineByte + j);
p2 = (unsigned char *)(pBmpBuf2 + y * lineByte2 + x);
(*p2) = (*p1);
}
}
// 将缩放后的图像数据保存到新文件中
FILE *fp2 = fopen("zoomed.bmp", "wb");
if (fp2 == NULL)
printf("Error");
fwrite(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp2);
fwrite(&infoHead, sizeof(BITMAPINFOHEADER), 1, fp2);
fwrite(pColorTable, sizeof(RGBQUAD), 256, fp2);
fwrite(pBmpBuf2, lineByte2 * dstHeight, 1, fp2);
fclose(fp2);
// 释放内存
delete[] pColorTable;
delete[] pBmpBuf;
delete[] pBmpBuf2;
}
```
希望这些注释能帮助你理解代码的功能和实现过程。如果还有其他问题,请随时提问。
修改以下代码“”“#include<stdio.h> #define N 5 typedef struct student{ long sno; char name[10]; float score[3]; }STU; void fun(char *filename,STU n){ FILE *fp; /**********found**********/ fp=fopen(filename,"rb"); /*********”
#include <stdio.h>
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, STU n) {
FILE *fp;
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("Cannot open file!\n");
return;
}
fread(&n, sizeof(STU), 1, fp);
printf("sno:%ld\nname:%s\nscore[0]:%.2f\nscore[1]:%.2f\nscore[2]:%.2f\n", n.sno, n.name, n.score[0], n.score[1], n.score[2]);
fclose(fp);
}
int main() {
STU s[N] = {
{20210001, "Tom", {89, 78, 92}},
{20210002, "Jerry", {78, 87, 91}},
{20210003, "Mickey", {90, 77, 83}},
{20210004, "Minnie", {94, 92, 90}},
{20210005, "Donald", {85, 88, 82}}
};
FILE *fp;
fp = fopen("test.dat", "wb");
if (fp == NULL) {
printf("Cannot open file!\n");
return 0;
}
fwrite(&s[2], sizeof(STU), 1, fp);
fclose(fp);
fun("test.dat", s[2]);
return 0;
}
```
阅读全文